OpenCV Python calcOpticalFlowFarneback

Thanks so much if anyone can help me. Im try, use the sample book, "OReilly Programming Computer Vision with Python," at the end of page 216.

#!/usr/bin/env python import cv2 def draw_flow(im,flow,step=16): h,w = im.shape[:2] y,x = mgrid[step/2:h:step,step/2:w:step].reshape(2,-1) fx,fy = flow[y,x].T # create line endpoints lines = vstack([x,y,x+fx,y+fy]).T.reshape(-1,2,2) lines = int32(lines) # create image and draw vis = cv2.cvtColor(im,cv2.COLOR_GRAY2BGR) for (x1,y1),(x2,y2) in lines: cv2.line(vis,(x1,y1),(x2,y2),(0,255,0),1) cv2.circle(vis,(x1,y1),1,(0,255,0), -1) return vis cap = cv2.VideoCapture(0) ret,im = cap.read() prev_gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) while True: # get grayscale image ret,im = cap.read() gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY) # compute flow #flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,None,0.5,3,15,3,5,1.2,0) flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,float(0),float(0),3,15,3,5,float(1),0) prev_gray = gray # plot the flow vectors cv2.imshow('Optical flow',draw_flow(gray,flow)) if cv2.waitKey(10) == 27: break 

Im running in terminal (LXUbuntu, lxterminal) and I get the following error:

 VIDIOC_QUERYMENU: Invalid argument VIDIOC_QUERYMENU: Invalid argument VIDIOC_QUERYMENU: Invalid argument Traceback (most recent call last): File "hw.py", line 35, in <module> flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,None,0.5,3,15,3,5,1.2,0) TypeError: a float is required 

I understand that the problem is with the calcOpticalFlowFarneback function because it requires a number in float, therefore im try calcOpticalFlowFarneback (prev_gray, gray, None, float (0.5), 3,15,3,5, float (1.2), 0 ), but does not work.

Many thanks.

+8
python opencv
source share
4 answers

You need to change the code a bit.

First of all, enable the Numpy library, as methods like mgrid , int32 , vstack are numpy functions.

So at the top of the code add:

 from numpy import * 

Secondly, to your question, the fourth argument should be int . You set it as a float. Do it 1 (or 3 as you like). And the last argument is deduced by himself. You do not need it. Therefore delete it.

So my last statement looks below (and this works fine for me):

 flow = cv2.calcOpticalFlowFarneback(prev_gray,gray,0.5,1,3,15,3,5,1) 

Try this and let me know if any error occurs.

+13
source share

@sunside

 flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) cv2.calcOpticalFlowFarneback(prev, next, flow, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags) → flow 

- opencv3.x grama, see alose opencv3.x doc

Otherwise:

 flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) TypeError: a float is required cv2.calcOpticalFlowFarneback(prev, next, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags[, flow]) 

you should have run opencv2.x, it can be claimed as follows:

 import cv2 print cv2.__version__ 

See opencv2.x doc

+3
source share

When I call the function as follows, the following error message is displayed:

  flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) 

Mistake:

 flow = cv2.calcOpticalFlowFarneback(prvs, next, None, 0.5, 3, 15, 3, 5, 1.2, 0) TypeError: a float is required 

When I delete the None value, my program works correctly:

 flow = cv2.calcOpticalFlowFarneback(prvs, next, 0.5, 3, 15, 3, 5, 1.2, 0) 

The calcOpticalFlowFarneback() function has the form:

 cv2.calcOpticalFlowFarneback(prev, next, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags[, flow]) → flow 

Turns out we shouldn't pass a None value for the pyr_scale parameter. We can pass None the flow parameter, though.

  • pyr_scale : parameter that determines the image scale (<1) for creating pyramids for each image; pyr_scale = 0.5 means the classic pyramid, where each next layer is half the size of the previous one.

  • flow : A calculated stream image that is the same size as prvs , and enter CV_32FC2 .

+2
source share

If you are using Python3, insert def draw_flow

 y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1).astype(int) flow = cv2.calcOpticalFlowFarneback(prevgray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) 

For Python 2 you should use

 y, x = np.mgrid[step/2:h:step, step/2:w:step].reshape(2,-1) flow = cv2.calcOpticalFlowFarneback(prevgray, gray, 0.5, 3, 15, 3, 5, 1.2, 0) 
+1
source share

All Articles