use*_*185 7 opencv image-processing computer-vision
我一直在努力找到检测纸张上4个黑色方块的最佳方法,并用它们将纸张隔离在自己的图像中.

在您的图像上似乎只有4个黑色方块,所以您需要做的是:
做检查:
A)矩形区域较大,有些常数(在我的解决方案中为100)
B)矩形的宽度/高度接近1.0(在我的灵魂中它是[ 0.9,1.1 ]范围)
代码:
Mat img = imread("test.jpg"), gray;
vector<Vec4i> hierarchy;
vector<vector<Point2i> > contours;
cvtColor(img, gray, CV_BGR2GRAY);
threshold(gray, gray, 100, 255, THRESH_BINARY);
bitwise_not(gray, gray);
findContours(gray, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
for(size_t i=0; i<contours.size(); i++)
{
Rect rect = boundingRect(contours[i]);
double k = (rect.height+0.0)/rect.width;
if (0.9<k && k<1.1 && rect.area()>100)
{
drawContours(img, contours, i, Scalar(0,0,255));
}
}
imshow("result", img);
waitKey();
Run Code Online (Sandbox Code Playgroud)
结果:

另请阅读此SO讨论 - 您不需要4个方格来检测纸张.
| 归档时间: |
|
| 查看次数: |
3362 次 |
| 最近记录: |