Optical Flow Example Not Producing Output

asked 2014-08-15 04:11:42 -0600

cpsmusic gravatar image

Hi,

I'm new to OpenCV. I'm currently developing a simple cloud tracking application. I'd like to use OpenCV's optical flow for this. I found some sample code which builds and runs OK however I'm not generating any output - the output image is blank. I'm using OpenCV 2.4.9 on OSX (10.9.4).

Can anyone see anything wrong with the sample code below?

Here's the code I'm using:

// Pyramid L-K optical flow example.

#include <stdio.h>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>

const int MAX_CORNERS = 500;

int main(int argc, char** argv) {

    // Initialize, load two images from the file system, and
    // allocate the images and other structures we will need for
    // results.

    IplImage* imgA = cvLoadImage("01.png", CV_LOAD_IMAGE_GRAYSCALE); 
    IplImage* imgB = cvLoadImage("02.png", CV_LOAD_IMAGE_GRAYSCALE);

    CvSize img_sz = cvGetSize( imgA ); 
    int win_size = 10;

    IplImage* imgC = cvLoadImage("out.png", CV_LOAD_IMAGE_UNCHANGED );

    // The first thing we need to do is get the features
    // we want to track.

    IplImage* eig_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 ); 
    IplImage* tmp_image = cvCreateImage( img_sz, IPL_DEPTH_32F, 1 );

    int corner_count = MAX_CORNERS;
//  CvPoint2D32f* cornersA = new CvPoint2D32f[ MAX_CORNERS ];
    CvPoint2D32f cornersA[ MAX_CORNERS ];

    cvGoodFeaturesToTrack( imgA, eig_image, tmp_image, cornersA, &corner_count, 0.01, 5.0, 0, 3, 0, 0.04);
    cvFindCornerSubPix( imgA, cornersA, corner_count, cvSize(win_size,win_size), cvSize(-1,-1), 
                        cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));

    // Call the Lucas Kanade algorithm //
    char features_found[ MAX_CORNERS ]; 
    float feature_errors[ MAX_CORNERS ];

    CvSize pyr_sz = cvSize( imgA->width+8, imgB->height/3 );

    IplImage* pyrA = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 ); 
    IplImage* pyrB = cvCreateImage( pyr_sz, IPL_DEPTH_32F, 1 );

    CvPoint2D32f cornersB[ MAX_CORNERS ];

    cvCalcOpticalFlowPyrLK( imgA, imgB,pyrA, pyrB, cornersA, cornersB, corner_count, cvSize( win_size,win_size ), 
                            5, features_found, feature_errors, cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 ), 0);

    // Now make some image of what we are looking at: //
    for( int i = 0; i < corner_count; i++ ) {
        if( features_found[i] == 0 || feature_errors[i] > 550 ) { 
            printf("Error is %f\n", feature_errors[i]); 
            continue;
        }

        printf("Got it\n"); 
        CvPoint p0 = cvPoint( cvRound( cornersA[i].x ), cvRound( cornersA[i].y ) );
        CvPoint p1 = cvPoint( cvRound( cornersB[i].x ), cvRound( cornersB[i].y ));

        cvLine( imgC, p0, p1, CV_RGB(255,0,0), 1, CV_AA, 1 ); 
    }

    cvNamedWindow("ImageA",0); 
    cvNamedWindow("ImageB",0); 
    cvNamedWindow("LKpyr_OpticalFlow",0);

    cvShowImage("ImageA",imgA); 
    cvShowImage("ImageB",imgB); 
    cvShowImage("LKpyr_OpticalFlow",imgC);

    cvWaitKey(0);

    return 0; 
}
edit retag flag offensive close merge delete

Comments

1

your democode is horribly outdated, they did away with the old c-api some years ago.

please try a more recent sample

berak gravatar imageberak ( 2014-08-15 04:16:39 -0600 )edit

Thanks for that!

cpsmusic gravatar imagecpsmusic ( 2014-08-15 04:43:38 -0600 )edit