Skip to content

Object Vision

Renato Dell'Osso edited this page Aug 24, 2023 · 1 revision

Object Vision

Using Masks

First, the image needs to be converted from the RGB color space to the HSV color space for more accurate detection. To do this we use cv2.cvtColor.

img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

****************************************************************To create a mask, make a variable for the mask. Use cv2.inRange to select only the pixels with an HSV value between two given HSV values.

H values range from 0-180. S values range from 0-255. V values range from 0-255.

****************************************************************mask = cv2.inRange(img, (0, 0, 0), (180, 255, 255))

To remove noise we apply a median blur, then erode and dilate the mask.

mask = cv2.medianBlur(mask, 25) kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, ksize=(15, 15)) mask = cv2.erode(mask, kernel) mask = cv2.dilate(mask, kernel)

To combine two masks use cv2.bitwise_and.

mask3 = cv2.bitwise_and(mask1, mask2)

Contours

Contours are basically the outline of a detected object.

****Create a contours variable, this has all contours found in your mask. Find the areas of the contours and use the largest. Find the X and Y of the center of the contours.

Code below shows how to do all of the above.

	cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

  cntsAreas = []

  for i in cnts[0]:
    M = cv2.moments(i)

    cntsAreas.append(M["m00"])

  if len(cntsAreas) > 0:
    maxValue = max(cntsAreas)

    maxCntsIndex = cntsAreas.index(maxValue)

    M = cv2.moments(cnts[0][maxCntsIndex])

    cX = int((M["m10"] / M["m00"]))
    cY = int((M["m01"] / M["m00"]))

cX is the X position of the center of the contour, cY is the Y position of the center of the contour.

You can also display contours on your image capture with:

img = cv2.drawContours(img, cnts[0], -1, (0, 255, 0), 3)

Clone this wiki locally