Object tracking with Mean-shift – OpenCV 3.4 with python 3 Tutorial 29
Ethereum
0
Source code:
import cv2 import numpy as np video = cv2.VideoCapture("mouthwash.avi") _, first_frame = video.read() x = 300 y = 305 width = 100 height = 115 roi = first_frame[y: y + height, x: x + width] hsv_roi = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180]) roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) term_criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) while True: _, frame = video.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) mask = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) _, track_window = cv2.meanShift(mask, (x, y, width, height), term_criteria) x, y, w, h = track_window cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.imshow("Mask", mask) cv2.imshow("Frame", frame) key = cv2.waitKey(60) if key == 27: break video.release() cv2.destroyAllWindows()
Files:
Related Posts
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Recent Posts
- Detecting colors (Hsv Color Space) – Opencv with Python
- Write using your Eyes – Gaze controlled keyboard with Python and Opencv p.10
- Play sounds – Gaze controlled keyboard with Python and Opencv p.9
- Press a key by blinking eyes – Gaze controlled keyboard with Python and Opencv p.8
- Virtual Keyboard 3 – Gaze controlled keyboard with Python and Opencv p.7