import cv2
import numpy as np
imSeq = cv2.VideoCapture(r'D:\software\scripts\NikScr\img\hbg70_v001.%04d.jpg')
assert imSeq.isOpened()
cv2.namedWindow("Sequences", cv2.WINDOW_NORMAL)
# param for corner detection
feature_params = dict( maxCorners=100,
qualityLevel=0.3,
minDistance=7,
blockSize=7)
#Paramters 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 random Colors
color = np.random.randint(0, 255, (100, 3))
#Take first Frame and Find Corners
ret, old_frame = imSeq.read()
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None,
**feature_params)
#Create a mask Image for Drawing Purpose
mask = np.zeros_like(old_frame)
while True:
ret, frame = imSeq.read()
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
good_new = p1[st == 1]
good_old = p0[st == 1]
#Draw 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),
color[i].tolist(), 2)
frame = cv2.circle(frame, (a, b), 5,
color[i].tolist(), 1)
img = cv2.add(frame, mask)
cv2.imshow("Sequences", img)
k = cv2.waitKey(25)
if k == 27:
break
# Updating Previous frame and Points
old_gray = frame_gray.copy()
p0 = good_new.reshape(-1, 1, 2)
imSeq.release()
cv2.destroyAllWindows()
I’m trying to run this script for a image sequence but I’m getiing a error saying
Traceback (most recent call last):
File "D:\software\scripts\NikScr\opticalFlowTracking.py", line 50, in <module>
mask = cv2.line(mask, (a, b), (c, d),
cv2.error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'line'
> Overload resolution failed:
> - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
> - Can't parse 'pt1'. Sequence item with index 0 has a wrong type
I have copied the exact code from the web page
Python OpenCV: Optical Flow with Lucas-Kanade method - GeeksforGeeks
please can you let me know where I’m wrong.
Can anyone please suggest a python code for camera matrix, with just the 2d image sequence for monocular camera in windows?