EMGU.CV入门(十五、模板匹配)

22 篇文章 29 订阅
21 篇文章 0 订阅

一、函数介绍

1.1 MatchTemplate

模板匹配函数
参数说明
参数1:输入图像
参数2:匹配模板
参数3:返回矩阵
参数4:算法类型
其中算法类型共计六种:
在这里插入图片描述
在这里插入图片描述

        //
        // 摘要:
        //     This function is similiar to cvCalcBackProjectPatch. It slids through image,
        //     compares overlapped patches of size wxh with templ using the specified method
        //     and stores the comparison results to result
        //
        // 参数:
        //   image:
        //     Image where the search is running. It should be 8-bit or 32-bit floating-point
        //
        //   templ:
        //     Searched template; must be not greater than the source image and the same data
        //     type as the image
        //
        //   result:
        //     A map of comparison results; single-channel 32-bit floating-point. If image is
        //     WxH and templ is wxh then result must be W-w+1xH-h+1.
        //
        //   method:
        //     Specifies the way the template must be compared with image regions
        //
        //   mask:
        //     Mask of searched template. It must have the same datatype and size with templ.
        //     It is not set by default.
        public static void MatchTemplate(IInputArray image, IInputArray templ, IOutputArray result, TemplateMatchingType method, IInputArray mask = null)

1.2 MinMaxLoc

参数1:输入MatchTemplate函数返回的矩阵
参数2、3、4、5:分别为最小值、最大值、最小值的位置、最大值的位置

        //
        // 摘要:
        //     Finds minimum and maximum element values and their positions. The extremums are
        //     searched over the whole array, selected ROI (in case of IplImage) or, if mask
        //     is not IntPtr.Zero, in the specified array region. If the array has more than
        //     one channel, it must be IplImage with COI set. In case if multi-dimensional arrays
        //     min_loc->x and max_loc->x will contain raw (linear) positions of the extremums
        //
        // 参数:
        //   arr:
        //     The source array, single-channel or multi-channel with COI set
        //
        //   minVal:
        //     Pointer to returned minimum value
        //
        //   maxVal:
        //     Pointer to returned maximum value
        //
        //   minLoc:
        //     Pointer to returned minimum location
        //
        //   maxLoc:
        //     Pointer to returned maximum location
        //
        //   mask:
        //     The optional mask that is used to select a subarray. Use IntPtr.Zero if not needed
        public static void MinMaxLoc(IInputArray arr, ref double minVal, ref double maxVal, ref Point minLoc, ref Point maxLoc, IInputArray mask = null)

1.3 Rectangle

绘制矩形

        //
        // 摘要:
        //     Draws a rectangle specified by a CvRect structure
        //
        // 参数:
        //   img:
        //     Image
        //
        //   rect:
        //     The rectangle to be drawn
        //
        //   color:
        //     Line color
        //
        //   thickness:
        //     Thickness of lines that make up the rectangle. Negative values make the function
        //     to draw a filled rectangle.
        //
        //   lineType:
        //     Type of the line
        //
        //   shift:
        //     Number of fractional bits in the point coordinates
        public static void Rectangle(IInputOutputArray img, Rectangle rect, MCvScalar color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0

二、单匹配

2.1 效果

在这里插入图片描述

2.2 代码

 // 1. 加载原图
var image1 = new Image<Bgr, byte>("bird1.png");
var image0 = image1.Mat.Clone();
var imgGray = new Mat();
CvInvoke.CvtColor(image0,imgGray,ColorConversion.Bgr2Gray);
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));

// 2. 原图转灰度
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(Text(imgGray.Bitmap, "灰度")));

// 3. 加载模板
var img3 = new Mat("birdTemplate.png",0);
PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(Text(img3.Bitmap, "模板")));

// 需要用到的一些参数
var res = new Mat();
double minLoc = 0, maxLoc = 0;
Point minPoint = new Point();
Point maxPoint = new Point();

// 4. Sqdiff取最小值
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.Sqdiff);
CvInvoke.MinMaxLoc(res,ref minLoc,ref maxLoc, ref minPoint,ref maxPoint);
var img4 = image0.Clone();
CvInvoke.Rectangle(img4, new Rectangle(minPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage4 = new WriteableBitmap(Bitmap2BitmapImage(Text(img4.Bitmap, "Sqdiff")));

// 5 .SqdiffNormed
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.SqdiffNormed);
CvInvoke.MinMaxLoc(res, ref minLoc, ref maxLoc, ref minPoint, ref maxPoint);
var img5 = image0.Clone();
CvInvoke.Rectangle(img5, new Rectangle(minPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage7 = new WriteableBitmap(Bitmap2BitmapImage(Text(img5.Bitmap, "SqdiffNormed")));

// 6 .Ccoeff
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.Ccoeff);
CvInvoke.MinMaxLoc(res, ref minLoc, ref maxLoc, ref minPoint, ref maxPoint);
var img6 = image0.Clone();
CvInvoke.Rectangle(img6, new Rectangle(maxPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage5 = new WriteableBitmap(Bitmap2BitmapImage(Text(img6.Bitmap, "Ccoeff")));

// 7 .CcoeffNormed
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.CcoeffNormed);
CvInvoke.MinMaxLoc(res, ref minLoc, ref maxLoc, ref minPoint, ref maxPoint);
var img7 = image0.Clone();
CvInvoke.Rectangle(img7, new Rectangle(maxPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage8 = new WriteableBitmap(Bitmap2BitmapImage(Text(img7.Bitmap, "CcoeffNormed")));


// 8 .Ccorr
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.Ccorr);
CvInvoke.MinMaxLoc(res, ref minLoc, ref maxLoc, ref minPoint, ref maxPoint);
var img8 = image0.Clone();
CvInvoke.Rectangle(img8, new Rectangle(maxPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage6 = new WriteableBitmap(Bitmap2BitmapImage(Text(img8.Bitmap, "Ccorr")));


// 9 .CcorrNormed
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.CcorrNormed);
CvInvoke.MinMaxLoc(res, ref minLoc, ref maxLoc, ref minPoint, ref maxPoint);
var img9 = image0.Clone();
CvInvoke.Rectangle(img9, new Rectangle(maxPoint, img3.Size), new MCvScalar(0, 0, 255), 2);
PreviewImage9 = new WriteableBitmap(Bitmap2BitmapImage(Text(img9.Bitmap, "CcorrNormed")));

三、多匹配

3.1 效果

在这里插入图片描述

3.2 代码

 // 1. 加载原图
var image1 = new Image<Bgr, byte>("Test.png");
var image0 = image1.Mat.Clone();
var imgGray = new Mat();
CvInvoke.CvtColor(image0,imgGray,ColorConversion.Bgr2Gray);
PreviewImage1 = new WriteableBitmap(Bitmap2BitmapImage(Text(image1.Bitmap, "原图")));

// 2. 加载模板
var img3 = new Mat("testTemplate.png",0);
PreviewImage2 = new WriteableBitmap(Bitmap2BitmapImage(Text3(img3.Bitmap, "模板")));

// 3. 匹配
var res = new Mat();
CvInvoke.MatchTemplate(imgGray, img3, res, TemplateMatchingType.CcoeffNormed);
var img4 = image0.Clone();
var m = new Matrix<float>(res.Rows, res.Cols);
res.CopyTo(m);
var image = res.ToImage<Gray, byte>();
for (int i = 0; i < res.Rows; i++)
{
	for (int j = 0; j < res.Cols; j++)
	{
		if (m[i, j] > 0.8)
		{
		   CvInvoke.Rectangle(img4, new Rectangle(new Point(j, i), img3.Size), new MCvScalar(0, 0, 255), 2);
		}
	}
}

PreviewImage3 = new WriteableBitmap(Bitmap2BitmapImage(Text(img4.Bitmap, "结果")));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值