user1651460 - 8 months ago 190

C++ Question

I am useing the 2.4.4 version of OpenCV. - i know its a beta

but there is an example about

`simpleflow_demo.cpp`

The documentation about the method is a little bit strange, saying the output files are a x- and yflow instead of the

`cv::Mat& flow`

Any ideas how to fix the problem to get the function working?

Answer

Try this simple demo that worked for me, then modify for your needs (display help from here):

```
Mat frame1 = imread("/home/radford/Desktop/1.png");
Mat frame2 = imread("/home/radford/Desktop/2.png");
namedWindow("flow");
Mat flow;
calcOpticalFlowSF(frame1, frame2, flow, 3, 2, 4);
Mat xy[2];
split(flow, xy);
//calculate angle and magnitude
Mat magnitude, angle;
cartToPolar(xy[0], xy[1], magnitude, angle, true);
//translate magnitude to range [0;1]
double mag_max;
minMaxLoc(magnitude, 0, &mag_max);
magnitude.convertTo(magnitude, -1, 1.0/mag_max);
//build hsv image
Mat _hsv[3], hsv;
_hsv[0] = angle;
_hsv[1] = Mat::ones(angle.size(), CV_32F);
_hsv[2] = magnitude;
merge(_hsv, 3, hsv);
//convert to BGR and show
Mat bgr;//CV_32FC3 matrix
cvtColor(hsv, bgr, COLOR_HSV2BGR);
imshow("flow", bgr);
waitKey(0);
```

Source (Stackoverflow)