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

dnph8jn4  于 2023-01-28  发布在  其他
关注(0)|答案(4)|浏览(372)

嗨..我有矩形检测的问题,有不规则的一面(不直),如上图。实际上与方法houghline可以检测一些参数配置的矩形上的线。计算后相交,并得到4角,我可以旋转它到正常的位置。
但如果我用另一个矩形(不同大小,但仍然有不规则的边)来改变图像,我需要重新配置参数。这是因为在四个边上都检测不到线,除此之外,线可以超过4条。
除了houghline之外,是否还有其他更简单的方法(不需要重新配置/困难的配置)?

ux6nzvsh

ux6nzvsh1#

这种方法是计算包含所有矩形像素的旋转矩形。
也许你可以把它和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;
}

给出这个结果:

i86rm4rw

i86rm4rw2#

试试这个:
1.对映像运行findCountours
2.应用approxPolyDP将轮廓近似为矩形。轮廓的边将更加规则。
3.使用矩和/或几何分割矩形轮廓。

ar7v8xwq

ar7v8xwq3#

利用初等几何,你需要找到坐标

  • 具有最小x坐标的黑色像素位置
  • 具有最大x坐标的黑色像素位置
  • 具有最小y坐标的黑色像素位置
  • 具有最大y坐标的黑色像素位置

这4个点将是你的矩形的边缘。

t5zmwmid

t5zmwmid4#

Micka的回答很好但是根据
OpenCV findContours() just returning one external contour
要提取的轮廓应该是白色的,背景是黑色的。
把面具减去255:

mask = 255 - mask;

相关问题