Untitled
To use ORB (Oriented FAST and Rotated BRIEF) for detecting interesting points and track them using optical flow with OpenCV in Python, follow this example code. This code uses the `cv2.ORB_create()` to detect keypoints in the first frame and then tracks these points using the Lucas-Kanade method for optical flow (`cv2.calcOpticalFlowPyrLK`). First, make sure you have OpenCV installed in your environment. You can install it via pip if you haven't already: ```sh pip install opencv-python opencv-python-headless ``` Here's the code: ```python import cv2 import numpy as np # Initialize the video capture object cap = cv2.VideoCapture(0) # Use 0 for webcam # Parameters for ShiTomasi corner detection feature_params = dict(maxCorners=100, qualityLevel=0.3, minDistance=7, blockSize=7) # Parameters for Lucas-Kanade optical flow lk_params = dict(winSize=(15, 15), maxLevel=2, criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) # Create ORB detector orb = cv2.ORB_create() # Take first frame and find corners in it ret, old_frame = cap.read() if not ret: print("Failed to grab frame") exit() # Convert to grayscale old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) # Detect ORB keypoints kp = orb.detect(old_gray, None) # Convert keypoints to Point2f for calcOpticalFlowPyrLK p0 = cv2.KeyPoint_convert(kp) # Create a mask image for drawing purposes mask = np.zeros_like(old_frame) while True: ret, frame = cap.read() if not ret: break frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Calculate optical flow p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) # Select good points if p1 is not None: good_new = p1[st == 1] good_old = p0[st == 1] # Draw the tracks for i, (new, old) in enumerate(zip(good_new, good_old)): a, b = new.ravel() c, d = old.ravel() mask = cv2.line(mask, (a, b), (c, d), (0, 255, 0), 2) frame = cv2.circle(frame, (a, b), 5, (0, 0, 255), -1) img = cv2.add(frame, mask) cv2.imshow('frame', img) # Press 'q' to exit the loop if cv2.waitKey(30) & 0xFF == ord('q'): break # Update the previous frame and previous points old_gray = frame_gray.copy() p0 = good_new.reshape(-1, 1, 2) cap.release() cv2.destroyAllWindows() ``` This code captures video from your default camera, detects keypoints using ORB in the first frame, and then tracks these points using the Lucas-Kanade method for optical flow. The points are drawn on the video frames, which are displayed in real-time. Press 'q' to exit the loop and close the window.
Leave a Comment