How to extract velocity vectors of a pixels from calcOpticalFlowFarneback

穿精又带淫゛_ 提交于 2019-12-01 23:20:37

Mat& flow: The computed flow image; will have the same size as prevImg and type CV_32FC2

This type means that you have 2 float variables (presumed x and y) for each pixel's motion. Try this:

Optical_Flow.at<cv::Point2f>(1023,767).x
Optical_Flow.at<cv::Point2f>(1023,767).y
void drawOptFlowMap (const Mat& flow, Mat& cflowmap, int step, double scale, const Scalar& color) {
 for(int y = 0; y < cflowmap.rows; y += step)
        for(int x = 0; x < cflowmap.cols; x += step)
        {
            const Point2f& fxy = flow.at<Point2f>(y, x);
            line(cflowmap, Point(x,y), Point(cvRound(x+fxy.x), cvRound(y+fxy.y)),
                 color);
            circle(cflowmap, Point(cvRound(x+fxy.x), cvRound(y+fxy.y)), 1, color, -1);
        }
    }


int main()
{
 ...read, declare images etc.... 
 cvNamedWindow("optical Flow", CV_WINDOW_NORMAL);

calcOpticalFlowFarneback(Previous_Gray, Current_Gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);
cvtColor(Previous_Gray, cflow, CV_GRAY2BGR);
drawOptFlowMap(flow, cflow, 32, 50, CV_RGB(0, 255, 0));
imshow("optical Flow", cflow);
etc etc 

}

To extract the computed Optical Flow vectors you can use the following function:

static void writeOpticalFlowVectors(const Mat& flow){
    std::ofstream myfile;
    std::string fullPath = "opticalFlowVectors.txt";
    myfile.open (fullPath.c_str());

    for(std::size_t x = 0; x < flow.rows; ++x){
        for(std::size_t y = 0; y < flow.cols; ++y){
            myfile << flow.at<cv::Point2f>(x,y).x;
            myfile<< ", ";
            myfile<< flow.at<cv::Point2f>(x,y).y;
            myfile << "\n";     
        }
    }
    myfile.close();
}

This will iterate over your output Mat and write every vector in the form x, y to a txt file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!