13

非常感谢,如果有人可以帮助我。我尝试使用第 216 页末尾的“OReilly Programming Computer Vision with Python”一书的示例。

    #!/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

我在终端(LXUbuntu,lxterminal)中执行,我收到以下错误:

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

我知道问题出在函数 calcOpticalFlowFarneback 中,因为这需要浮点数,因此,我尝试 calcOpticalFlowFarneback(prev_gray,gray,None,float(0.5),3,15,3,5,float(1.2),0 ) 但不工作。

非常感谢。

4

4 回答 4

13

您需要稍微更改一下代码。

首先,包括 Numpy 库,因为mgrid, int32,等方法vstack是 numpy 函数。

所以在代码的顶部,添加:

from numpy import *

其次,关于您的问题,第四个参数应该是int. 您已将其作为浮点数提供。将其设为 1(或 3,如您所愿)。最后一个参数是输出本身。你不需要它。所以删除它。

所以我的最终陈述如下所示(对我来说效果很好):

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

试试这个,如果出现任何错误,请告诉我。

于 2012-12-03T16:51:02.777 回答
5

@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 语法,请参见opencv3.x doc

否则:

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])

你必须在 opencv2.x 上运行过,它可以这样断言:

import cv2
print cv2.__version__

opencv2.x 文档

于 2017-05-23T09:50:37.573 回答
3

如果你使用 Python3,插入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)

对于 Python 2,您应该使用

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)
于 2018-02-10T22:08:05.823 回答
2

当我如下调用该函数时,将显示以下错误消息:

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

错误:

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

当我删除该值时None,我的程序可以正常工作:

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

功能calcOpticalFlowFarneback()成型:

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

事实证明,我们不应该传递Nonefor 参数的值pyr_scale。我们可以传递None给参数flow

  • pyr_scale:一个参数,指定图像比例(<1)为每个图像构建金字塔;pyr_scale = 0.5表示经典金字塔,其中下一层比上一层小两倍。

  • flow: 计​​算的流图,其大小prvs和类型相同CV_32FC2

于 2016-08-01T19:05:36.920 回答