2012-12-03 64 views
8

非常感謝,如果有人能幫助我。林嘗試,用書「奧萊利編程計算機視覺與Python」的一個例子,在頁面的末端216OpenCV Python calcOpticalFlowFarneback

#!/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)但不工作。

非常感謝。

+0

「浮點數需要」 很可能會參照非float,參數列表中的非int參數.. – asheeshr

回答

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) 

試試這個,讓我知道如果有錯誤出現。

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 

事實證明,我們不應該通過參數pyr_scaleNone的值。不過,我們可以通過None參數flow

  • pyr_scale:參數,指定圖像規模(< 1)構建金字塔爲每個圖像; pyr_scale = 0.5意味着古典金字塔,其中每個下一層比前一層小兩倍。

  • flow:與prvsCV_32FC2類型具有相同大小的計算流程圖像。

+0

正如他們所指出的,這是無關的評論,然而這就是爲什麼發生這種情況: 因爲該函數接受python 2和python 3中的不同參數。所以,請確保您正在閱讀與您的環境相匹配的手冊。你可以用cv2.cv .__ version__檢查 – GrigorisG

3

@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,看到alose opencv3.x doc

否則:

必須已在opencv2.x運行
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]) 

,也可以是斷言如下:

import cv2 
print cv2.__version__ 

opencv2.x doc

1

如果使用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)