c++ - OpenCV C++ - 具有不规则边的矩形检测

标签 c++ opencv detection

enter image description here

您好.. 我遇到了矩形检测问题,它的边不规则(不直),如上图所示。实际上,使用方法 houghline 可以通过一些参数配置来检测矩形上的线。在计算相交并得到 4 个角后,我可以将它旋转到正常位置。

但是如果我把图像换成另一个矩形(不同大小但仍然有不规则的边),我需要重新配置参数。这是因为四边都没有检测到线,而且线可以超过4条。

除了houghline还有其他方法更简单(不需要重新配置/困难配置)吗?

最佳答案

这种方法是计算包含所有矩形像素的旋转矩形。

也许你可以将它与 vasanth 的答案结合起来,这样你就可以首先近似多项式以获得规则边界,然后使用 cv::minAreaRect

提取旋转的矩形

这是我的代码:

int main()
{
    cv::Mat input = cv::imread("../inputData/RotatedRect.png");

    // convert to grayscale (you could load as grayscale instead)
    cv::Mat gray;
    cv::cvtColor(input,gray, CV_BGR2GRAY);

    // compute mask (you could use a simple threshold if the image is always as good as the one you provided)
    cv::Mat mask;
    cv::threshold(gray, mask, 0, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);

    // find contours (if always so easy to segment as your image, you could just add the black/rect pixels to a vector)
    std::vector<std::vector<cv::Point>> contours;
    std::vector<cv::Vec4i> hierarchy;
    cv::findContours(mask,contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    /// Draw contours and find biggest contour (if there are other contours in the image, we assume the biggest one is the desired rect)
    // drawing here is only for demonstration!
    int biggestContourIdx = -1;
    float biggestContourArea = 0;
    cv::Mat drawing = cv::Mat::zeros( mask.size(), CV_8UC3 );
    for( int i = 0; i< contours.size(); i++ )
    {
        cv::Scalar color = cv::Scalar(0, 100, 0);
        drawContours( drawing, contours, i, color, 1, 8, hierarchy, 0, cv::Point() );

        float ctArea= cv::contourArea(contours[i]);
        if(ctArea > biggestContourArea)
        {
            biggestContourArea = ctArea;
            biggestContourIdx = i;
        }
    }

    // if no contour found
    if(biggestContourIdx < 0)
    {
        std::cout << "no contour found" << std::endl;
        return 1;
    }

    // compute the rotated bounding rect of the biggest contour! (this is the part that does what you want/need)
    cv::RotatedRect boundingBox = cv::minAreaRect(contours[biggestContourIdx]);
    // one thing to remark: this will compute the OUTER boundary box, so maybe you have to erode/dilate if you want something between the ragged lines



    // draw the rotated rect
    cv::Point2f corners[4];
    boundingBox.points(corners);
    cv::line(drawing, corners[0], corners[1], cv::Scalar(255,255,255));
    cv::line(drawing, corners[1], corners[2], cv::Scalar(255,255,255));
    cv::line(drawing, corners[2], corners[3], cv::Scalar(255,255,255));
    cv::line(drawing, corners[3], corners[0], cv::Scalar(255,255,255));

    // display
    cv::imshow("input", input);
    cv::imshow("drawing", drawing);
    cv::waitKey(0);

    cv::imwrite("rotatedRect.png",drawing);

    return 0;
}

给出这个结果:

enter image description here

关于c++ - OpenCV C++ - 具有不规则边的矩形检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583649/

相关文章:

c++ - 减小可执行 visual studio 2017 社区的大小

c++ - OpenFrameworks - 减去 GL 窗口

python - 如何用python获取特定颜色(RGB)的像素坐标[x,y]列表?

python - 属性错误 : module 'cv2.cv2' has no attribute 'CascadeClassifer' (OpenCV Face Detection)

iphone - Objective-C : is it possible to detect QR code data type?

c++ - 当我从公共(public)成员函数返回引用时,为什么我可以暴露私有(private)成员?

c++ - 在 C++ 中返回 Vector 等同于 C#

python - Opencv,如何覆盖图像?

c++ - 来自无符号短数组的 OpenCV Mat

c - 带视频的opencv圆检测