【发布时间】:2017-05-24 14:31:03
【问题描述】:
我刚开始在 c++ 中使用 openCV,我需要做一些矩形边缘检测。
使用这段代码我会说它大约 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