Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Using ROI from camera feed as Template for cvMatchTemplate

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,

click to hide/show revision 2
retagged

updated 2013-10-21 14:56:53 -0600

berak gravatar image

Using ROI from camera feed as Template for cvMatchTemplate

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,

Using ROI from camera feed as Template for cvMatchTemplate

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" , CV_WINDOW_NORMAL);
    cvNamedWindow("c" , CV_WINDOW_NORMAL);  
    cvNamedWindow("d" , CV_WINDOW_NORMAL);
    cvNamedWindow("e" , CV_WINDOW_NORMAL);
    cvNamedWindow("f" , CV_WINDOW_NORMAL);
    cvNamedWindow("g" , CV_WINDOW_NORMAL);
    cvNamedWindow("h" , CV_WINDOW_NORMAL);
    cvMoveWindow("a", 134, 0);     
    cvMoveWindow("b", 134, 400);
    cvMoveWindow("c", 551, 0);  
    cvMoveWindow("d", 551, 400);
    cvMoveWindow("e", 968, 0);
    cvMoveWindow("f", 968, 400);
    cvMoveWindow("g", 1385, 0);
    cvMoveWindow("h", 1385, 400);

    cvCreateTrackbar("RECT-X", "a", &rectx, 640, NULL);
    cvCreateTrackbar("RECT-Y", "a", &recty, 480, NULL);
    cvCreateTrackbar("RECT-WIDTH", "a", &rectwidth, 63, NULL);
    cvCreateTrackbar("RECT-HEIGHT", "a", &rectheight, 47, NULL);

    while(true){

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

      frame=cvCloneImage(frame); 
      CvSize sz = cvGetSize(frame);
      IplImage *src, *templ, *img, *ftmp[6]; // ftmp will hold results
      int i;
      CvRect roi;

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

      // Read in the source image to be searched
      src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
      cout << "RECTX = " << endl << " " << rectx << endl << endl;
      cout << "RECTY = " << endl << " " << recty << endl << endl;
      cout << "RECTWIDTH = " << endl << " " << rectwidth << endl << endl;
      cout << "RECTHEIGHT = " << endl << " " << rectheight << endl << endl;

      if (rectx < rectwidth){rectx = 1;};
      if (rectx > (sz.width - rectwidth)) {rectx = sz.width - rectwidth;};
      if (recty < rectheight){recty = 1;};
      if (recty > (sz.height - rectheight)) {recty = sz.height - rectheight;};
      if(rectwidth< 1){rectwidth=1;};
      if(rectheight< 1){rectheight=1;};


      img = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
      src = cvCreateImage(cvGetSize(frame), IPL_DEPTH_8U, 3);
      CvMat* submat = cvCreateMatHeader(frame->height, frame->width, CV_8UC3);


      cvCopy(frame, img);
       cvCopy(frame, src);
      roi = cvRect(rectx,recty,rectwidth,rectheight);
      cvSetImageROI(frame, roi);
      CvSize s=cvGetSize(frame);

      CvMat* sub=cvGetSubRect(img, submat, roi);
      cvCopy(frame, sub);
      cvSet(sub, cvScalar(0, 255, 0));
      cvShowImage( "b", img );
      cvReleaseImage(&img);
      cvReleaseMat(&submat);

      // Allocate Output Images:
      int iwidth = src->width - s.width + 1;
      int iheight = src->height - s.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
      cvShowImage( "a", frame);
      cvShowImage( "c", ftmp[0] );
      cvShowImage( "d", ftmp[1] );
      cvShowImage( "e", ftmp[2] );
      cvShowImage( "f", ftmp[3] );
      cvShowImage( "g", ftmp[4] );
      cvShowImage( "h", ftmp[5] );
       cvReleaseImage(&src);                 
       cvResetImageROI(frame);
      cvReleaseImage(&ftmp[0]);
  cvReleaseImage(&ftmp[1]);
  cvReleaseImage(&ftmp[2]);
  cvReleaseImage(&ftmp[3]);
  cvReleaseImage(&ftmp[4]);
  cvReleaseImage(&ftmp[5]);




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

    cvDestroyAllWindows() ;
    cvReleaseCapture(&capture);     

    return 0;
  }