模板匹配
#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" using namespace cv; //-----------------------------------【宏定义部分】-------------------------------------------- // 描述:定义一些辅助宏 //------------------------------------------------------------------------------------------------ #define WINDOW_NAME1 "【原始图片】" //为窗口标题定义的宏 #define WINDOW_NAME2 "【匹配窗口】" //为窗口标题定义的宏 //-----------------------------------【全局变量声明部分】------------------------------------ // 描述:全局变量的声明 //----------------------------------------------------------------------------------------------- Mat g_srcImage; Mat g_templateImage; Mat g_resultImage; int g_nMatchMethod; int g_nMaxTrackbarNum = 5; //-----------------------------------【全局函数声明部分】-------------------------------------- // 描述:全局函数的声明 //----------------------------------------------------------------------------------------------- void on_Matching(int, void*); static void ShowHelpText(); //-----------------------------------【main( )函数】-------------------------------------------- // 描述:控制台应用程序的入口函数,我们的程序从这里开始执行 //----------------------------------------------------------------------------------------------- int main() { //【0】显示帮助文字 ShowHelpText(); //【1】载入原图像和模板块 g_srcImage = imread("8.jpg", 1); g_templateImage = imread("9.jpg", 1); //【2】创建窗口 namedWindow(WINDOW_NAME1, WINDOW_AUTOSIZE); namedWindow(WINDOW_NAME2, WINDOW_AUTOSIZE); //【3】创建滑动条并进行一次初始化 createTrackbar("方法", WINDOW_NAME1, &g_nMatchMethod, g_nMaxTrackbarNum, on_Matching); on_Matching(0, 0); waitKey(0); return 0; } //-----------------------------------【on_Matching( )函数】-------------------------------- // 描述:回调函数 //------------------------------------------------------------------------------------------- void on_Matching(int, void*) { //【1】给局部变量初始化 Mat srcImage; g_srcImage.copyTo(srcImage); //【2】初始化用于结果输出的矩阵 int resultImage_cols = g_srcImage.cols - g_templateImage.cols + 1; int resultImage_rows = g_srcImage.rows - g_templateImage.rows + 1; g_resultImage.create(resultImage_cols, resultImage_rows, CV_32FC1); //【3】进行匹配和标准化 matchTemplate(g_srcImage, g_templateImage, g_resultImage, g_nMatchMethod); normalize(g_resultImage, g_resultImage, 0, 1, NORM_MINMAX, -1, Mat()); //【4】通过函数 minMaxLoc 定位最匹配的位置 double minValue; double maxValue; Point minLocation; Point maxLocation; Point matchLocation; minMaxLoc(g_resultImage, &minValue, &maxValue, &minLocation, &maxLocation, Mat()); //【5】对于方法 SQDIFF 和 SQDIFF_NORMED, 越小的数值有着更高的匹配结果. 而其余的方法, 数值越大匹配效果越好 //此句代码的OpenCV2版为: //if( g_nMatchMethod == CV_TM_SQDIFF || g_nMatchMethod == CV_TM_SQDIFF_NORMED ) //此句代码的OpenCV3版为: if (g_nMatchMethod == TM_SQDIFF || g_nMatchMethod == TM_SQDIFF_NORMED) { matchLocation = minLocation; } else { matchLocation = maxLocation; } //【6】绘制出矩形,并显示最终结果 rectangle(srcImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0); rectangle(g_resultImage, matchLocation, Point(matchLocation.x + g_templateImage.cols, matchLocation.y + g_templateImage.rows), Scalar(0, 0, 255), 2, 8, 0); imshow(WINDOW_NAME1, srcImage); imshow(WINDOW_NAME2, g_resultImage); } //-----------------------------------【ShowHelpText( )函数】---------------------------------- // 描述:输出一些帮助信息 //---------------------------------------------------------------------------------------------- static void ShowHelpText() { //输出欢迎信息和OpenCV版本 printf("\n\n\t\t\t非常感谢购买《OpenCV3编程入门》一书!\n"); printf("\n\n\t\t\t此为本书OpenCV3版的第84个配套示例程序\n"); printf("\n\n\t\t\t 当前使用的OpenCV版本为:" CV_VERSION); printf("\n\n ----------------------------------------------------------------------------\n"); //输出一些帮助信息 printf("\t欢迎来到【模板匹配】示例程序~\n"); printf("\n\n\t请调整滑动条观察图像效果\n\n"); printf("\n\t滑动条对应的方法数值说明: \n\n" "\t\t方法【0】- 平方差匹配法(SQDIFF)\n" "\t\t方法【1】- 归一化平方差匹配法(SQDIFF NORMED)\n" "\t\t方法【2】- 相关匹配法(TM CCORR)\n" "\t\t方法【3】- 归一化相关匹配法(TM CCORR NORMED)\n" "\t\t方法【4】- 相关系数匹配法(TM COEFF)\n" "\t\t方法【5】- 归一化相关系数匹配法(TM COEFF NORMED)\n"); }
努力的意义就是放眼望去以后都是喜欢的人和事......