gpt4 book ai didi

iphone - 如何在 iOS (iPhone) 中使用 opencv 比较图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:39 25 4
gpt4 key购买 nike

我想在我的项目中比较 iPhone 相机拍摄的 2 张图像。我正在使用 OpenCV 来做这件事。还有其他更好的方法吗?如果我得到相似度百分比,那就太好了。

我正在使用 OpenCV 以下代码进行图像比较:

-(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 ...),如果我传递不同的图像,它会崩溃。

最佳答案

试试这段代码,它逐位比较图像,即100%

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

NSData *imgdata1 = UIImagePNGRepresentation(img1);

NSData *imgdata2 = UIImagePNGRepresentation(img2);

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

关于iphone - 如何在 iOS (iPhone) 中使用 opencv 比较图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15742958/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com