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

相关文章

Java如何根据word模板导出数据

《Java如何根据word模板导出数据》这篇文章主要为大家详细介绍了Java如何实现根据word模板导出数据,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... pom.XML文件导入依赖 <dependency> <groupId>cn.afterturn</groupId>

Nginx路由匹配规则及优先级详解

《Nginx路由匹配规则及优先级详解》Nginx作为一个高性能的Web服务器和反向代理服务器,广泛用于负载均衡、请求转发等场景,在配置Nginx时,路由匹配规则是非常重要的概念,本文将详细介绍Ngin... 目录引言一、 Nginx的路由匹配规则概述二、 Nginx的路由匹配规则类型2.1 精确匹配(=)2

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

Python中Flask模板的使用与高级技巧详解

《Python中Flask模板的使用与高级技巧详解》在Web开发中,直接将HTML代码写在Python文件中会导致诸多问题,Flask内置了Jinja2模板引擎,完美解决了这些问题,下面我们就来看看F... 目录一、模板渲染基础1.1 为什么需要模板引擎1.2 第一个模板渲染示例1.3 模板渲染原理二、模板

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Nginx location匹配模式与规则详解

《Nginxlocation匹配模式与规则详解》:本文主要介绍Nginxlocation匹配模式与规则,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、环境二、匹配模式1. 精准模式2. 前缀模式(不继续匹配正则)3. 前缀模式(继续匹配正则)4. 正则模式(大

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注