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

2024-01-25 23:30
文章标签 模板 cv 入门 匹配 十五 emgu

本文主要是介绍EMGU.CV入门(十五、模板匹配),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、函数介绍

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 neededpublic 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 coordinatespublic 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, "结果")));

这篇关于EMGU.CV入门(十五、模板匹配)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/644964

相关文章

浅谈配置MMCV环境,解决报错,版本不匹配问题

《浅谈配置MMCV环境,解决报错,版本不匹配问题》:本文主要介绍浅谈配置MMCV环境,解决报错,版本不匹配问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录配置MMCV环境,解决报错,版本不匹配错误示例正确示例总结配置MMCV环境,解决报错,版本不匹配在col

详解nginx 中location和 proxy_pass的匹配规则

《详解nginx中location和proxy_pass的匹配规则》location是Nginx中用来匹配客户端请求URI的指令,决定如何处理特定路径的请求,它定义了请求的路由规则,后续的配置(如... 目录location 的作用语法示例:location /www.chinasem.cntestproxy

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

关于Gateway路由匹配规则解读

《关于Gateway路由匹配规则解读》本文详细介绍了SpringCloudGateway的路由匹配规则,包括基本概念、常用属性、实际应用以及注意事项,路由匹配规则决定了请求如何被转发到目标服务,是Ga... 目录Gateway路由匹配规则一、基本概念二、常用属性三、实际应用四、注意事项总结Gateway路由

基于Java实现模板填充Word

《基于Java实现模板填充Word》这篇文章主要为大家详细介绍了如何用Java实现按产品经理提供的Word模板填充数据,并以word或pdf形式导出,有需要的小伙伴可以参考一下... Java实现按模板填充wor编程d本文讲解的需求是:我们需要把数据库中的某些数据按照 产品经理提供的 word模板,把数据