slow optical flow

asked 2016-02-21 01:58:04 -0600

AdamIB gravatar image

updated 2018-02-04 04:02:21 -0600

Hi,

Is there any suggestion to enhance the performance for this optical flow implementation

#define MAX_COUNT 250 #define DELAY_T 20 #definePI 3.1415        void main (){ IplImage* image = 0;

IplImage* current_Img = 0;
IplImage* Old_Img = 0;
IplImage * imgA = 0;
IplImage * imgB = 0;
CvCapture * capture = cvCreateFileCapture("1.mov"); 
cvNamedWindow("Origin");
IplImage * eig_image = 0;
IplImage * tmp_image = 0;
int corner_count = MAX_COUNT;
CvPoint2D32f* cornersA = new CvPoint2D32f[MAX_COUNT];
CvPoint2D32f * cornersB = new CvPoint2D32f[MAX_COUNT];

CvSize img_sz;
int win_size = 20;

IplImage* pyrA = 0;
IplImage* pyrB = 0;

char features_found[MAX_COUNT];
float feature_errors[MAX_COUNT];

int one_zero = 0;
int t_delay = 0;

while (1) {

    if (cvGrabFrame(capture) == 0)
        break;  
    if (Old_Img == 0)
    {
        image = cvRetrieveFrame(capture);
        current_Img = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
        Old_Img = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);

    }
    if (one_zero == 0)
    {
        if (eig_image == 0)
        {
            eig_image = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
            tmp_image = cvCreateImage(cvSize(image->width, image->height), image->depth, image->nChannels);
        }


        memcpy(Old_Img->imageData, current_Img->imageData, sizeof(char)*image->imageSize);
        image = cvRetrieveFrame(capture);
        memcpy(current_Img->imageData, image->imageData, sizeof(char)*image->imageSize);


        if (imgA == 0)
        {
            imgA = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
            imgB = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
        }


        cvCvtColor(current_Img, imgA, CV_BGR2GRAY);
        cvCvtColor(Old_Img, imgB, CV_BGR2GRAY);


        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));


        CvSize pyr_sz = cvSize(imgA->width + 8, imgB->height / 3);
        if (pyrA == 0)
        {
            pyrA = cvCreateImage(pyr_sz, IPL_DEPTH_32F, 1);
            pyrB = cvCreateImage(pyr_sz, IPL_DEPTH_32F, 1);
        }

        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, 0.3), 0);


        for (int i = 0; i< corner_count; ++i)
        {

            if (features_found[i] == 0 || feature_errors[i] > MAX_COUNT)
                continue;


            float fVecLength = sqrt((float)((cornersA[i].x - cornersB[i].x)*(cornersA[i].x - cornersB[i].x) + (cornersA[i].y - cornersB[i].y)*(cornersA[i].y - cornersB[i].y)));
            //Vector Angle
            float fVecSetha = fabs(atan2((float)(cornersB[i].y - cornersA[i].y), (float)(cornersB[i].x - cornersA[i].x)) * 180 / PI);

            cvLine(image, cvPoint(cornersA[i].x, cornersA[i].y), cvPoint(cornersB[i].x, cornersA[i].y), CV_RGB(0, 255, 0), 2);

            printf("[%d] - Sheta:%lf, Length:%lf\n", i, fVecSetha, fVecLength);
        }




    }
    cvShowImage("Origin", image);
    one_zero++;
    if ((one_zero % DELAY_T) == 0)
    {
        one_zero = 0;
    }


    if (cvWaitKey(10) >= 0)
        break;
}


cvReleaseCapture(&capture);

cvDestroyWindow("Origin");

cvReleaseImage(&Old_Img);

cvReleaseImage(&imgA);
cvReleaseImage(&imgB);
cvReleaseImage(&eig_image);
cvReleaseImage(&tmp_image);
delete cornersA;
delete cornersB;
cvReleaseImage(&pyrA);
cvReleaseImage(&pyrB);

}

edit retag flag offensive close merge delete

Comments

3

please avoid using opencv's discontinued c-api, this is a dead end.

please use the c++ api instead

berak gravatar imageberak ( 2016-02-21 01:59:55 -0600 )edit

dear berak, thanks for your reply. Can you plz expaln more?

thanks

AdamIB gravatar imageAdamIB ( 2016-02-21 02:03:34 -0600 )edit