检查iOS中图像中子图像的存在

时间:2013-12-13 08:29:10

标签: ios opencv machine-learning computer-vision image-comparison

我有2张图片,比如smallImage和largeImage。现在,我想检查一下tinyImage是否是largeImage的一部分(或子图像)。我没有得到如何检查。我想在iOS中完成这一切。在gogling之后,我得到了使用openCV库是最好的库用于这样的东西。但我没有得到如何使用openCV库来执行我的任务。

总之,我想知道,

1)在较大图像中查找/检查子图像有哪些不同的技术/方法/算法

2)如何使用openCV库执行(1)。(即如何使用它来检查较大图像中的子图像)

1 个答案:

答案 0 :(得分:5)

您可以使用OpennCV模板匹配算法..

请试试..

https://github.com/Fl0p/OpenCV-iOS

下载opencv2.framework

https://github.com/aptogo/OpenCVForiPhone/tree/master/OpenCVClient

下载UIImage + OpenCV

导入文件opencv2 / nonfree / nonfree.hpp,opencv2 / highgui / highgui.hpp,opencv2 / calib3d / calib3d.hpp,UIImage + OpenCV.h

使用此功能匹配图像。

-(BOOL) matchImages:(UIImage*)largerImage Image2:(UIImage*)subImage
{

 cv::Mat tempMat1 = [largerImage CVMat];
 cv::Mat tempMat2 = [subImage CVMat];

 cv::Mat result;


 int match_method = CV_TM_SQDIFF_NORMED;
 //cv::cvtColor(tempMat1, debug, CV_GRAY2BGR);
 //cv::cvtColor(tempMat1, tempMat1, cv::COLOR_RGB2GRAY);
 //cv::cvtColor(tempMat2, tempMat2, cv::COLOR_RGB2GRAY);

 int result_cols =  tempMat1.cols - tempMat2.cols + 1;
 int result_rows = tempMat1.rows - tempMat2.rows + 1;

 result.create( result_cols, result_rows, CV_32FC1 );


 /// Do the Matching and Normalize
 cv::matchTemplate( tempMat1,tempMat2 ,result,match_method);

 double minVal; double maxVal;
 cv::Point minLoc, maxLoc, matchLoc;
 cv::minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat() );
 if( match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED ) matchLoc = minLoc;
 else matchLoc = maxLoc;

//NSLog(@"%d %d",tempMat1.cols,tempMat1.rows);
NSLog(@"%f %f",minVal,maxVal);

 if (minVal < 0.25) {

     NSLog(@"success");

     //cv::rectangle(tempMat1,matchLoc,cv::Point(matchLoc.x + tempMat2.cols , matchLoc.y + tempMat2.rows),CV_RGB(255,0,0),3);

     //UIImage *resImage = [[UIImage alloc] initWithCVMat:tempMat1];
     //UIImageView * imageview = [[UIImageView alloc] initWithImage:resImage];
     //[imageview setFrame:CGRectMake(0, 0, resImage.size.width, resImage.size.height)];
     //[self.view addSubview:imageview];

     return YES;
 }
 else {

    return NO;
 }
}