我想在两个图像中跟踪一个对象(镜头A、镜头B(。我知道对象在第一个镜头(镜头A(中的位置,但不知道对象在第二个镜头(场景B(中的定位。镜头A有多个对象,所以为了跟踪特定的对象,我选择要跟踪的对象所在的图像的ROI。问题是如何在镜头B中跟踪该对象的特征,同时保持相同的ROI大小。我可以在不选择ROI的情况下跟踪整个图像B中该对象的特征吗?
这是我的密码。目前,它在SHOTB中选择SHOTA的相同ROI,但有时SHOTA ROI中的对象不在SHOT B的ROI中。
IplImage* imgA = cvLoadImage("52783180_RAW_OVR1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cvSetImageROI(imgA, cvRect(2300, 1700, 1000,1200));
cvNamedWindow("SHOTA",0);
cvShowImage("SHOTA", imgA);
//cvWaitKey(0);
CvSize img_sz = cvGetSize( imgA );
int win_size = 10;
IplImage* imgB = cvLoadImage("52783180_RAW_OVR2.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cvSetImageROI(imgB, cvRect(2300, 1700, 1000,1200));
cvNamedWindow("SHOTB",0);
cvShowImage("SHOTB", imgB);
IplImage* imgC=cvLoadImage("52783180_RAW_OVR2.jpg",CV_LOAD_IMAGE_UNCHANGED);
cvSetImageROI(imgC, cvRect(2300, 1700, 1000,1200));
//cvNamedWindow("SHOTA",0);
//cvShowImage("SHOTA", imgA);
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 ];
//cvSetImageROI(imgA, cvRect(2300, 1700, 1000,1200));
cvGoodFeaturesToTrack(
imgA,
eig_image,
tmp_image,
cornersA,
&corner_count,
0.01,
5.0,
0,
3,
0,
0.04
);
//cvResetImageROI(imgA);
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 = 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 ),
0
);
// Now make some image of what we are looking at:
//
float sum=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;
}
sum+=(cornersA[i].x-cornersB[i].x)*(cornersA[i].x-cornersB[i].x)+(cornersA[i].y-cornersB[i].y)*(cornersA[i].y-cornersB[i].y);
// 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 );
}
cvResetImageROI(imgC);
sum=sum/corner_count;
printf("%fn",sum);
cvNamedWindow("ImageA",0);
cvNamedWindow("ImageB",0);
cvNamedWindow("LKpyr_OpticalFlow",0);
cvShowImage("ImageA",imgA);
cvShowImage("ImageB",imgB);
cvShowImage("LKpyr_OpticalFlow",imgC);
cvWaitKey(0);
GoodfeaturestoTrack 使用掩码而不是setimageroi解决的问题