title: Image Comparison using openCv
body: <p>I am using OpenCV algorithm for image comparison:
  My code :
  -(void)opencvImageCompare{
      NSMutableArray *valuesArray=[[NSMutableArray alloc]init];</p>
  
  <pre><code>NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  
  IplImage *img = [self CreateIplImageFromUIImage:imageView.image];
  // always check camera image
  if(img == 0) {
      printf("Cannot load camera img");
  
  }
  
  
  for (int i=0; i&lt;[appDelegate.weedArray count]; i++) {
      weedObject=[appDelegate.weedArray objectAtIndex:i];
      IplImage  *res; 
      CvPoint   minloc, maxloc;
      double    minval, maxval;
      double values;
      NSString *weedImage=[weedObject.image stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
      UIImage *imageTocompare = [UIImage imageNamed:weedImage];
      IplImage *imageTocompareIpl = [self CreateIplImageFromUIImage:imageTocompare];
      // always check server image 
      if(imageTocompareIpl == 0) {
          printf("Cannot load serverIplImageArray image");
      }
      if(img-&gt;width-imageTocompareIpl-&gt;width&lt;=0 &amp;&amp; img-&gt;height-imageTocompareIpl-&gt;height&lt;=0){
          int balWidth=imageTocompareIpl-&gt;width-img-&gt;width;
          int balHeight=imageTocompareIpl-&gt;height-img-&gt;height;
          img-&gt;width=img-&gt;width+balWidth+100;
          img-&gt;height=img-&gt;height+balHeight+100;
      }
  
  
  
      CvSize size = cvSize(
                           img-&gt;width  - imageTocompareIpl-&gt;width  + 1,
                           img-&gt;height - imageTocompareIpl-&gt;height + 1
                           );
  
      res = cvCreateImage(size, IPL_DEPTH_32F, 1);
  
      // CV_TM_SQDIFF CV_TM_SQDIFF_NORMED
      // CV_TM_CCORR  CV_TM_CCORR_NORMED
      // CV_TM_CCOEFF CV_TM_CCOEFF_NORMED
  
      cvMatchTemplate(img, imageTocompareIpl, res,CV_TM_CCOEFF);
      cvMinMaxLoc(res, &amp;minval, &amp;maxval, &amp;minloc, &amp;maxloc, 0);
      printf("\n value %f", maxval-minval);
      values=maxval-minval;
      NSString *valString=[NSString stringWithFormat:@"%f",values];
      [valuesArray addObject:valString];
      weedObject.values=[valString doubleValue];
      printf("\n------------------------------");
  
      cvReleaseImage(&amp;imageTocompareIpl);
  
  cvReleaseImage(&amp;res);
  }
  cvReleaseImage(&amp;img);
  </code></pre>
  
  <p>}
  But I am not getting accurate result, when I choose same images for comparison it does not give value as 0 but some other values. Please Help me with this issue.</p>