简体   繁体   English

如何使用OpenCV识别空矩形

[英]How to identify empty rectangle using OpenCV

Problem statement: 问题陈述:

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 我必须确定图像中的哪个框为空以及图像中的哪个框已填充(此处数字和颜色表示该框已填充) https://photos.app.goo.gl/FpaShWVL1RV7z1Gt8

After applying find contour (External) I have seperated the outer 21 boxes 应用查找轮廓(外部)后,我将外部21个框分开

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

After seperating the boxes from image I have 从图像中分离出盒子之后

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

Now the question is how to identify which box is empty and which box is filled. 现在的问题是如何识别哪个盒子是空的,哪个盒子是填充的。

Thanks in advance. 提前致谢。

I would suggest you use the erode() function to disconnect any number touching the outer boundary. 我建议您使用erode()函数断开任何接触外边界的数字。 You can then run findcontours() using RETR_TREE flag to get the child contours. 然后,您可以使用RETR_TREE标志运行findcontours()以获得子轮廓。 You can eliminate the outer boundary since it will have the largest area. 您可以消除外边界,因为它将具有最大的面积。 If you have more contours then you know the box is filled. 如果轮廓更多,则说明该框已填充。

This link for morphological operations will help you with erosion. 形态操作的此链接将帮助您进行腐蚀。

If you want to know if a box is empty or not... you first need to define 'empty' in binary. 如果您想知道一个盒子是否为空...,您首先需要在二进制文件中定义“空”。 What I mean is : what does it mean to you, and how does it translate into computer language ? 我的意思是:这对您意味着什么,以及如何将其翻译成计算机语言? Basically, what I think is : you may want to define a range of RGB color (white for instance) and if say 99% of your pixels are in that range, the probability of the image being empty is pretty high. 基本上,我认为是:您可能想要定义RGB颜色的范围(例如白色),如果说99%的像素在该范围内,则图像为空的可能性非常高。

You could also take the average of the colors in your image, and set a threshold for the standard deviation, that, if passed - or not, would trigger an empty - or not, rectangle. 您还可以取图像中颜色的平均值,并设置标准偏差的阈值,如果该阈值通过(或不通过),将触发一个矩形(或不通过)。 The only limit is your imagination really, but what you do define in your program. 唯一的限制是您的想象力,但是您在程序中定义的是什么。

It will be extremely helpful if you share your code in your question as we can modify it to answer your question. 如果您在问题中共享您的代码,这将非常有用,因为我们可以对其进行修改以回答您的问题。

Anyway, with the amount of information i get from your question, i see that you have done most of the work using findContour . 无论如何,利用我从问题中获得的大量信息,我看到您已经使用findContour完成了大部分工作。 To separate empty boxes from filled box, use the function countNonZero . 要将空盒子与填充盒子分开,请使用countNonZero函数。

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. 您可以通过将countZero结果除以方框面积来归一化结果,以得到0〜1的结果。 This will make decision of a threshold cut simpler. 这将使阈值削减的决定更加简单。

Here is a sample code: 这是一个示例代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM