1. 程式人生 > >OpenCV3模板匹配

OpenCV3模板匹配

模板匹配,就是在一幅影象中尋找另一幅模板影象最匹配(也就是最相似)的部分的技術。
通過在輸入影象image上滑動影象塊,對實際的影象塊和模板影象templ進行匹配。

單目標匹配

#include "pch.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv; int main() { Mat img, templ, result; img = imread("green.jpg"); templ = imread("football.jpg"); //1.構建結果影象resultImg(注意大小和型別) //如果原圖(待搜尋影象)尺寸為W x H, 而模版尺寸為 w x h, 則結果影象尺寸一定是(W-w+1)x(H-h+1) //結果影象必須為單通道32位浮點型影象 int result_cols = img.cols - templ.cols + 1; int result_rows = img.
rows - templ.rows + 1; result.create(result_cols, result_rows, CV_32FC1); //2.模版匹配 //這裡我們使用的匹配演算法是標準平方差匹配 method=CV_TM_SQDIFF_NORMED,數值越小匹配度越好 matchTemplate(img, templ, result, CV_TM_SQDIFF_NORMED); //3.正則化(歸一化到0-1) normalize(result, result, 0, 1, NORM_MINMAX, -1, Mat()); //4.找出resultImg中的最大值及其位置 double
minVal = -1; double maxVal; Point minLoc; Point maxLoc; Point matchLoc; cout << "匹配度:" << minVal << endl; // 定位極值的函式 minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, Mat()); cout << "匹配度:" << minVal << endl; cout << "minPosition: " << minLoc << endl; cout << "maxPosition: " << maxLoc << endl; matchLoc = minLoc; //5.根據resultImg中的最大值位置在源圖上畫出矩形和中心點 Point center = Point(minLoc.x + templ.cols / 2, minLoc.y + templ.rows / 2); rectangle(img, matchLoc, Point(matchLoc.x + templ.cols, matchLoc.y + templ.rows), Scalar(0, 255, 0), 2, 8, 0); circle(img, center, 2, Scalar(255, 0, 0), 2); imshow("img", img); imshow("template", templ); waitKey(0); return 0; }

結果:

多目標模板匹配

///多目標模板匹配
#include "pch.h"
#include <opencv2/opencv.hpp>
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
	Mat srcImg = imread("screen.png", CV_LOAD_IMAGE_COLOR);
	Mat tempImg = imread("line.jpg", CV_LOAD_IMAGE_COLOR);
	//1.構建結果影象resultImg(注意大小和型別)
	//如果原圖(待搜尋影象)尺寸為W x H, 而模版尺寸為 w x h, 則結果影象尺寸一定是(W-w+1)x(H-h+1)
	//結果影象必須為單通道32位浮點型影象
	int width = srcImg.cols - tempImg.cols + 1;
	int height = srcImg.rows - tempImg.rows + 1;
	Mat resultImg(Size(width, height), CV_32FC1);
	//2.模版匹配
	matchTemplate(srcImg, tempImg, resultImg, CV_TM_CCOEFF_NORMED);
	imshow("result", resultImg);
	//3.正則化(歸一化到0-1)
	normalize(resultImg, resultImg, 0, 1, NORM_MINMAX, -1);
	//4.遍歷resultImg,給定篩選條件,篩選出前幾個匹配位置
	int tempX = 0;
	int tempY = 0;
	char prob[10] = { 0 };
	//4.1遍歷resultImg
	for (int i = 0; i < resultImg.rows;i++)
	{
		for (int j = 0; j < resultImg.cols; j++)
		{
			//4.2獲得resultImg中(j,x)位置的匹配值matchValue
			double matchValue = resultImg.at<float>(i, j);
			//sprintf(prob, "%.2f", matchValue);
			//4.3給定篩選條件
			//條件1:概率值大於0.9
			//條件2:任何選中的點在x方向和y方向上都要比上一個點大5(避免畫邊框重影的情況)
			if (matchValue > 0.9&& abs(i - tempY) > 5 && abs(j - tempX) > 5)
			{
				//5.給篩選出的點畫出邊框和文字
				rectangle(srcImg, Point(j, i), Point(j + tempImg.cols, i + tempImg.rows), Scalar(0, 255, 0), 1, 8);
				putText(srcImg, prob, Point(j, i + 100), CV_FONT_BLACK, 1, Scalar(0, 0, 255), 1);
				tempX = j;
				tempY = i;
			}
		}
	}
	imshow("srcImg", srcImg);
	imshow("template", tempImg);
	waitKey(0);
	return 0;
}

視訊單目標匹配

///視訊單目標模板匹配
#include "pch.h"
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
	//1.定義VideoCapture類物件video,讀取視訊
	VideoCapture video("1.mp4");
	//1.1.判斷視訊是否開啟
	if (!video.isOpened())
	{
		cout << "video open error!" << endl;
		return 0;
	}
	//2.迴圈讀取視訊的每一幀,對每一幀進行模版匹配
	while (1)
	{
		//2.1.讀取幀
		Mat frame;
		video >> frame;
		//2.2.對幀進行異常檢測
		if (frame.empty())
		{
			cout << "frame empty" << endl;
			break;
		}
		//2.3.對幀進行模版匹配
		Mat tempImg = imread("green.JPG", CV_LOAD_IMAGE_COLOR);
		cout << "Size of template: " << tempImg.size() << endl;
		//2.3.1.構建結果影象resultImg(注意大小和型別)
		//如果原圖(待搜尋影象)尺寸為W x H, 而模版尺寸為 w x h, 則結果影象尺寸一定是(W-w+1)x(H-h+1)
		//結果影象必須為單通道32位浮點型影象
		int width = frame.cols - tempImg.cols + 1;
		int height = frame.rows - tempImg.rows + 1;
		Mat resultImg(Size(width, height), CV_32FC1);
		//2.3.2.模版匹配
		matchTemplate(frame, tempImg, resultImg, CV_TM_CCOEFF_NORMED);
		imshow("result", resultImg);
		//2.3.3.正則化(歸一化到0-1)
		normalize(resultImg, resultImg, 0, 1, NORM_MINMAX, -1);
		//2.3.4.找出resultImg中的最大值及其位置
		double minValue = 0;
		double maxValue = 0;
		Point minPosition;
		Point maxPosition;
		minMaxLoc(resultImg, &minValue, &maxValue, &minPosition, &maxPosition);
		cout << "minValue: " << minValue << endl;
		cout << "maxValue: " << maxValue << endl;
		cout << "minPosition: " << minPosition << endl;
		cout << "maxPosition: " << maxPosition << endl;
		//2.3.5.根據resultImg中的最大值位置在源圖上畫出矩形
		rectangle(frame, maxPosition, Point(maxPosition.x + tempImg.cols, maxPosition.y + tempImg.rows), Scalar(0, 255, 0), 1, 8);
		imshow("srcImg", frame);
		imshow("template", tempImg);
		if (waitKey(10) == 27)
		{
			cout << "ESC退出" << endl;
			break;
		};
	}
	return 0;
}

參考:
https://www.cnblogs.com/skyfsm/p/6884253.html
https://blog.csdn.net/abc8730866/article/details/68487029
https://www.w3cschool.cn/opencv/opencv-pswj2dbc.html