The Gunnar-Farneback optical flow

The Gunnar-Farneback algorithm was developed to produce dense Optical Flow technique results (that is, on a dense grid of points). The first step is to approximate each neighborhood of both frames by quadratic polynomials. Afterwards, considering these quadratic polynomials, a new signal is constructed by a global displacement. Finally, this global displacement is calculated by equating the coefficients in the quadratic polynomials' yields.

Let's now see the implementation of this method, which uses the calcOpticalFlowFarneback()function. The following is an example (maxMovementFarneback) that uses this function to detect the maximum movement as shown in the previous example:

void maxMovementFarneback(Mat& prev_frame, Mat& frame)
{
    // 1-Set the Parameters
    Mat optical_flow = Mat(prev_frame.size(), COLOR_BGR2GRAY);
    double pyr_scale = 0.5;
    int levels = 3;
    int win_size = 5;
    int iterations = 5;
    int poly_n = 5;
    double poly_sigma = 1.1;
    int flags = 0;

    // 2-Farneback method for the Optical Flow technique
    calcOpticalFlowFarneback(prev_frame, frame, optical_flow, pyr_scale, levels, win_size, iterations, poly_n, poly_sigma, flags);

    // 3-Show the movements
    int max_move = 0;
    for (int i = 1; i <optical_flow.rows ; i++)
    {
        for (int j = 1; j <optical_flow.cols ; j++)
        {
            Point2f &p = optical_flow.at<Point2f>(i, j);
            Point pA = Point(round(i + p.x),round(j + p.y));
            Point pB = Point(i, j);
            int move = sqrt(p.x*p.x + p.y*p.y);
            if( move >MIN_MOVEMENT )
            {
                line(prev_frame, pA, pB, Scalar(255,0,0),2);
                if ( move > max_move )
                    max_move = move;
            }
        }
    }
    if(max_move >MAX_MOVEMENT)
    {
        putText(prev_frame,"INTRUDER",Point(100,100),FONT_ITALIC,3,Scalar(255,0,0),5);
        imshow("Video Camera", prev_frame);
        cout << "Press a key to continue..." << endl;
        waitKey();
    }
}

This function receives two consecutive frames, estimates the optical flow with different parameters, and returns an array with the same size as the input frame, where each pixel is actually a point (Point2f) that represents the displacement for that pixel. Firstly, different parameters are set for this function. Of course, you can also use your own criteria to configure the performance. Then, with these parameters, the Optical Flow technique is performed between each two consecutive frames. Consequently, we obtain an array with the estimations for each pixel, which is optical_flow. Finally, the movements that are greater than MIN_MOVEMENT are displayed on the screen. If the largest movement is greater than MAX_MOVEMENT, then an INTRUDER message is displayed.

Understandably, this method is quite slow because the Optical Flow technique is computed over each pixel on the frame. The output of this algorithm is similar to the previous method, although it's much slower.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset