Optical Flow with Lucas-Kanade method – OpenCV 3.4 with python 3 Tutorial 31
Source code:
[python]
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
# Create old frame
_, frame = cap.read()
old_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Lucas kanade params
lk_params = dict(winSize = (15, 15),
maxLevel = 4,
criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03))
# Mouse function
def select_point(event, x, y, flags, params):
global point, point_selected, old_points
if event == cv2.EVENT_LBUTTONDOWN:
point = (x, y)
point_selected = True
old_points = np.array([[x, y]], dtype=np.float32)
cv2.namedWindow("Frame")
cv2.setMouseCallback("Frame", select_point)
point_selected = False
point = ()
old_points = np.array([[]])
while True:
_, frame = cap.read()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if point_selected is True:
cv2.circle(frame, point, 5, (0, 0, 255), 2)
new_points, status, error = cv2.calcOpticalFlowPyrLK(old_gray, gray_frame, old_points, None, **lk_params)
old_gray = gray_frame.copy()
old_points = new_points
x, y = new_points.ravel()
cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
[/python]
8 Comments
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Opencv Beginner Tutorial
- 1) Loading images
- 2) Loading Video and Webcam
- 3) Drawing and writing on images
- 4) Basic operations on images
- 5) Add images and Threshold
- 6) Blending images
- 7) Bitwise Operators
- 8) Trackbars
- 9) Object detection using HSV Color space
- 10) Basic Thresholding
- 11) Histograms
- 12) Basic geometric transformations
- 13) Perspective transformation
- 14) Affine transformation
- 15) Adaptive thresholding5
- 16) Smoothing images
- 17) Morphological transformation
- 18) Edge detection
- 19) Find and Draw Contours
- 20) Template matching
- 21) Lines detection with Hough Transform
- 22) Corners detection
- 23) Image Pyramids
- 24) Image Pyramids (Blending and reconstruction)
- 25) Feature detection (SIFT, SURF, ORB)
- 26) Feature Matching (Brute-Force)
- 27) Mouse Events
- 28) Histogram and Back Projection
- 29) Object tracking with Mean-shift
- 30) Object tracking with Camshift
- 31) Optical Flow with Lucas-Kanade method
- 32) Background Subtraction
- 33) k-Nearest Neighbour classification
- 34) Object tracking using Homography
- 35) Fourier Transform
- 36) Knn handwritten digits recognition
- 37) Face detection using Haar Cascades
Most Read:
-
Train YOLO to detect a custom object (online with free GPU)
-
YOLO object detection using Opencv with Python
-
Detecting colors (Hsv Color Space) – Opencv with Python
-
How to install Python 3 and Opencv 4 on Windows
-
How to install Dlib for Python 3 on Windows
-
Check if two images are equal with Opencv and Python
-
Feature detection (SIFT, SURF, ORB) – OpenCV 3.4 with python 3 Tutorial 25
-
Eye motion tracking – Opencv with Python
Hi there. Could help with an video analysis. I want to calculate velocity of diferent joints during an kick to a ball. Is it possible to change this codes and choose multiple points?
Yes it’s possible to choose multiple points. Actually the code is done for that, I just choosed one point to make it easier for people to understand it.
You should first track the points using corners detection and then apply the optical flow method for all the points.
Hi
I was studying Radon transform from some days.
Was wondering if you had any insight how can it be in any ways used to detect optical flow of sequence of images.
can we use the mouse callback function to track several points that I am interested in at the same time, except for using corners detection function
Thanks for sharing the projects, I was wondering this approach can be used to find a movement of a downside camera whereas it is like an optic mouse can detect movement & direction
Thanks
Thank you so much.
You teached me alot
This method of tracking is great!. Seems more reliable for tracking moving objects! This might be along shot but do you think you are able to do this using tkinter or some other gui other than using inshow as a form. The problem using imshow is that the video and the whole program pauses when you move the form not so good when tracking objects.
Hello…
Could you tell me how to find the outermost coordinates?
That would be the lower left-hand corner and the upper right-hand corner.
Best regards.