Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Opencv 使用圆形Hough变换检测圆无法正确工作_Opencv_C++11_Hough Transform - Fatal编程技术网

Opencv 使用圆形Hough变换检测圆无法正确工作

Opencv 使用圆形Hough变换检测圆无法正确工作,opencv,c++11,hough-transform,Opencv,C++11,Hough Transform,我想使用OpenCV和圆形Hough变换(HoughCircles)检测圆形对象。在图像中,我有3个圆圈,但设置阈值时,我只能检测到其中一个(或两个)。我想有更健壮的代码,可以检测所有3个圆,然后我会过滤感兴趣的圆(基本上使用半径)。但首先,我怎样才能同时检测出所有3个圆。有什么帮助吗?这是我的密码 #include <opencv2/opencv.hpp> #include <iostream> #include <string>

我想使用OpenCV和圆形Hough变换(
HoughCircles
)检测圆形对象。在图像中,我有3个圆圈,但设置阈值时,我只能检测到其中一个(或两个)。我想有更健壮的代码,可以检测所有3个圆,然后我会过滤感兴趣的圆(基本上使用半径)。但首先,我怎样才能同时检测出所有3个圆。有什么帮助吗?这是我的密码

    #include <opencv2/opencv.hpp>
    #include <iostream>
    #include <string>
    #include <vector>

    using namespace cv;
    using namespace std;

    int thresh = 100;
    int max_thresh = 9000;
    Mat src;
    void thresh_callback(int, void* );

    int main()
    {

                 cv::Mat src = cv::imread("w1.png");
                 resize(src, src, Size(640,480), 0, 0, INTER_CUBIC);
                  char* source_window = "Source";
                  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
                  imshow( source_window, src );

                  createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
                  thresh_callback( 0, 0 );

    waitKey(0);
    return(0);
    }

    void thresh_callback(int, void* ) {

          Mat src_gray, gray;
          cv::Mat bgr_image = cv::imread( "w1.png");
          cv::Mat orig_image = bgr_image.clone();

         //blur( bgr_image, src_gray, Size(1,1) );
           medianBlur(bgr_image, src_gray,1);
          //cv::GaussianBlur(bgr_image, src_gray, cv::Size(1, 1), 1, 1);
          cvtColor( src_gray,gray, CV_BGR2GRAY );

        Mat canny_output;
        Canny( gray, canny_output, thresh, thresh*1, 5,true );

                    Mat bw,dil,bw1,erd;
                    dilate(canny_output,dil,Mat());
                    erode(dil,erd,Mat());
                    Mat tmp=canny_output.clone();
                    Size kernalSize (15,15);
                    Mat element = getStructuringElement (MORPH_RECT, kernalSize, Point(9,9)  );
                    morphologyEx( canny_output, bw1, MORPH_CLOSE, element );

            // Use the Hough transform to detect circles in the combined threshold image

std::vector<cv::Vec3f> circles;
            cv::HoughCircles(canny_output, circles, CV_HOUGH_GRADIENT, 1, canny_output.rows/1.1, 10, 100, 10, 0);
            //cv::HoughCircles(blue_hue_image, circles, CV_HOUGH_GRADIENT, 1, blue_hue_image.rows/1, 10, 100, 10, 0);
            // Loop over all detected circles and outline them on the original image
            if(circles.size() == 0) std::exit(-1);
            for(size_t current_circle = 0; current_circle < circles.size(); ++current_circle) {
                Point center(cvRound(circles[current_circle][0]), cvRound(circles[current_circle][1]));
                int radius = cvRound(circles[current_circle][2]);
                cv::circle(orig_image, center, radius, cv::Scalar(0, 255, 0), 10);
            }
            // Show images

             resize(orig_image, orig_image, Size(640,480), 0, 0, INTER_CUBIC);
             char* source_window4 = "Detected window on the input image";
             namedWindow( source_window4, CV_WINDOW_AUTOSIZE );
             imshow( source_window4, orig_image );

    }
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int thresh=100;
int max_thresh=9000;
Mat-src;
无效阈值回调(int,void*);
int main()
{
cv::Mat src=cv::imread(“w1.png”);
调整大小(src,src,大小(640480),0,0,中间立方);
char*source\u window=“source”;
namedWindow(源窗口、CV窗口、自动调整大小);
imshow(源窗口,src);
createTrackbar(“Canny thresh:”、“Source”、&thresh、max_thresh、thresh_回调);
thresh_回调(0,0);
等待键(0);
返回(0);
}
无效阈值回调(int,void*){
材料src_灰色、灰色;
cv::Mat bgr_image=cv::imread(“w1.png”);
cv::Mat orig_image=bgr_image.clone();
//模糊(bgr_图像,src_灰度,大小(1,1));
medianBlur(bgr_图像,src_灰度,1);
//cv::高斯模糊(bgr_图像,src_灰度,cv::大小(1,1),1,1);
CVT颜色(src_灰色、灰色、CV_BGR2灰色);
Mat-canny_输出;
Canny(灰色,Canny_输出,阈值,阈值*1,5,真);
Mat bw、dil、bw1、erd;
扩张(canny_输出,dil,Mat());
侵蚀(dil、erd、Mat());
Mat tmp=canny_output.clone();
粒径(15,15);
Mat元素=getStructuringElement(变形、内核大小、点(9,9));
形态学(canny_输出,bw1,形态闭合,元素);
//使用Hough变换在组合阈值图像中检测圆
std::向量圆;
cv::HoughCircles(canny_输出,圆,cv_-HOUGH_梯度,1,canny_输出。行/1.1,10,100,10,0);
//cv::HoughCircles(蓝色色调图像、圆形、cv色调梯度、1、蓝色色调图像行/1、10、100、10、0);
//在所有检测到的圆上循环,并在原始图像上勾勒出它们的轮廓
如果(circles.size()==0)std::exit(-1);
用于(大小为当前圆=0;当前圆<圆。大小();+当前圆){
点中心(cvRound(圆[当前_圆][0]),cvRound(圆[当前_圆][1]);
int radius=cvRound(圆[current_circle][2]);
cv::圆(原始图像,中心,半径,cv::标量(0,255,0),10);
}
//显示图像
调整大小(原始图像、原始图像、大小(640480)、0、0、中间立方);
char*source\u window4=“在输入图像上检测到窗口”;
namedWindow(源窗口4,CV窗口自动大小);
imshow(源窗口4,原始图像);
}
这里是输入图像

结果图像

我不知道你为什么认为有三个圆圈。我至少能看到8个。好的,我只会发现主要的。两个写着一个世界,一个在门窗上。我对一些小的、不规则的或半圆形的不感兴趣。现在可以了吗?左边的“一个世界”更像是一个鸡蛋,门上的圆圈里有一个圆圈。但这就是为什么要有阈值来匹配不完美的圆。圆检测不适用于椭圆或蛋,只适用于圆;)有了阈值,我可以得到两个同一个世界的圆圈或窗口的圆圈。但这三者永远不会在一起。所以我的问题是为什么。有什么帮助吗?