0
我想跟蹤2個圖像中的一個對象(Shot A,Shot B)。 我知道對象在第一張照片(ShotA)中的位置,但我不知道對象在第二張照片中的位置(Shot B)。 鏡頭A有多個對象,所以爲了跟蹤特定的對象,我選擇了圖像的ROI,我想跟蹤的對象是。問題是如何在保持相同ROI大小的同時跟蹤Shot B中該對象的特徵。我可以在沒有選擇ROI的情況下跟蹤整個圖像B中該物體的特徵嗎?跟蹤功能
這是我的代碼。目前,它選擇將太的在SHOTB同一ROI,但有時在將太的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("%f\n",sum);
cvNamedWindow("ImageA",0);
cvNamedWindow("ImageB",0);
cvNamedWindow("LKpyr_OpticalFlow",0);
cvShowImage("ImageA",imgA);
cvShowImage("ImageB",imgB);
cvShowImage("LKpyr_OpticalFlow",imgC);
cvWaitKey(0);