Using ROI from camera feed as Template for cvMatchTemplate

asked 2013-10-21 14:50:55 -0600

joeish80829 gravatar image

updated 2013-10-21 21:46:36 -0600

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

its suppose to use a a 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,

Edit: figured it out! here is some pretty neat code.....any ideas on how to improve it pls post back

  #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;
    }

    int rectx = 0;
    int recty = 0;
    int rectwidth = 64;
    int rectheight = 48;

    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);
    cvNamedWindow("a" , CV_WINDOW_NORMAL);     
    cvNamedWindow("b ...
(more)
edit retag flag offensive close merge delete

Comments

why are you creating img and src twice ?

are you ever using the roi , you create there ?

you try to run 6 different methods of template matching, but never evaluate their output ?

whatever you create, you have to release. either stick to that rule, or change to c++ (preaching to deaf ears, i know) // hint : ftmp memleak.

man, what a ball of ...

berak gravatar imageberak ( 2013-10-21 15:08:09 -0600 )edit

@berak I use the ROI in cvSetImageROI ....and i said in my post "I I took out the trackbars and windows to keep it short per guidelines ...." I took out the release images too to make the long code shorter can you help me get past that error

joeish80829 gravatar imagejoeish80829 ( 2013-10-21 15:16:23 -0600 )edit

ah, ok. you're right on the roi, did not see that. sorry. but then:

int iwidth = src->width - frame->width + 1;

int iheight = src->height - frame->height + 1;

this is still the size of the original image, not the roi, you're trying to use.

and again, creating an IplImage twice will give you memleak you can never correct.

berak gravatar imageberak ( 2013-10-21 15:21:53 -0600 )edit

and i still think, your reasons of choosing the c-api are ill-posed. -- see, it bites you!

berak gravatar imageberak ( 2013-10-21 15:28:35 -0600 )edit

"because it's the only thing i know" is not a valid reason

berak gravatar imageberak ( 2013-10-21 15:31:24 -0600 )edit

@berak thanks for the help your ideas led me to the right answer

joeish80829 gravatar imagejoeish80829 ( 2013-10-21 21:38:31 -0600 )edit