Use Python, OpenCV’s Meanshift and Camshift algorithms to find and track objects in videos

This blog will show you how to use Meanshift and Camshift algorithms to find and track objects in videos.

* * MeanShift: all moved

Camshift (Continuously Adaptive Meanshift) Continuously Adaptive Meanshift **

  • Cv2.meanshift (): meanShift averaging always finds a window with the largest pixel distribution and tracks objects;

  • Cv2.camshift (): CamShift is an optimization of Meanshift, which continuously automatically resizes Windows and calculates the direction of the best fit ellipse. It again applies the mean transform with the newly scaled search window and the position of the previous window until the desired accuracy is achieved;

1. The rendering

The official example — Meanshift equalizes as follows:

Official example — CAMshift continuous adaptive equalization:

As you can see, Camshift automatically adjusts the size and rotation of the box to better fit the tracking object;

2. The source code

2.1 MeanShift

Continuously Adaptive MeanShift (Continuously Adaptive MeanShift) is used to find and track the target

# CAMshift is the MeanShift optimization that continuously automatically resizes the window and calculates the direction of the best fit ellipse. It again applies the mean transform with the newly scaled search window and the position of the previous window until the desired accuracy is achieved;
import numpy as np
import cv2

cap = cv2.VideoCapture('images/slow_traffic_small.mp4')

Get the first frame of the video
ret, frame = cap.read()

Set the initial window position
x, y, w, h = 300.200.100.50  # Hardcode the location
track_window = (x, y, w, h)

# Set ROI on tracked objects
roi = frame[y:y + h, x:x + w]

# Consider only the tone of HSV
hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
To avoid false values due to low light, use the cv2.inrange () function to discard low light values.
mask = cv2.inRange(hsv_roi, np.array((0..60..32.)), np.array((180..255..255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0.180])
cv2.normalize(roi_hist, roi_hist, 0.255, cv2.NORM_MINMAX)

# Set termination criteria, 10 iterations or move at least 1pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10.1)

while (1):
    ret, frame = cap.read()

    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0.180].1)

        # Apply meanShift to get a new position
        ret, track_window = cv2.meanShift(dst, track_window, term_crit)

        # Draw it on the image
        x, y, w, h = track_window
        img2 = cv2.rectangle(frame, (x, y), (x + w, y + h), 255.2)
        cv2.imshow('img2', img2)

        k = cv2.waitKey(60) & 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k) + ".jpg", img2)

    else:
        break

cv2.destroyAllWindows()
cap.release()
Copy the code

2.2 Camshift (Continuously Adaptive Meanshift)

Continuously Adaptive MeanShift (Continuously Adaptive MeanShift) is used to find and track the target
# CAMshift is the MeanShift optimization that continuously automatically resizes the window and calculates the direction of the best fit ellipse. It again applies the mean transform with the newly scaled search window and the position of the previous window until the desired accuracy is achieved;

import numpy as np
import cv2

cap = cv2.VideoCapture('images/slow_traffic_small.mp4')

Get the first frame of the video
ret, frame = cap.read()

Set the initial window position
x, y, w, h = 300.200.100.50  # Hardcode the location
track_window = (x, y, w, h)

# Set ROI on tracked objects
roi = frame[y:y + h, x:x + w]

hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0..60..32.)), np.array((180..255..255.)))
roi_hist = cv2.calcHist([hsv_roi], [0], mask, [180], [0.180])
cv2.normalize(roi_hist, roi_hist, 0.255, cv2.NORM_MINMAX)

# Set termination conditions, iterate 10 times or move at least 1pt
term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10.1)

while (1):
    ret, frame = cap.read()

    if ret == True:
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
        dst = cv2.calcBackProject([hsv], [0], roi_hist, [0.180].1)

        # Apply CamShift to get the new position
        # Return a rotated rectangle and box argument (to be passed as a search window in the next iteration)
        # It first applies a mean transform. Once the MeanShift converges, it updates the window size and calculates the direction of the best fit ellipse. It again applies the mean transform with the newly scaled search window and the position of the previous window. The process continues until the required precision is met.
        ret, track_window = cv2.CamShift(dst, track_window, term_crit)

        # Draw on the image
        pts = cv2.boxPoints(ret)
        pts = np.int0(pts)
        img2 = cv2.polylines(frame, [pts], True.255.2)
        cv2.imshow('img2', img2)
        # cv2.waitKey(0)

        k = cv2.waitKey(60) & 0xff
        if k == 27:
            break
        else:
            cv2.imwrite(chr(k) + ".jpg", img2)

    else:
        break

cv2.destroyAllWindows()
cap.release()
Copy the code

Reference 3.

  • Docs.opencv.org/3.0-beta/do…
  • Github.com/opencv/open…
  • Interactive Camshift