如何使用 OpenCV 检测纸上的配准标记? [英] How can I detect registration markers on paper using OpenCV?

查看:15
本文介绍了如何使用 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.

推荐答案

你的图片好像只有4个黑色方块所以你要做的是:

It seems that on your image there are only 4 black squares so what you have to do is:

  1. 将图像转换为灰色
  2. 做门槛
  3. 查找黑色轮廓(在 OpenCV 中执行此操作之前,您必须反转图像,因为默认情况下 OpenCV 会查找白色轮廓)
  4. 循环遍历这些轮廓并找到边界矩形.
  5. 检查:

A) 矩形的面积大于某个常数(在我的解决方案中是 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)

代码:

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();

结果:

另请阅读 此 SO 讨论 - 你不需要那 4 个方格来检测纸张.

Also read this SO discussion - you don't need that 4 squares to detect paper.

这篇关于如何使用 OpenCV 检测纸上的配准标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆