Based on OpenCV combat: object tracking

introduce

The basic idea of ​​tracking an object is to find the outline of the object, based on HSV color values.

Outline: Highlight the image segment of the object. For example, if you apply a binary threshold to an image with (180, 255), pixels larger than 180 will be highlighted in white, while others will be black. The white part is called the outline.

Before proceeding below, please install OpenCV in your system. Open a command prompt and type

pip install opencv-python
copy

Step 1: Read data from camera

import cv2
cam = cv2.VideoCapture(0) 
img = cam.read()[1] #_, img = cam.read()
copy

Parameter 0 - for the main camera, e.g. in a laptop webcam is considered the main camera. 1- stands for secondary school and so on.

Step 2: Preprocessing the frame

1. Normalize the image using a Gaussian filter. A normalized image may lose a lot of small information, but we need to normalize/blur the image so that our objects get an even color distribution.

#cv2.gaussianBlur(source_image,Kernal_size,Border_width)

Blur_img = cv2.GaussianBlur(img,(11,11),0)
copy

2. Convert the image to the HSV color model.

HSV = cv2.cvtColor(Blur_img, cv2.COLOR_BGR2HSV)
copy

Step 3: Find the HSV color of the corresponding object

Obj_low = np.array([0,0,0]) # In my case (H,S,V)
Obj_high = np.array([179,157,79])
copy

Step 4: Thresholding

Applies a binary threshold, black and white Obj_low and Obj_high, within the given range of HSV values.

MASK = cv2.inRange(HSV, Obj_low, Obj_high)
copy

#MASK2 = cv2.inRange(HSV,Obj2_low,Obj2_high)

If you try to track two different objects, you need to create 2 different masks and end up using the "bitwise AND" operator on the two masks.

#mask = cv2.bitwise_and(mask1,mask2)

Erosion and Dilation: Erosion and Dilation fill in black and white blobs in a thresholded image. This makes the image sharper, smoother and highlights the main subject.

MASK = cv2.erode(MASK1, None, iterations=2)
MASK = cv2.dilate(MASK1, None, iterations =2)
copy

Step 5: Find Contours in the Image

Outline: Highlight the image segment of the object. For example, if you apply a binary threshold to an image with (180, 255), pixels larger than 180 will be highlighted in white, while others will be black. The white part is called the outline.

cnts = cv2.findContours(MASK1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
if len(cnts)>0 :
 c = max(cnts, key = cv2.contourArea)
copy

In the image given above, the entire white border area is the outline. There may be more than one outline, but the main object will have the largest area. So choose the largest contour. Then..

Step 6: Draw the circle on the object

After getting the outline of the main object, draw a circle on the outline.

((x,y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M['m10']/ M['m00']), int(M['m01']/ M['m00']) )
cv2.circle(img, center, 5, (0,0,255), -1)
cv2.circle(img, center, int(radius), (0,0,255), 2)
copy

object tracker code

import cv2
import numpy as np
cam = cv2.VideoCapture(0)
Obj_low = np.array([0,0,0])
Obj_high = np.array([179,157,79])
while True:
    img = cam.read()[1]
    img = cv2.resize(img, (800,600) )
    blur_img = cv2.GaussianBlur(img,(21,21),0)
    HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
MASK1 = cv2.inRange(HSV, Obj_low, Obj_high)
    MASK1 = cv2.erode(MASK1, None, iterations=2)
    MASK1 = cv2.dilate(MASK1, None, iterations =2)
    cnts = cv2.findContours(MASK1.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    center = None
    if len(cnts)>0 :
        c = max(cnts, key = cv2.contourArea)
        ((x,y), radius) = cv2.minEnclosingCircle(c)
        M = cv2.moments(c)
        center = (int(M['m10']/ M['m00']), int(M['m01']/ M['m00']) )
        if radius>10:
            cv2.circle(img, center, 5, (0,0,255), -1)
            cv2.circle(img, center, int(radius), (0,0,255), 2)
    cv2.imshow("my window",img)
    k = cv2.waitKey(1)
    if k==27:
        break
cam.release()
cv2.destroyAllWindows()
copy

Posted by Supernovas on Sun, 22 May 2022 11:24:03 +0300