OpenCV:如何对通过calcOpticalFlowFarneback获得的向量应用过滤器?

时间:2017-01-20 09:43:23

标签: c++ opencv opticalflow

我已经找到并构建了一个光流图,我现在想要删除任何低于某个阈值的向量。这就是我设置Farneback光流的方法:

if (prevgray.empty() == false ) {
calcOpticalFlowFarneback(prevgray,gray,flowUmat, 0.4,1,50,2,5,1.2,0);
flowUmat.copyTo(flow);

for( int y=0; y<original.rows; y+=7){
    for (int x=0;x<original.cols;x+=7){

        const Point2f& flowatxy=flow.at<Point2f>(y,x);
        line(original, Point(x,y), Point(cvRound(x+flowatxy.x*4), cvRound(y+flowatxy.y*4)), Scalar(0,255,0));
        theta=atan((flowatxy.y)/(flowatxy.x)); //very unsure of this
        circle(original, Point(x,y), 0.1, Scalar(0,0,0),-1);
    }
}
gray.copyTo(prevgray);

    }
else{gray.copyTo(prevgray);}

我在考虑将每个矢量与相邻矢量或图像中所有矢量的平均值进行比较。

1 个答案:

答案 0 :(得分:0)

要删除任何低于特定阈值的矢量,首先需要估算长度,如果您需要,例如丢弃长运动矢量。你的循环看起来不像:

...    
const Point2f& flowatxy=flow.at<Point2f>(y,x);
float flowVectorLength = flowatxy.x * flowatxy.x + flowatxy.y * flowatxy.y;
if( flowVectorLength > threshold * threshold)
    continue;
...

实际上,运动矢量长度必须由float flowVectorLength = sqrt(flowatxy.x * flowatxy.x + flowatxy.y * flowatxy.y);计算,但为了避免平方根(sqrt)的计算复杂估计,您可以将其与threshold的平方进行比较。