问题
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
After applying find contour (External) I have seperated the outer 21 boxes
https://photos.app.goo.gl/12DGPy3iAYgPUMZ39
After seperating the boxes from image I have
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.
回答1:
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
. To separate empty boxes from filled box, use the function 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. 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
回答2:
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.
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.
回答3:
I would suggest you use the erode()
function to disconnect any number touching the outer boundary. You can then run findcontours()
using RETR_TREE
flag to get the child contours. 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.
来源:https://stackoverflow.com/questions/53708797/how-to-identify-empty-rectangle-using-opencv