我正在努力解决这个问题,因为在我从相机捕获的图像中,我需要找到某些图案的存在和位置。
为此我发现使用matchTemplate
opencv的方法。我使用了opencv示例中使用的图像并编写了代码,但结果不同。
http://opencv.itseez.com/doc/tutorials/imgproc/histograms/template_matching/template_matching.html http://opencv.itseez.com/doc/tutorials/imgproc/histograms/template_matching/template_matching.html
这是告诉我们关于 matchTemplate 的链接。
当我实现它时,它显示了结果:-
我的代码如下:-
-(void)matchPatchNet
{
IplImage *res;
CvPoint minloc, maxloc;
double minval, maxval;
int img_width, img_height;
int tpl_width, tpl_height;
int res_width, res_height;
NSString *pathPatron = [[NSBundle mainBundle] pathForResource:@"timage" ofType:@"jpg"];
UIImage *tim = [UIImage imageWithContentsOfFile:pathPatron];
NSString *pathPatron2 = [[NSBundle mainBundle] pathForResource:@"simage" ofType:@"jpg"];
UIImage *tim2 = [UIImage imageWithContentsOfFile:pathPatron2];
IplImage *img = [self CreateIplImageFromUIImage:tim2];//
IplImage *tpl = [self CreateIplImageFromUIImage:tim];
cv::Mat forground1 = [tim2 CVMat];
cv::Mat forground2 = [tim CVMat];
img_width = img->width;
img_height = img->height;
tpl_width = tpl->width;
tpl_height = tpl->height;
res_width = img_width - tpl_width + 1;
res_height = img_height - tpl_height + 1;
res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );
cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED );
UIImage *ipala=[self UIImageFromIplImage:res];
cv::Mat forground3 = [ipala CVMat];
cv::normalize(forground3, forground3, 0, 1, cv::NORM_MINMAX, CV_8UC1);
cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
cvRectangle( img,
cvPoint( maxloc.x, maxloc.y ),
cvPoint( maxloc.x + tpl_width, maxloc.y + tpl_height ),
cvScalar( 0, 255, 0, 0 ), 1, 0, 0 );
/* display images */
self.imageView.image = [self UIImageFromIplImage:img];
cvReleaseImage(&img);
cvReleaseImage(&tpl);
cvReleaseImage(&res);
}
请告诉我我做错了什么。请帮助我。
提前致谢