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,

#Paramters for Lucas Kanade Optical flow
lk_params = dict( winSize=(15, 15),
                  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 =
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
p0 = cv2.goodFeaturesToTrack(old_gray, mask=None,

#Create a mask Image for Drawing Purpose
mask = np.zeros_like(old_frame)

while True:
    ret, frame =
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #calculate optical flow
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray,
                                           p0, None,

    #Select Good Points
    good_new = p1[st == 1]
    good_old = p0[st == 1]

    #Draw Tracks
    for i, (new, old) in enumerate(zip(good_new,
        a, b = new.ravel()
        c, d = old.ravel()
        mask = cv2.line(mask, (a, b), (c, d),
                        color[i].tolist(), 2)

        frame =, (a, b), 5,
                           color[i].tolist(), 1)

    img = cv2.add(frame, mask)
    cv2.imshow("Sequences", img)

    k = cv2.waitKey(25)
    if k == 27:

    # Updating Previous frame and Points
    old_gray = frame_gray.copy()
    p0 = good_new.reshape(-1, 1, 2)


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\", 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/ 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