简体   繁体   中英

Using ROI from camera feed as Template for cvMatchTemplate

This is code that im rewriting that i wrote successfully before.

its suppose to use aa roi from a webcam and match it with cvMatchTemplate against other webcam frames...I took out the trackbars and windows to keep it short per guidelines but in the original you could move the trackbars to select a section of the frame in the top left window and in the bottom left window you saw your template here is a picture of what it looked like:

http://i983.photobucket.com/albums/ae313/edmoney777/Screenshotfrom2013-10-21112021_zpsae11e3f0.png

Here is the error im getting I tried changing the depth of src to 32F with no luck...read the templmatch.cpp line 384 the error mssg gave me but no help there

 OpenCV Error: Assertion failed (result.size() == cv::Size(std::abs
 (img.cols - templ.cols) + 1, std::abs(img.rows - templ.rows) + 1) 
 && result.type() == CV_32F) in cvMatchTemplat

Im new to opencv and could use a little help debugging the code below

 #include <cv.h>
 #include <highgui.h>
 using namespace std;


 int main(){
   CvCapture* capture =0;       

   capture = cvCaptureFromCAM(0);
   if(!capture){
     printf("Capture failure\n");
     return -1;
   }

   IplImage* frame=0;
   double width=640.0;
   double height=480.0;
   cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
   cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, height);

       while(true){

     frame = cvQueryFrame(capture);           
     if(!frame) break;

     frame=cvCloneImage(frame); 
     IplImage *src, *templ, *ftmp[6]; // ftmp will hold results
     int i;
     CvRect roi;
     int rectx = 0;
     int recty = 0;
     int rectwidth = frame->width /10;
     int rectheight = frame->height /10;
     IplImage* img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);

     // Read in the source image to be searched
     src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);

     roi=cvRect(rectx, recty, rectwidth, rectheight);

     img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
     src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
     cvCopy(frame, img);
     cvSetImageROI(frame, roi);

     cvShowImage( "b", img );
     cvReleaseImage(&img);
      // Allocate Output Images:
     int iwidth = src->width - frame->width + 1;
     int iheight = src->height - frame->height + 1;

     for(i = 0; i < 6; ++i){
       ftmp[i]= cvCreateImage( cvSize( iwidth, iheight ), 32, 1 );
     }

     // Do the matching of the template with the image
     for( i = 0; i < 6; ++i ){
       cvMatchTemplate( src, frame, ftmp[i], i );
       cvNormalize( ftmp[i], ftmp[i], 1, 0, CV_MINMAX );
     }       
     // DISPLAY

     cvReleaseImage(&src);                 
     cvResetImageROI(frame);
     cvReleaseImage(&frame);


     //Wait 50mS
     int c = cvWaitKey(10);
     //If 'ESC' is pressed, break the loop
     if((char)c==27 ) break;      
   }

   cvDestroyAllWindows() ;
   cvReleaseCapture(&capture);     

   return 0;
 }

I am new to OpenCV and really don't know what to do with this error-message. Anyone an idea/pointer what to do? Your help is very appreciated! Cheers,

As you are performing template matching on the selected ROI of the image, you have to create the output images according to the size of ROI.

// Allocate Output Images:
int iwidth = src->width - rectwidth + 1;
int iheight = src->height - rectheight + 1;

Your code contains memory leaks which may crash the program eg all 6 images of ftmp are being allocated in each iteration but not being released anywhere. Either release the images at the end of iteration, or create them only once before while loop.

Also, OpenCV documentation explicitly states not to modify the frame returned by cvQueryFrame . So you may consider removing cvReleaseImage(&frame); . Check this answer for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM