【问题标题】:Straight lines detection in contour with openCV使用openCV在轮廓中检测直线
【发布时间】:2017-05-24 14:31:03
【问题描述】:

我刚开始在 c++ 中使用 openCV,我需要做一些矩形边缘检测。

这是我用于测试的图像:

这是我使用阈值得到的结果 (THRESH_BINARY):

使用这段代码我会说它大约 90% 是正确的:

void find_contour(Mat src)
{
    int largest_area=0;
    int largest_contour_index=0;
    Rect bounding_rect;

    Mat thr;
    cvtColor( src, thr, COLOR_BGR2GRAY ); //Convert to gray
    threshold( thr, thr, 175 , 255, THRESH_BINARY ); //Threshold the gray

    imshow("Binary", thr);

    vector<vector<Point> > contours; // Vector for storing contours

    findContours( thr, contours, RETR_CCOMP, CHAIN_APPROX_SIMPLE ); // Find the contours in the image

    for( size_t i = 0; i< contours.size(); i++ ) // iterate through each contour.
    {
        double area = contourArea( contours[i] );  //  Find the area of contour

        if( area > largest_area )
        {
            largest_area = area;
            largest_contour_index = i;               //Store the index of largest contour
            bounding_rect = boundingRect( contours[i] ); // Find the bounding rectangle for biggest contour
        }
    }

    drawContours( src, contours, largest_contour_index, Scalar( 0, 255, 0 ), 2 ); // Draw the largest contour using previously stored index.

    imshow( "result", src );
}

我想消除右侧的“错误”,然后沿每条边绘制折线并找到它们的交点(卡片角),以便进行透视变换。

最好的方法是什么?

我愿意接受任何建议。

感谢您的宝贵时间!

【问题讨论】:

  • 您是否需要扩展您的技术以支持其他图像?因为显然有些背景不能很好地处理阈值。
  • 一般卡片是白色的,背景是一张桌子(浅棕色)。我尝试了各种代码 sn-ps 和阈值处理让我得到了最好的结果。
  • 很公平。我最近一直在使用颜色模型,有一些简单的模型在这里可能有用,但如果阈值足以满足您的需求,我不会费心谈论它们。
  • 如果你能指出一些资源,我愿意学习;)
  • 一次一个问题。无论如何,我的第一个想法是使用霍夫变换,因为它们很好,简单,易于使用和理解。这有什么用吗? docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_lines/…

标签: c++ opencv edge-detection opencv-contour


【解决方案1】:

我会尝试如下:

  • 大约找到角点;例如,这可以通过在八个主要基本方向上寻找最远的轮廓像素,给出一个八边形(加上一些逻辑来选择正确的四个点)来完成;

  • 从拐角处可以得到边的近似值;

  • 在每个边缘(倾斜矩形)周围放置适当的 ROI,并将其拉直,使边缘接近水平;

  • 在 ROI 中使用 Hough 变换,最好仅在垂直梯度上使用。一点水平模糊也有帮助。

使用这些 ROI 的目的是减少错误检测的可能性并使检测方向敏感。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    • 2014-07-27
    • 1970-01-01
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多