Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/134.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
C++ 链接缺少(通过Canny)边_C++_Opencv_Edge Detection_Canny Operator - Fatal编程技术网

C++ 链接缺少(通过Canny)边

C++ 链接缺少(通过Canny)边,c++,opencv,edge-detection,canny-operator,C++,Opencv,Edge Detection,Canny Operator,我需要检测图像中的所有矩形 这是我的密码: Mat PolygonsDetection(Mat src) { Mat gray; cvtColor(src, gray, CV_BGR2GRAY); Mat bw; Canny(src, bw, 50, 200, 3, true); imshow("canny", bw); morphologyEx(bw, bw, MORPH_CLOSE, cv::noArray(),cv::Point(-1,

我需要检测图像中的所有矩形

这是我的密码:

Mat PolygonsDetection(Mat src)
 {
    Mat gray;
    cvtColor(src, gray, CV_BGR2GRAY);

    Mat bw;
    Canny(src, bw, 50, 200, 3, true);
    imshow("canny", bw);

    morphologyEx(bw, bw, MORPH_CLOSE, cv::noArray(),cv::Point(-1,-1),1);

    imshow("morph", bw);

    vector<vector<Point>> countours;
    findContours(bw.clone(), countours, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

    vector<Point> approx;
    Mat dst = src.clone();

    for(int i = 0; i < countours.size(); i++)
    {
        approxPolyDP(Mat(countours[i]), approx, arcLength(Mat(countours[i]), true) * 0.01, true);

        if (approx.size() >= 4 && (approx.size() <= 6))
        {
            int vtc = approx.size();
            vector<double> cos;
            for(int j = 2; j < vtc + 1; j++)
                cos.push_back(Angle(approx[j%vtc], approx[j-2], approx[j-1]));

            sort(cos.begin(), cos.end());

            double mincos = cos.front();
            double maxcos = cos.back();

            if (vtc == 4)// && mincos >= -0.5 && maxcos <= 0.5)
            {
                Rect r = boundingRect(countours[i]);
                double ratio = abs(1 - (double)r.width / r.height);

                line(dst, approx.at(0), approx.at(1), cvScalar(0,0,255),4);
                line(dst, approx.at(1), approx.at(2), cvScalar(0,0,255),4);
                line(dst, approx.at(2), approx.at(3), cvScalar(0,0,255),4);
                line(dst, approx.at(3), approx.at(0), cvScalar(0,0,255),4);
                SetLabel(dst, "RECT", countours[i]);
            }
        }
    }

    return dst;
 }
以下是我的输出:

取而代之的是17个长方形16个小长方形和1个大长方形,我只得到了12个长方形。 我是opencv新手,可能我给Canny函数和morphologyEx传递了错误的参数。。。 所以,我的问题是: 我怎么了?
如何修复它?

您可以使用通常的腐蚀扩张技巧。在Matlab中,我通常执行以下操作来创建遮罩。这会使管线变大并流动在一起

%Create Structuring element
sel = strel('disk', 1);
% bw is the black and white (binary image) created using e.g. Otsu thresholding
bw2 = imcomplement(bw);
bw3 = imerode(bw2, sel);
bw4 = imdilate(bw3, sel);
tightmask = imcomplement(bw4);

我经常使用它来查找图像中的独立组件

尝试使用精明的参数;第一个阈值,特别是第二个阈值。比如Cannysrc,bw,50,100,3,真的;可能会把你带到那里,这并不复杂。我检查了那个解决方案,但它不起作用。