Home > database >  Need to identify the "tray is empty or not" using opencv
Need to identify the "tray is empty or not" using opencv

Time:02-26

I want to recognize the tray is empty or not in the given image using OpenCV in python.

below is what I have tried

  1. 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]
  1. 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)
  1. 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: input - tray with coin and nut washer

Output: output - tray with coin and nut washer

Empty Tray: Empty Tray

CodePudding user response:

I recommend you to:

  1. do res

    Note that this solution is subject to different parameters, to better control them and get stable results I advise you to consider the recommendations above.

  • Related