码迷,mamicode.com
首页 > 其他好文 > 详细

OpenCV:金字塔Lucas-Kanade光流测试代码

时间:2015-02-11 16:38:47      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:opencv

<span style="font-size:14px;">#include <opencv\cxcore.h>
#include <opencv\highgui.h>
#include <opencv\cv.h>
#include <stdio.h>

const int MAX_CORNERS = 500;
int main(int argc, char** argv) {

    IplImage* imgA = cvLoadImage("OpticalFlow0.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    IplImage* imgB = cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    CvSize      img_sz    = cvGetSize( imgA );
    int         win_size = 10;
    IplImage* imgC = cvLoadImage("OpticalFlow1.jpg",CV_LOAD_IMAGE_UNCHANGED);

    //需要做的第一件事就是获取我们想要跟踪的特征
    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 ];
    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)
        );

    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        = new CvPoint2D32f[ MAX_CORNERS ];
    cvCalcOpticalFlowPyrLK(				//在图像金字塔中计算LK光流
        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
        );

    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),2 );
    }
    cvNamedWindow("ImageA",0);
    cvNamedWindow("ImageB",0);
    cvNamedWindow("LKpyr_OpticalFlow",0);
    cvShowImage("ImageA",imgA);
    cvShowImage("ImageB",imgB);
    cvShowImage("LKpyr_OpticalFlow",imgC);
    cvWaitKey(0);
    return 0;
}
</span>

运行结果截图:

技术分享



OpenCV:金字塔Lucas-Kanade光流测试代码

标签:opencv

原文地址:http://blog.csdn.net/u010002704/article/details/43734927

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!