金字塔LK光流算法
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
金字塔LK光流算法
constint MAX_CORNERS = 500;
void CImageExampleDlg::OnBnClickedButton8()
{
// TODO: Add your control notification handler code here
IplImage* imgA =
cvLoadImage("F://program//Book//Chapter4//OpticalFlow0.jpg",CV_LOAD_IMAGE_GRAYSCALE);
IplImage* imgB =
cvLoadImage("F://program//Book//Chapter4//OpticalFlow1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
CvSizeimg_sz = cvGetSize( imgA );
int win_size = 10;
IplImage* imgC =
cvLoadImage("F://program//Book//Chapter4//OpticalFlow1.jpg",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 ];
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 ];
CvSizepyr_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(
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 ),
);
// Now make some image of what we are looking at:
//
for( int i=0; i if( features_found[i]==0|| feature_errors[i]>550 ) { // printf("Error is %f/n",feature_errors[i]); continue; } // printf("Got it/n"); CvPointp0 = cvPoint( cvRound( cornersA[i].x ), cvRound( cornersA[i].y ) ); CvPointp1 = 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); }