opencv多对象匹配-模板匹配算法c++实现

#include<opencv2/opencv.hpp>
#include<opencv2/highgui.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/core.hpp>
#include<math.h>
#include<iostream>
#include<vector>

using namespace std;
using namespace cv;




int main(int argc, char** argv)
{
	Mat result;
	Mat src = imread("mario.png", IMREAD_COLOR);
	Mat templ = imread("cin.png", IMREAD_COLOR);
	int match_method = TM_CCOEFF_NORMED;
	if (src.empty() || templ.empty())
	{
		cout << "加载图片失败" << endl;
		return -1;
	}
	int srcW, srcH, templatW, templatH;
	srcW = src.cols;
	srcH = src.rows;
	templatW = templ.cols;
	templatH = templ.rows;
	if (srcW < templatW || srcH < templatH)
	{
		cout << "模板不能比原图小" << endl;
		return -1;
	}
	int resultW = srcW - templatW + 1;
	int resultH = srcH - templatH + 1;
	result.create(Size(resultW, resultH), CV_32FC1);
	matchTemplate(src, templ, result, match_method);	
	cout << "depth:" << result.depth() << endl;
	cout << "type:" << result.type() << endl;
	cout << "channels:" << result.channels() << endl;
	cout << "total:" << result.total() << endl;


	Point matchLoc;
	int count = 0;	


	if (result.channels() == 1)
	{
		MatIterator_<float> it_begin, it_end;

		for (it_begin = result.begin<float>(), it_end = result.end<float>(); it_begin != it_end; it_begin++)
		{
						
			if (*it_begin >= 0.8)
			{
				count++;
				matchLoc = it_begin.pos();

				rectangle(src, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar(255, 0, 0), 2, 8, 0);
			}
		}
		cout <<"count = "<<count << endl;
	}
	
	namedWindow("output", WINDOW_AUTOSIZE);
	moveWindow("output", src.cols, 0);
	imshow("output", src);
	imshow("result", result);
	waitKey(0);

	return 0;
}

运行结果:
opencv模板匹配

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值