跟踪功能

时间:2013-02-25 17:41:53

标签: opencv

我想跟踪2张图像中的对象(镜头A,镜头B)。 我知道第一个镜头(ShotA)中物体的位置,但我不知道第二个镜头(镜头B)中物体的位置。 镜头A有多个对象,因此为了跟踪特定对象,我选择了我想要跟踪的对象的图像的ROI。问题是如何在保持相同ROI大小的同时跟踪Shot B中该对象的功能。我可以在不选择投资回报率的情况下跟踪整个图像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("%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);

1 个答案:

答案 0 :(得分:0)

通过使用mask而不是setimageroi for GoodfeaturestoTrack来解决问题