Tracking feature using optical flow

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?

do you understand the error message? do you want help with that?

same as Hand Tracking Project

I copied the code exactly as it was from the page on the page it was running and when i tried to run the program it gave me the error. that is why i posted the query. asking where i may be wrong? i googled the error and understood that some input was wrong but if the program is running on another system then what could go wrong? and the reply I get is jus so encouraging and just clears all my doubt about. Thanks alot @crackwitz

calcOpticalFlowPyrLK() works with float points, and opencv’s drawing expects integer ones, so you need to convert all 4 a,b,c,d values to int for the drawing coords

Hey I’m referring to this video to this guy has also explained the working of the code. It is the same code as you said calcOpticalFlowPyrLK is used and the 4 values also are the same there is no conversion of int mentioned? does this have to anything with opencv version? My version is 4.5.2 ?

it’s somewhat based on opencv/lk_track.py at master · opencv/opencv · GitHub

funny: last touched in 2019, and it fails with the same issue but on the circle call on my build of 4.5.2

perhaps it’s time to extend the drawing calls to taking floats