您的位置:首页 > 运维架构

calcOpticalFlowFarneback

2017-04-08 04:16 483 查看
//函数原型
void calcOpticalFlowFarneback( InputArray prev, InputArray next, InputOutputArray flow,
double pyr_scale, int levels, int winsize,
int iterations, int poly_n, double poly_sigma,
int flags );
/*@param prev first 8-bit single-channel input image.输入的影像必须是单通道,8位的灰度影像
@param next second input image of the same size and the same type as prev.
@param flow computed flow image that has the same size as prev and type CV_32FC2.输出的结果是32位,2通道的影像,可以使用Point2f 或者Vec2f进行转换
@param pyr_scale parameter, specifying the image scale (\<1) to build pyramids for each image;金字塔缩放级别,这个参数必须设置为小于1的浮点数
pyr_scale=0.5 means a classical pyramid, where each next layer is twice smaller than the previous
one.
@param 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.
@param 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.
@param iterations number of iterations the algorithm does at each pyramid level 迭代的次数,迭代的次数越高,精度越好;一般情况2~3次迭代就能够获得良好的
结果,但是选择6次,在大多数影像中都能获得良好的效果
@param poly_n size of the pixel neighborhood used to find polynomial expansion in each pixel; 拟合多边形的窗口尺寸 一般情况下选择5~7之间
larger values mean that the image will be approximated with smoother surfaces, yielding more
robust algorithm and more blurred motion field, typically poly_n =5 or 7.
@param poly_sigma standard deviation of the Gaussian that is used to smooth derivatives used as a这个参数一般选择0.2*polyN
basis for the polynomial expansion; for poly_n=5, you can set poly_sigma=1.1, for poly_n=7, a
good value would be poly_sigma=1.5.
@param flags operation flags that can be a combination of the following:
-   **OPTFLOW_USE_INITIAL_FLOW** uses the input flow as an initial flow approximation.
-   **OPTFLOW_FARNEBACK_GAUSSIAN** uses the Gaussian \f$\texttt{winsize}\times\texttt{winsize}\f$
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.*/
//一段具体的代码
#include <iostream>#include <vector>#include <opencv2/opencv.hpp>  //头文件#include <opencv2/xfeatures2d.hpp>using namespace cv;  //包含cv命名空间using namespace std;//string filename="C:\\Users\\Administrator\\Desktop\\标准测试图片"int main(){string filename1 = "C:\\Users\\Administrator\\Desktop\\标准测试图片\\left08.jpg";string filename2 = "C:\\Users\\Administrator\\Desktop\\标准测试图片\\left09.jpg";Mat img1 = imread(filename1, IMREAD_GRAYSCALE);Mat img2 = imread(filename2, IMREAD_GRAYSCALE);Mat result;//保存结果 CF_32FC2Mat Out = Mat::zeros(img1.size(), CV_8UC3);calcOpticalFlowFarneback(img1, img2, result, 0.5, 6, 7, 6, 5, 1.1,OPTFLOW_FARNEBACK_GAUSSIAN);for (int i = 0; i < img1.rows;i+=30){for (int j = 0; j < img1.cols;j+=30){Point2f pt = result.at<Point2f>(i, j);line(Out, Point2f(j, i), Point2f(j + pt.x, i + pt.y), Scalar(255, 0, 0), 1, CV_AA);}}imshow("Result", Out);imwrite("Result.jpg", Out);waitKey(0);return 0;}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息