如何使用 OpenCV 识别空矩形 [英] How to identify empty rectangle using OpenCV

查看:51
本文介绍了如何使用 OpenCV 识别空矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题说明:

我必须确定图像中的哪个框是空的,图像中的哪个框是填充的,(此处数字和颜色表示该框已填充)https://photos.app.goo.gl/FpaShWVL1RV7z1Gt8

I have to identify which box in the image is empty and which box in image is filled, ( here number and color signify that the box is filled) https://photos.app.goo.gl/FpaShWVL1RV7z1Gt8

应用查找轮廓后(外部)我已经把外面的21个盒子分开了

After applying find contour (External) I have seperated the outer 21 boxes

https://photos.app.goo.gl/12DGPy3iAYgPUMZ39

从图像中分离框后我有

https://photos.app.goo.gl/NQVmA5pAWufSReVD8

现在的问题是如何识别哪个盒子是空的,哪个盒子是填充的.

Now the question is how to identify which box is empty and which box is filled.

提前致谢.

推荐答案

如果您在问题中分享您的代码将会非常有帮助,因为我们可以修改它来回答您的问题.

It will be extremely helpful if you share your code in your question as we can modify it to answer your question.

无论如何,根据我从您的问题中获得的信息量,我看到您使用 findContour 完成了大部分工作.要将空框与填充框分开,请使用函数 countNonZero.

Anyway, with the amount of information i get from your question, i see that you have done most of the work using findContour. To separate empty boxes from filled box, use the function countNonZero.

您可以将每个矩形提供给函数,它会返回输入中的非零像素总数.由于白色区域被认为是空的,较高的分数将对应于空矩形.您可以通过将 countZero 结果除以框区域来对结果进行归一化,以获得 0~1 的结果.这将使阈值削减的决定更简单.

You can feed each of the rectangle to the function and it will return the total non-zero pixels in the input. Since white area is considered empty, a higher score will correspond to an empty rectangle. You can normalize the result by dividing the countZero result with the box area to obtain result from 0~1. This will make decision of a threshold cut simpler.

这是一个示例代码:

x,y,w,h = contour_box[i]
total_white = cv2.countNonZero(img_Src[y:y+h,x:x+w])
ratio = total_white / float(w*h)

# if the white pixel count is 80% of box size, box is empty
if ratio > 0.8 :
     box_is_empty = True

这篇关于如何使用 OpenCV 识别空矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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