1

I want to see if a template in present in an image using openCv and c++. However due to different distance at which the image is taken and different position of the image, the match does not occur correctly.

here is my code:

IplImage* image    = cvLoadImage("C:/images/Photo0734.jpg", 1);
IplImage* templat  = cvLoadImage("C:/images/templatecoin.jpg", 1);
int percent =25;// declare a destination IplImage object with correct size, 
depth and  channels
IplImage* image3 = cvCreateImage( cvSize((int)((image->width*percent)/100) , 
(int)((image->height*percent)/100) ),image->depth, image->nChannels );

//use cvResize to resize source to a destination image
cvResize(image, image3);
IplImage* image2   = cvCreateImage(cvSize(image3->width, image3->height), 
IPL_DEPTH_8U,   1);
IplImage* templat2 = cvCreateImage(cvSize(templat->width, 
templat->height),   IPL_DEPTH_8U, 1); 

cvCvtColor(image3, image2, CV_BGR2GRAY);
cvCvtColor(templat, templat2, CV_BGR2GRAY);


int w = image3->width - templat->width + 1;
int h = image3->height - templat->height + 1;
result = cvCreateImage(cvSize(w, h), IPL_DEPTH_32F, 1);
cvMatchTemplate(image2, templat2, result, CV_TM_CCORR_NORMED);

double min_val, max_val;
CvPoint min_loc, max_loc;
cvMinMaxLoc(result, &min_val, &max_val, &min_loc, &max_loc);

cvRectangle(image3, max_loc, cvPoint(max_loc.x+templat->width, 
max_loc.y+templat->height), cvScalar(0,1,1), 1);

cvShowImage("src", image3);
//cvShowImage("result image", result);
cvWaitKey(0);

Please note that I am Unable to use "Mat". Is it possible to use IplImage* and enable the code to be invariant to scaling and rotation? help me.

vidzz
  • 197
  • 2
  • 4
  • 15

1 Answers1

0

Let have a look to that :

SIFT Wiki

SIFT example

OpenCV SIFT documentation

I think that can be usefull for you.

Community
  • 1
  • 1
Vuwox
  • 2,331
  • 18
  • 33
  • @ Alexandre Bizeau- Hi, your example is using cv::Mat and i am not able to use it. That is why I am asking if invariant rotation and scaling can be implemented with IplImage. – vidzz Apr 19 '13 at 18:21
  • You cannot using because you are doing C ? Or I missing something ? Let me have a look for IplImage, if there a way to do it and to your code what the problem. – Vuwox Apr 19 '13 at 18:27
  • plz have a look to this :http://stackoverflow.com/questions/16109471/load-image-with-opencv-mat-c -I am unable to load an image with cv::Mat, however I manage to do so with IplImage – vidzz Apr 19 '13 at 18:39
  • Instead of talking about cv::Mat (Mat you are talking about) can you use cvMat ? The C format for Mat ? – Vuwox Apr 19 '13 at 18:41
  • Okay, But once you have load an image with cvLoadImage can you convert it in cvMat, normally you should. You have a problem with your OpenCV configuration for sure if you cannot use Mat. Don't avoid your first problem. Try to solve that before because IplImage are not support for OpenCV now. – Vuwox Apr 19 '13 at 18:47
  • I will be very pleased if you could kindly explain to me how to convert cvLoadImage to cvMat. – vidzz Apr 19 '13 at 18:51
  • Once you have done this : IplImage* image = cvLoadImage("C:/images/Photo0734.jpg", 1); Try do to cvMat* myMat(image); or cv::Mat* myMat(image); If I remember well. – Vuwox Apr 19 '13 at 19:01