OpenCV Python calcOpticalFlowFarneback
来源:互联网 发布:知无涯 教育 编辑:程序博客网 时间:2024/05/22 06:59
今天在看《python计算机视觉编程》P235的光流一小节时,将示例代码写到电脑上运行时遇到了错误,
代码如下:
import cv2def 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 viscap = 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 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
阅读全文
0 0
- OpenCV Python calcOpticalFlowFarneback
- OpenCV中CalcOpticalFlowFarneback()函数分析
- OpenCV中CalcOpticalFlowFarneback()函数分析
- calcOpticalFlowFarneback
- calcOpticalFlowFarneback
- OpenCV图像处理-光流法-原理分析1-calcOpticalFlowFarneback
- calcOpticalFlowFarneback函数解释
- .NET + OpenCV & Python + OpenCV
- python-opencv
- python opencv
- python+opencv
- OpenCV+ Python
- OPENCV+PYTHON
- python-opencv
- opencv python
- python opencv
- mac 安装opencv/python-opencv
- opencv python cool
- 操作复杂对象结构——访问者模式(二)
- Java 性能调优的 11 个实用技巧
- python 怎么写一个包含单双三引号的字符串
- 小程序页面跳转
- 在应用程序中使用虚拟内存(VirtualAlloc VirtualFree)
- OpenCV Python calcOpticalFlowFarneback
- perl模块安装
- 不同vlan之间的通信
- 快速求和(炒鸡数据版)
- 树莓派配置usb无线网卡
- PIL.Image的简单使用
- 2144 数据结构实验之图论九:最小生成树
- 费曼技巧
- 总结--15个实用CSS属性