Check presence of subimage in image in iOS [closed]

£可爱£侵袭症+ 提交于 2019-12-06 16:55:56

You can use OpennCV Template Matching algorithm..

please try it..

download opencv2.framework from https://github.com/Fl0p/OpenCV-iOS

download UIImage+OpenCV from https://github.com/aptogo/OpenCVForiPhone/tree/master/OpenCVClient

import files opencv2/nonfree/nonfree.hpp, opencv2/highgui/highgui.hpp, opencv2/calib3d/calib3d.hpp, UIImage+OpenCV.h

use this function for matching images.

-(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;
 }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!