OpenCV进行图形匹配的方法,如若原图图中没有欲找的图,怎么设置返回错误..

OpenCV进行图形匹配的方法,如若原图图中没有欲找的图,怎么设置返回错误.....

https://zhidao.baidu.com/question/560478015170545444.html

OpenCV里面有一个模式匹配函数为:cvMatchTemplate,这个函数查找原图中有没有目标图,配合cvMinMaxLoc这个函数就可以得到在目标图在原图中的坐标。可是,我发现如果原图中没有目标图,还是会返回一个坐标的。后来发现这个是相似的坐标。请问,如何整它,让他在原图中查找,如果原图中包含该图,就返回坐标,如果没有,就提示出错。
还有,我查找了资料,发现cvMatchTemplate函数中设置result值,这个是返回的映射图形,是根据第四个参数的不同而改变的。我设置第四个参数为CV_TM_SQDIFF,也就是说完全匹配上了,result中有那么一个像素点该为0,可是为什么全是黑色啊,我无奈了......
求解求解......
收起
默风OnLine | 浏览 2770 次
我有更好的答案
推荐于2016-05-13 17:48:55 最佳答案

OpenCV封装了很多各种各样的匹配函数,如果想简单一点解决你的问题,你可以试试用chamerMatching函数。这个是C++程序,OpenCV2.0.0之后的版本应该都有(没有查,手头上只有2.4.4和2.4.6,都是有的)。顺便附上简单的代码,你可以试试。

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
 
#include <iostream>
 
using  namespace  cv;
using  namespace  std;
 
void  help()
{
  
    cout <<  "\nThis program demonstrates Chamfer matching -- computing a distance between an \n"
             "edge template and a query edge image.\n"
             "Usage: \n"
             "./chamfer <image edge map> <template edge map>,"
             " By default the inputs are logo_in_clutter.png logo.png\n" ;
}
 
const  char * keys = 
{
     "{1| |logo_in_clutter.png|image edge map    }"
     "{2| |logo.png               |template edge map}"
};
 
int  main(  int  argc,  const  char ** argv )
{
 
     help();
     CommandLineParser parser(argc, argv, keys);
 
     string image = parser.get<string>( "1" );
     string templ = parser.get<string>( "2" );
     Mat img = imread(image.c_str(), 0);
     Mat tpl = imread(templ.c_str(), 0);
 
     if  (img.empty() || tpl.empty())
     {
         cout <<  "Could not read image file "  << image <<  " or "  << templ <<  "."  << endl;
         return  -1;
     }
     Mat cimg;
     cvtColor(img, cimg, CV_GRAY2BGR);
 
     // if the image and the template are not edge maps but normal grayscale images,
     // you might want to uncomment the lines below to produce the maps. You can also
     // run Sobel instead of Canny.
 
     // Canny(img, img, 5, 50, 3);
     // Canny(tpl, tpl, 5, 50, 3);
 
     vector<vector<Point> > results;
     vector< float > costs;
     int  best = chamerMatching( img, tpl, results, costs );
     if ( best < 0 )
     {
         cout <<  "matching not found"  << endl;
         return  -1;
     }
 
     size_t  i, n = results[best].size();
     for ( i = 0; i < n; i++ )
     {
         Point pt = results[best][i];
         if ( pt.inside(Rect(0, 0, cimg.cols, cimg.rows)) )
            cimg.at<Vec3b>(pt) = Vec3b(0, 255, 0);
     }
 
     imshow( "result" , cimg);
 
     waitKey();
 
     return  0;
}

 忘了说了,chamerMatching的输出值如果小于0,表示没有发现匹配项。

追问
亲,能相信说明一下chamerMatching用法么?在百度上查找这个函数没有找到啊,有点郁闷。
追答
OpenCV手册中是这么介绍的:

This program demonstrates Chamfer matching -- computing a distance between an edge template and a query edge image.
contrib.hpp中的函数原型声明是这样的:
int chamerMatching( Mat& img, Mat& templ,
                                  vector<vector<Point> >& results, vector<float>& cost,
                                  double templScale=1, int maxMatches = 20,
                                  double minMatchDistance = 1.0, int padX = 3,
                                  int padY = 3, int scales = 5, double minScale = 0.6, double maxScale = 1.6,
                                  double orientationWeight = 0.5, double truncate = 20);
简单解释一下:
chamerMatching()是用来进行轮廓匹配的。源图像和目标图像都需要先提取出轮廓,例如canny算子。例如:源图像templ,目标图像img,best=chamerMatching(img,templ,results,cost)。results[]存放匹配结果。每个results[i]都是一系列点集构成的链表,例如result[1]={(120,120),(121,120),...},这些点集组成一幅轮廓图像。而best表示匹配效果最好的index,所以可以认为result[best]就是想要的结果。如果best小于0表示没有发现匹配项。
函数里面其他的默认参数一般可以不用改动。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值