I am trying to implement the Lucas-Kanade algorithm for optical flow on Python using cv2.calcOpticalFlowPyrLK. However, when I try to draw a line to connect the current and previous points as well as the point itself, the functions return None.
Here is the code
#draw the overlaying tracking img
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel() #tmp new value
c,d = old.ravel() #tmp old value
#draws a line connecting the old point with the new point
mask = cv2.line(mask,(a,b),(c,d),color[i].tolist(),2) #returns None... why??
#draws the new point
frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) #returns None... why??
img = cv2.add(frame,mask)
#show on window
cv2.imshow("frame",img)
a,b,c,d are numpy.float32
frame is my value read from the webcam
mask is init to an array of zeros
color is a random array
This is the error message I get when I try to imshow
error: /Users/vagrant/pisi-64bit/tmp/opencv-2.4.5-2/work/opencv-2.4.5/modules/core/src/array.cpp:2482: error: (-206) Unrecognized or unsupported array type in function cvGetMat
I really appreciate the help!
According to the OpenCV docs, cv2.line() and cv2.circle() always return None. http://docs.opencv.org/modules/core/doc/drawing_functions.html
These methods are in place methods, so they alter the first argument that you pass to them. See this Stack Overflow question that describes these types of functions: How are Python in-place operator functions different than the standard operator functions?
Your fixed code should look something like this:
#draw the overlaying tracking img
for i,(new,old) in enumerate(zip(good_new,good_old)):
a,b = new.ravel() #tmp new value
c,d = old.ravel() #tmp old value
#draws a line connecting the old point with the new point
cv2.line(mask,(a,b),(c,d),color[i].tolist(),2) #returns None... why??
#draws the new point
cv2.circle(frame,(a,b),5,color[i].tolist(),-1) #returns None... why??
img = cv2.add(frame,mask)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With