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),不使用不传递即可.
//对视频进行模板匹配操作
#include <opencv2/opencv.hpp>
#include <iostream> #define MATCHMETHOD TM_CCOEFF_NORMED//宏定义匹配模式
using namespace cv;
using namespace std; Mat src,resultImage; int main(int argc, char** argv)
{
VideoCapture capture("F:/2019视觉培训内容/2019视觉培训内容/4buff1.avi");
Mat digita_l=imread("F:/2019视觉培训内容/2019视觉培训内容/数字识别数据集/数码管/1.png");
while (capture.read(src)) {
Mat frame;
src.copyTo(frame); //初始化输出矩阵
int resultImage_cols = src.cols - digita_l.cols + ;
int resultImage_rows = src.rows - digita_l.rows + ;
resultImage.create(resultImage_cols,resultImage_rows,CV_32FC1); //进行匹配和标准化
matchTemplate(src, digita_l, resultImage, MATCHMETHOD);//滑动模板,将匹配程度依此放在resultImage中
normalize(resultImage, resultImage, , , NORM_MINMAX, -, Mat()); //定位最匹配位置
double minValue, maxValue;
Point minLocation, maxLocation, matchLocation;
minMaxLoc(resultImage,&minValue,&maxValue,&minLocation,&maxLocation,Mat()); if(MATCHMETHOD== TM_SQDIFF|| MATCHMETHOD==TM_SQDIFF_NORMED)//这两种方法,值越小匹配读越高
{
matchLocation = minLocation;
}
else { matchLocation = maxLocation; } rectangle(frame, matchLocation, Point(matchLocation.x + digita_l.cols, matchLocation.y + digita_l.rows), Scalar(, , ), , , ); imshow("匹配结果", frame);
imshow("结果图", resultImage); char c = waitKey();//延时100ms
if (c == ) //等待“Esc”
{
break;
}
}
capture.release();//释放视频的内存
waitKey(); return ;
}