minMaxLoc函数:
void minMaxLoc( const Mat& src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0, const Mat& mask=Mat() );
说明:
1 minMaxLoc寻找矩阵(一维数组当作向量,用Mat定义) 中最小值和最大值的位置.
2 参数若不需要,则置为NULL或者0,即可.
3 minMaxLoc针对Mat和MatND的重载中 ,第5个参数是可选的(optional),不使用不传递即可.
1 //对视频进行模板匹配操作 2 #include <opencv2/opencv.hpp> 3 #include <iostream> 4 5 #define MATCHMETHOD TM_CCOEFF_NORMED//宏定义匹配模式 6 using namespace cv; 7 using namespace std; 8 9 Mat src,resultImage; 10 11 int main(int argc, char** argv) 12 { 13 VideoCapture capture("F:/2019视觉培训内容/2019视觉培训内容/4buff1.avi"); 14 Mat digita_l=imread("F:/2019视觉培训内容/2019视觉培训内容/数字识别数据集/数码管/1.png"); 15 while (capture.read(src)) { 16 Mat frame; 17 src.copyTo(frame); 18 19 //初始化输出矩阵 20 int resultImage_cols = src.cols - digita_l.cols + 1; 21 int resultImage_rows = src.rows - digita_l.rows + 1; 22 resultImage.create(resultImage_cols,resultImage_rows,CV_32FC1); 23 24 //进行匹配和标准化 25 matchTemplate(src, digita_l, resultImage, MATCHMETHOD);//滑动模板,将匹配程度依此放在resultImage中 26 normalize(resultImage, resultImage, 0, 1, NORM_MINMAX, -1, Mat()); 27 28 //定位最匹配位置 29 double minValue, maxValue; 30 Point minLocation, maxLocation, matchLocation; 31 minMaxLoc(resultImage,&minValue,&maxValue,&minLocation,&maxLocation,Mat()); 32 33 if(MATCHMETHOD== TM_SQDIFF|| MATCHMETHOD==TM_SQDIFF_NORMED)//这两种方法,值越小匹配读越高 34 { 35 matchLocation = minLocation; 36 } 37 else { matchLocation = maxLocation; } 38 39 rectangle(frame, matchLocation, Point(matchLocation.x + digita_l.cols, matchLocation.y + digita_l.rows), Scalar(0, 0, 255), 2, 8, 0); 40 41 imshow("匹配结果", frame); 42 imshow("结果图", resultImage); 43 44 char c = waitKey(100);//延时100ms 45 if (c == 27) //等待“Esc” 46 { 47 break; 48 } 49 } 50 capture.release();//释放视频的内存 51 waitKey(0); 52 53 return 0; 54 }