OpenCV里面有一个模式匹配函数为:cvMatchTemplate,这个函数查找原图中有没有目标图,配合cvMinMaxLoc这个函数就可以得到在目标图在原图中的坐标。可是,我发现如果原图中没有目标图,还是会返回一个坐标的。后来发现这个是相似的坐标。请问,如何整它,让他在原图中查找,如果原图中包含该图,就返回坐标,如果没有,就提示出错。
还有,我查找了资料,发现cvMatchTemplate函数中设置result值,这个是返回的映射图形,是根据第四个参数的不同而改变的。我设置第四个参数为CV_TM_SQDIFF,也就是说完全匹配上了,result中有那么一个像素点该为0,可是为什么全是黑色啊,我无奈了......
求解求解......
OpenCV进行图形匹配的方法,如若原图图中没有欲找的图,怎么设置返回错误.....
答案:3 悬赏:10
解决时间 2021-03-06 18:30
- 提问者网友:说不出醉人情话
- 2021-03-06 02:06
最佳答案
- 二级知识专家网友:高冷不撩人
- 2021-03-06 03:39
OpenCV封装了很多各种各样的匹配函数,如果想简单一点解决你的问题,你可以试试用chamerMatching函数。这个是C++程序,OpenCV2.0.0之后的版本应该都有(没有查,手头上只有2.4.4和2.4.6,都是有的)。顺便附上简单的代码,你可以试试。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <iostream>
usingnamespacecv;
usingnamespacestd;
voidhelp()
{
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";
}
constchar* keys =
{
"{1| |logo_in_clutter.png|image edge map }"
"{2| |logo.png |template edge map}"
};
intmain( intargc, constchar** 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;
intbest = chamerMatching( img, tpl, results, costs );
if( best < 0 )
{
cout << "matching not found"<< endl;
return-1;
}
size_ti, 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();
return0;
}
忘了说了,chamerMatching的输出值如果小于0,表示没有发现匹配项。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"
#include <iostream>
usingnamespacecv;
usingnamespacestd;
voidhelp()
{
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";
}
constchar* keys =
{
"{1| |logo_in_clutter.png|image edge map }"
"{2| |logo.png |template edge map}"
};
intmain( intargc, constchar** 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;
intbest = chamerMatching( img, tpl, results, costs );
if( best < 0 )
{
cout << "matching not found"<< endl;
return-1;
}
size_ti, 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();
return0;
}
忘了说了,chamerMatching的输出值如果小于0,表示没有发现匹配项。
全部回答
- 1楼网友:承载所有颓废
- 2021-03-06 05:36
程序会给出每次识别的类似相似度的值,可以通过和正确图的值比较判断是否为正确图
- 2楼网友:安稳不如野
- 2021-03-06 04:54
搜一下:OpenCV进行图形匹配的方法,如若原图图中没有欲找的图,怎么设置返回错误.....
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯