机器视觉OpenCVSharp-模板匹配 Cv2.MatchTemplate()
Mat originalMat = new Mat(@"D:\Users\FengJianming\C#\OpenCVSharp\ OpenCVSharp\original.jpg", ImreadModes.AnyColor); //母图
Mat modelMat=new Mat(@"D:\Users\FengJianming\C#\OpenCVSharp\OpenCVSharp\model.jpg",ImreadModes.AnyColor); //模板
Mat resultMat =new Mat();
resultMat.Create(mat1.Cols - modelMat.Cols+ 1, mat1.Rows - modelMat.Cols + 1,MatType.CV_32FC1);//创建result的模板,就是MatchTemplate里的第三个参数
CV2.MatchTemplate(originalMat, modelMat, resultMat, TemplateMatchModes.SqDiff);//进行匹配(1母图,2模版子图,3返回的result,4匹配模式)
OpenCvSharp.Point minLocation,maxLocation;
CV2.MinMaxLoc(resultMat,out minLocation, out maxLocation);
CV2.Rectangle(originalMat,minLocation,new OpenCvSharp.Point(minLocation.X + modelMat. Cols,minLocation.Y + modelMat.Rows), Scalar.Red, 2); //画出匹配的矩
CV2.ImShow("母图", originalMat);
CV2.ImShow("模板", modelMat);