如何使用OpenCV在纸上检测注册标记? [英] How can I detect registration markers on paper using OpenCV?
问题描述
我一直在寻找最好的方法来检测纸上的4个黑色方块,并用它们在自己的图像中分离纸张。
I've been trying to find the best way to detect the 4 black squares on the paper and use them to isolate the paper in its own image.
< img src =https://i.stack.imgur.com/6nA73.jpgalt =enter image description here>
推荐答案
似乎在您的图片上只有4个黑色方块,所以您必须做的是:
It seems that on your image there are only 4 black squares so what you have to do is:
- 将图片转换为灰色
- 执行阈值
- 查找黑色轮廓(在OpenCV中执行此操作之前,必须反转图像,因为默认情况下,OpenCV会找到白色轮廓) li>
- 循环浏览这些轮廓并找到边界矩形。
-
执行检查:
- Convert image to grays
- Do threshold
- Find black contours (before doing this in OpenCV you have to invert your image, because by default OpenCV finds white contours)
- Cycle through these contours and find bounding rectangle.
Do the check:
A)Rectangle的面积大于一些常数(在我的解决方案中 100 )
A) Rectangle's area is bigger that some constant (in my solution it was 100)
B)矩形的宽度/高度接近1.0在我的心灵中是 [0.9,1.1] 范围)
B) Rectangle's width/height is near 1.0 (in my soultion it was [0.9, 1.1] range)
/ p>
The code:
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();
结果:
另请参阅这个讨论 - 你不需要4个方块来检测纸。
Also read this SO discussion - you don't need that 4 squares to detect paper.
这篇关于如何使用OpenCV在纸上检测注册标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!