如何在 OpenCV 中找到最大矩形的坐标?

How to find coordinates of largest rectangle in OpenCV?

我有以下代码:

    findContours( src, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );

    Mat drawing = Mat::zeros( src.size(), CV_8UC3 );

    double largest_area = 0;

    for( int i = 0; i < contours.size(); i++) {  // get the largest contour
        area = fabs( contourArea( contours[i] ) );
        if( area >= largest_area ){
            largest_area = area;
            largest_contours.clear(); 
            largest_contours.push_back( contours[i] );
        }
    }

    if( largest_area >= 3000 ){   // draw the largest contour if exceeded minimum largest area 
        drawContours( drawing, largest_contours, -1, Scalar(0,0,255), 2 );
    }

... 生成以下输出图像:

我想得到四个点的坐标(绿色标记),可以吗?

你想在透视中寻找矩形的角点吗?

您可能想尝试几种解决方案:

  1. 使用HoughLines进行直线检测并找到它们的交点。
  2. 使用Generalized Hough Transform
  3. 使用Harris corner detector。但是你需要过滤多余的角点。

对于类似的任务,我使用了以下过程(在我的情况下效果很好): 使用递增的 epsilon 参数对输入轮廓执行 cv::approxPolyDP,直到 returns 4 条或更少的多段线。如果它 returns 4 条折线,您可能会得到 4 个角点,正是您所需要的。如果它 returns 少于 4 条多段线很可能是有问题的。