opencv - opencv正方形检测辐射地板概率

标签 opencv image-processing geometry shape edge-detection

致力于正方形检测。问题出在辐射地板上。如你所见。
有解决这个问题的主意吗?

谢谢。

源图像:

输出:

源代码:

void EdgeDetection::find_squares(const cv::Mat& image, vector >& squares,cv::Mat& outputFrame) {

unsigned long imageSize = (long) (image.rows * image.cols) / 1000;
if  (imageSize > 1200) RESIZE = 9;
else if  (imageSize > 600) RESIZE = 5;
else if  (imageSize > 300) RESIZE = 3;
else RESIZE = 1;


Mat src(Size(image.cols / RESIZE, image.rows / RESIZE),CV_YUV420sp2BGR);
// Resize src to img size
resize(image, src, src.size() ,0.5, 0.5, INTER_LINEAR);

Mat imgeorj=image; const int N = 10;//11; Mat pyr, timg, gray0(src.size(), CV_8U), gray;

// down-scale and upscale the image to filter out the noise
pyrDown(src, pyr, Size(src.cols / 2, src.rows / 2));
pyrUp(pyr, timg, src.size());

#ifdef blured

Mat blurred(src);
medianBlur(src, blurred, 15);

#endif

vector<vector<Point> > contours;

// find squares in every color plane of the image
for ( int c = 0; c < 3; ++c) {
    int ch[] = {c, 0};
    mixChannels(&timg, 1, &gray0, 1, ch, 1);

    // try several threshold levels
    for ( int l = 0; l < N; ++l) {
        // hack: use Canny instead of zero threshold level.
        // Canny helps to catch squares with gradient shading
        if (l == 0) {
            // apply Canny. Take the upper threshold from slider
            // and set the lower to 0 (which forces edges merging)
            // Canny(gray0, gray, 0, thresh, 5);
            // Canny(gray0, gray, (10+l), (10+l)*3, 3);
            Canny(gray0, gray,50, 200, 3 );
            // dilate canny output to remove potential
            // holes between edge segments
            dilate(gray, gray, Mat(), Point(-1, -1));
            //erode(gray, gray, Mat(), Point(-1, -1), 1);
        } else {
            // apply threshold if l!=0:
            // tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
            gray = gray0 >= (l + 1) * 255 / N;
        }


        // find contours and store them all as a list
        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        vector<Point> approx;

        // test each contour
        for (size_t i = 0; i < contours.size(); ++i) {
            // approximate contour with accuracy proportional
            // to the contour perimeter

            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true);


            if (approx.size() == 4 &&
                fabs(contourArea(Mat(approx))) > 5000 &&
                isContourConvex(Mat(approx))) {
                float maxCosine = 0;

                for (register int j = 2; j < 5; ++j) {
                    // find the maximum cosine of the angle between joint edges
                    float cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                    maxCosine = MAX(maxCosine, cosine);
                }

                // if cosines of all angles are small
                // (all angles are ~90 degree) then write quandrange
                // vertices to resultant sequence
                if (maxCosine < 0.3) {
                    squares.push_back(approx);
                }

            }
        }

    }
}

debugSquares(squares, imgeorj,outputFrame);
}

最佳答案

您可以尝试使用Hough变换检测直线边缘,然后将其用于构建正方形。

关于opencv - opencv正方形检测辐射地板概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19977952/

相关文章:

java - Android opencv加载16位深度的灰度png

python - 人脸检测抛出错误:函数 cv::CascadeClassifier::detectMultiScale 中的 !empty()

image-processing - 如何通过脚本合并DM中的2个RGB图像?

algorithm - 如何快速判断一个点是否在二维凸多边形内 N 次

javascript - 从 x、y、宽度、高度和旋转中获取旋转的矩形点

C++,opencv : Is it safe to use the same Mat for both source and destination images in filtering operation?

c++ - 如何在OpenCV中将平行四边形图像转换为矩形图像?

c++ - 不裁剪图像旋转

通用图像处理库的 C# 设计指南

javascript - 如何在 anchor 标签点击显示的现有圆上制作同心圆