OpenCV Python calcOpticalFlowFarneback

今天在看《python计算机视觉编程》P235的光流一小节时,将示例代码写到电脑上运行时遇到了错误,

代码如下:

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)

    prev_gray = gray

    # plot the flow vectors
    cv2.imshow('Optical flow',draw_flow(gray,flow))
    if cv2.waitKey(10) == 27:
        break

报错信息为:

TypeError: a float is required

解决方案:

1、在代码顶部输入

from numpy import *

因为mgrid, int32, vstack都是numpy中的方法

2、将cv2.calcOpticalFlowFarneback(prev_gray, gray, None, 0.5, 3, 15, 3, 5, 1.2, 0)中的None去掉,即

cv2.calcOpticalFlowFarneback(prev_gray, gray, 0.5, 3, 15, 3, 5, 1.2, 0)

根据opencv文档中calcOpticalFlowFarneback中的定义,第三项需要一个float值

calcOpticalFlowFarneback

Computes a dense optical flow using the Gunnar Farneback’s algorithm.

C++:  void  calcOpticalFlowFarneback (InputArray  prevImg, InputArray  nextImg, InputOutputArray  flow, double  pyrScale, int  levels, int  winsize, int  iterations, int  polyN, double  polySigma, int  flags )
C:  void  cvCalcOpticalFlowFarneback (const CvArr*  prevImg, const CvArr*  nextImg, CvArr*  flow, double  pyrScale, int  levels, int  winsize, int  iterations, int  polyN, double  polySigma, int  flags )
Python:   cv2. calcOpticalFlowFarneback (prevImg, nextImg, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags [, flow ] ) → flow
Parameters:
  • prevImg – First 8-bit single-channel input image.
  • nextImg – Second input image of the same size and the same type as prevImg .
  • flow – Computed flow image that has the same size as prevImg and type CV_32FC2 .
  • pyrScale – Parameter specifying the image scale (<1) to build pyramids for each image. pyrScale=0.5 means a classical pyramid, where each next layer is twice smaller than the previous one.
  • levels – Number of pyramid layers including the initial image. levels=1 means that no extra layers are created and only the original images are used.
  • winsize – Averaging window size. Larger values increase the algorithm robustness to image noise and give more chances for fast motion detection, but yield more blurred motion field.
  • iterations – Number of iterations the algorithm does at each pyramid level.
  • polyN – Size of the pixel neighborhood used to find polynomial expansion in each pixel. Larger values mean that the image will be approximated with smoother surfaces, yielding more robust algorithm and more blurred motion field. Typically, polyN =5 or 7.
  • polySigma – Standard deviation of the Gaussian that is used to smooth derivatives used as a basis for the polynomial expansion. For polyN=5 , you can set polySigma=1.1 . For polyN=7 , a good value would be polySigma=1.5 .
  • flags –

    Operation flags that can be a combination of the following:

    • OPTFLOW_USE_INITIAL_FLOW Use the input flow as an initial flow approximation.
    • OPTFLOW_FARNEBACK_GAUSSIAN Use the Gaussian \texttt{winsize}\times\texttt{winsize} filter instead of a box filter of the same size for optical flow estimation. Usually, this option gives z more accurate flow than with a box filter, at the cost of lower speed. Normally, winsize for a Gaussian window should be set to a larger value to achieve the same level of robustness.


参考自:

[1] https://stackoverflow.com/questions/13685771/opencv-python-calcopticalflowfarneback

[2] http://www.opencv.org.cn/opencvdoc/2.3.2/html/modules/video/doc/motion_analysis_and_object_tracking.html#calcopticalflowfarneback

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值