OpenCV学习笔记(十七)模板匹配

来源:互联网 发布:淘宝查号网站 编辑:程序博客网 时间:2024/05/01 19:06

我的目的就是选取图像中的指示灯,识别那种功能的指示灯亮了,进而知道那种功能打开了。

模板匹配:

模板匹配是傻瓜似的识别,从一副图像中寻找与模板图像相似部分的技术。模板匹配由matchTemplate()函数完成。

介绍两个函数,matchTemplate()和minMaxLoc()函数:

void matchTemplate(InputArray image, InputArray temp, OutputArray result, int method)

  • image – Image where the search is running. It must be 8-bit or 32-bit floating-point.
  • templ – Searched template. It must be not greater than the source image and have the same data type.
  • result – Map of comparison results. It must be single-channel 32-bit floating-point. If image is W \times H and templ is w \times h , thenresult is (W-w+1) \times (H-h+1) .
  • method – Parameter specifying the comparison method (see below).
method:CV_TM_SQDIFF_NORMED,CV_TM_CCORR,CV_TM_CCORR_NORMED,CV_TM_CCOEFF,CV_TM_CCOEFF_NORMED

查找数组中的全局最小值和最大值:minMaxLoc()函数

void minMaxLoc(InputArray src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, InputArray mask=noArray())

  • src – Source single-channel array.
  • minVal – Pointer to the returned minimum value. NULL is used if not required.
  • maxVal – Pointer to the returned maximum value. NULL is used if not required.
  • minLoc – Pointer to the returned minimum location (in 2D case). NULL is used if not required.
  • maxLoc – Pointer to the returned maximum location (in 2D case). NULL is used if not required.
  • mask – Optional mask used to select a sub-array.
程序实现:

#include <opencv2/highgui/highgui.hpp>#include <opencv2/imgproc/imgproc.hpp>using namespace cv;#define WINDOW_NAME1 "【原始图片】"Mat g_srcImage, g_templateImage, g_resultImage;int g_nMatchMethod=3;//int g_nMaxTrackbarNum = 5;void on_Matching(int, void*);int main(){g_srcImage = imread("D:\\Projects\\1.jpg");g_templateImage = imread("D:\\Projects\\11.jpg");namedWindow(WINDOW_NAME1, CV_WINDOW_AUTOSIZE);/*createTrackbar("方法", WINDOW_NAME1, &g_nMatchMethod, g_nMaxTrackbarNum, on_Matching);*/on_Matching(0, 0);waitKey(0);return 0;}void on_Matching(int, void*){Mat srcImage;g_srcImage.copyTo(srcImage);//初始化用于结果输出的矩阵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);//进行匹配和标准化matchTemplate(g_srcImage, g_templateImage, g_resultImage, g_nMatchMethod);normalize(g_resultImage, g_resultImage, 0, 1, NORM_MINMAX, -1, Mat());//通过哦函数minMaxLoc定位最匹配位置double minValue, maxValue;Point minLocation, maxLocation;Point matchLocation;minMaxLoc(g_resultImage, &minValue, &maxValue, &minLocation, &maxLocation, Mat());//对于方法SQDIFF和SQDIFF_NORMED,越小的数值有着更高的匹配结果,而其余的方法,数值越大匹配效果越好if (g_nMatchMethod == CV_TM_CCORR_NORMED){maxLocation.x = maxLocation.x + 48;maxLocation.y = maxLocation.y + 2;matchLocation = maxLocation;}//绘制出矩形,并显示最终结果rectangle(srcImage, matchLocation, Point(matchLocation.x + g_templateImage.cols - 10, matchLocation.y + g_templateImage.rows - 10),Scalar(255, 255, 255), 1, 8, 0);imshow(WINDOW_NAME1, srcImage);}
模板图像:

原图像:



效果图:



使用模板匹配是选取了就是合位的指示灯,需要的不断的调整,实用性差,选取不准确,又难以多个选取。

0 0
原创粉丝点击