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

相关文章

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

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、核心特性