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 and templ is , thenresult is .
- method – Parameter specifying the comparison method (see below).
查找数组中的全局最小值和最大值: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
- OpenCV学习笔记(十七)模板匹配
- OpenCV 学习笔记(模板匹配)
- opencv学习笔记(三十二)模板匹配
- 【OpenCV学习笔记】【算法学习】一(模板匹配)
- OpenCV学习笔记(十三)——模板匹配
- OpenCV学习笔记(十三)——模板匹配
- Opencv Python版学习笔记(三)模板匹配
- OpenCV学习笔记(5)——模板匹配
- OpenCV学习笔记[4]模板匹配
- Python+OpenCV学习(7)---模板匹配
- 【openCV笔记1】模板匹配
- opencv笔记之模板匹配
- OpenCV 2 学习笔记(26): 自选区域进行模板匹配
- 【OpenCV学习笔记】【函数学习】十七(imread()函数)
- opencv学习之模板匹配
- Opencv学习之模板匹配
- OpenCV学习2 模板匹配
- opencv学习笔记(十七)——线性滤波
- 创建自定义的jsp标签
- torch入门笔记21:xavier初始化方法
- 精通 CSS+DIV 网页样式与布局 169
- oracle字母和数字,小数点,数字符号组合排序(注意:难度比前一篇oracle字母和数字组合排序加大)
- HTML中filter使用
- OpenCV学习笔记(十七)模板匹配
- jsp foreach 循环 嵌套外层函数
- 精通 CSS+DIV 网页样式与布局 170
- 理解websocket和socket.io
- JQUEY 格式化表单数据转JSON格式
- 把e.printStackTrace的堆栈信息打印在log.error()中
- iOS 如何返回到指定界面?
- c++queue容器
- 26.OpenGL--分格化