如何在iOS(iPhone)中使用opencv比较图像 [英] How to compare images using opencv in iOS (iPhone)

查看:178
本文介绍了如何在iOS(iPhone)中使用opencv比较图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中比较iPhone相机拍摄的2张照片。我正在使用OpenCV来做到这一点。还有其他更好的方法吗?
如果我得到%相似度,它会很棒。

I want to compare 2 images taken by iPhone camera in my project. I am using OpenCV for doing that. Is there any other better way to do that? If i got the % similarity, It will be great.

我使用OpenCV代码进行图像比较:

I am using OpenCV following code for image comparison:

-(void)opencvImageCompare{ 
 NSMutableArray *valuesArray=[[NSMutableArray alloc]init];
 IplImage *img = [self CreateIplImageFromUIImage:imageView.image];
// always check camera image
 if(img == 0) {
   printf("Cannot load camera img");

}

IplImage  *res; 
CvPoint   minloc, maxloc;
double    minval, maxval;
double values;

UIImage *imageTocompare = [UIImage imageNamed:@"MyImageName"];
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);

}

对于同一图像,我得到非零结果(14956) ...)如果我通过不同的图像崩溃。

For the same image I am getting non zero result (14956...) and if I pass different image its crash.

推荐答案

尝试此代码,它逐位比较图像,即100%

try this code, It compares images bit by bit, ie 100%

UIImage *img1 = // Some photo;
UIImage *img2 = // Some photo;

NSData *imgdata1 = UIImagePNGRepresentation(img1);

NSData *imgdata2 = UIImagePNGRepresentation(img2);

if ([imgdata1 isEqualToData:imgdata2]) 
{
NSLog(@"Same Image");
}

这篇关于如何在iOS(iPhone)中使用opencv比较图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆