Ask Your Question
0

Farneback optical flow in box/ROI

asked 2018-08-13 19:54:31 -0600

Krunch gravatar image

I'm detecting the optical flow by the Farneback method, but I need to delimit the area of the video that will be detected. I researched ROI, but I did not succeed. Any suggestions on how I can with the mouse create a box and the algorithm only run in that region?

#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help()
{
    cout <<
        "\nThis program demonstrates dense optical flow algorithm by Gunnar Farneback\n"
        "Mainly the function: calcOpticalFlowFarneback()\n"
        "Call:\n"
        "./fback\n"
        "This reads from video camera 0\n" << endl;
}
static void drawOptFlowMap(const Mat& flow, Mat& cflowmap, int step,
    double, 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)*5;
            line(cflowmap, Point(x, y), Point(cvRound(x + fxy.x), cvRound(y + fxy.y)),
                color);
            circle(cflowmap, Point(x, y), 2, color, -1);
        }
}

int main(int argc, char** argv)
{
    CommandLineParser parser(argc, argv, "{help h||}");
    if (parser.has("help")){
    help();
    return 0;
    }
    VideoCapture cap("completo.MPG");
    help();
    if (!cap.isOpened())
    return -1;

    Mat flow, cflow, frame;
    UMat gray, prevgray, uflow;
    namedWindow("flow", 1);

    for (;;) {
    cap >> frame;
    cvtColor(frame, gray, COLOR_BGR2GRAY);

    if (!prevgray.empty()){
    calcOpticalFlowFarneback(prevgray, gray, uflow, 0.5, 3, 15, 3, 5, 1.2, 0);
    cvtColor(prevgray, cflow, COLOR_GRAY2BGR);
    uflow.copyTo(flow);
    drawOptFlowMap(flow, cflow, 30, 1.5, Scalar(0, 255, 0));
    imshow("flow", cflow);

    VideoWriter(gray);
    }
    if (waitKey(30) >= 0)
    break;
    std::swap(prevgray, gray);
    }

    return 0;
}
edit retag flag offensive close merge delete

Comments

1

selectROI might also be useful here.

berak gravatar imageberak ( 2018-08-14 04:05:17 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-08-14 02:05:46 -0600

LBerger gravatar image

updated 2018-08-14 02:29:40 -0600

you can define a ROI :

    Mat flow, cflow, frame1,frame;
    ......
    cap >> frame1;
    Mat frame(frame1(Rect(0, 0, 100, 100)));

frame is not a deep copy of frame1 (frame and frame1 share same data)

You can define a rect as in grabcut.cpp or use function DefRectangle function

edit flag offensive delete link more

Comments

Thank you, it worked.

Krunch gravatar imageKrunch ( 2018-09-12 16:29:28 -0600 )edit

there is a new function to define a rectangle using mouse

LBerger gravatar imageLBerger ( 2018-09-13 00:58:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-08-13 19:54:31 -0600

Seen: 622 times

Last updated: Aug 14 '18