I want to recognize the tray is empty or not in the given image using OpenCV in python.
below is what I have tried
- detect the biggest rectangle and cropped by using the below code
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thresh = cv2.erode(thresh, kernel, iterations=4)
thresh = cv2.dilate(thresh, kernel, iterations=4)
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find the biggest contour
max_area = -1
max_c = 0
for i in range(len(cnts)):
contour = cnts[i]
area = cv2.contourArea(contour)
if (area > max_area):
max_area = area
max_c = i
contour = cnts[max_c]
areas = [cv2.contourArea(c) for c in cnts]
max_index = np.argmax(areas)
cnt=cnts[max_index]
x,y,w,h = cv2.boundingRect(cnt)
crop_img = img[y:y h, x:x w]
- and then trying to find black dots by getting contours after applying the canny edge detection and applying mask and noise removal methods
#Create empty mask and flood fill
mask = np.zeros(edges.shape)
#Create 3-channel alpha mask
mask_stack = np.dstack([mask]*3)
- and again threshold the image and find contours.
It works for medium and large objects but when I put the small objects like coins, this method is not working because the tray has some scratches and dust too. This is my first project using OpenCV in python
please help to find the solution to achieve this requirement.
Input:
Output:
Empty Tray:
CodePudding user response:
I recommend you to: