使用openCv进行图像比较

时间:2012-03-23 11:20:49

标签: iphone opencv

我正在使用OpenCV算法进行图像比较: 我的代码: - (无效)opencvImageCompare {     NSMutableArray * valuesArray = [[NSMutableArray alloc] init];

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<[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->width-imageTocompareIpl->width<=0 && img->height-imageTocompareIpl->height<=0){
        int balWidth=imageTocompareIpl->width-img->width;
        int balHeight=imageTocompareIpl->height-img->height;
        img->width=img->width+balWidth+100;
        img->height=img->height+balHeight+100;
    }



    CvSize size = cvSize(
                         img->width  - imageTocompareIpl->width  + 1,
                         img->height - imageTocompareIpl->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, &minval, &maxval, &minloc, &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(&imageTocompareIpl);

cvReleaseImage(&res);
}
cvReleaseImage(&img);

} 但是我没有得到准确的结果,当我选择相同的图像进行比较时,它没有给出值为0但是其他一些值。请帮我解决这个问题。

0 个答案:

没有答案