nagaraju
nagaraju

Reputation: 59

is there a way to convert a dense optical flow to sparse optical flow in opencv c++?

    Mat i1 = imread("1.jpg", 0); //read as a gray scale image
    Mat i2 = imread("2.jpg", 0); //reas as a gray scale image

    Mat flowMat; 

    vector <Point2f> i1_corner, i2_corner;
    vector <uchar> status;
    vector <float> err;

    goodFeaturesToTrack(i1, i1_corner, 1000, 0.01, 30);
    calcOpticalFlowPyrLK(i1, i2, i1_corner, i2_corner, status, err);

I want to track the i1_corner feature points in i2 image

in the above code I am tracking them using the iterative Lucas-Kanade method

calcOpticalFlowPyrLK(i1, i2, i1_corner, i2_corner, status, err);

But can I track them using the DIS optical flow which is implemeted in the following function in opencv

createOptFlow_DIS(DISOpticalFlow::PRESET_ULTRAFAST)->calc(i1, i2, flowMat);

the above function finds the dense optical flow for every pixel in image i1

Upvotes: 1

Views: 1161

Answers (1)

Tobias Senst
Tobias Senst

Reputation: 2830

Simple add the optical flow (displacement) vector of the respective position in the flow field:

Mat i1 = imread("1.jpg", 0); //read as a gray scale image
Mat i2 = imread("2.jpg", 0); //reas as a gray scale image

Mat flowMat; 

vector <Point2f> i1_corner, i2_corner;
vector <uchar> status;
vector <float> err;

goodFeaturesToTrack(i1, i1_corner, 1000, 0.01, 30);
createOptFlow_DIS(DISOpticalFlow::PRESET_ULTRAFAST)->calc(i1, i2, flowMat);
i2_corner.resize(i1_corner.size());
for( unsigned int i = 0; i < i1_corner.size(); i++)
{
    i2_corner[i] = i1_corner[i] + flowMat.at<cv::Point2f>(i1_corner[i]);
}

Please note, that the last line reads the flow at the pixel postion, i.e. i1_corner position are rounded. To get the flow value on a subpixel level you will need to perform an interpolation there. However, since in particular the DIS flow computes very blurry (rough) flow fields, interpolation will not change the tracking significantly.

Upvotes: 0

Related Questions