- LeadTools中文图像处理教程(1):更改数据格式 点击进入
- LeadTools中文图像处理教程(2):图像的几何变换 点击进入
- LeadTools中文图像处理教程(3):倾斜校正 点击进入
- LeadTools中文图像处理教程(4):调整图像亮度和对比度 点击进入
- LeadTools中文图像处理教程(5):图像去噪 点击进入
- LeadTools中文图像处理教程(6):检测和增强边缘、线条 点击进入
- LeadTools中文图像处理教程(7):应用艺术效果 点击进入
- LeadTools中文图像处理教程(8):调整色彩 点击进入
- LeadTools中文图像处理教程(9):添加图像至另一个图像 点击进入
- LeadTools中文图像处理教程(10):窗位(仅用于Medical) 点击进入
- LeadTools中文图像处理教程(11):对比图像 点击进入
你肯定非常熟悉文本搜索的方法,Ctrl+F轻松搞定。但是如果对于一张密密麻麻全是字符的图片,你是否还在苦恼怎样快速在茫茫词海找到你的目标?现在!LeadTools强大的图像处理功能为您轻松解决! 提供一张你想要搜索的图像A,再提供一张目标图像B,使用LeadTools的CorrelationCommand类,便可在B中对比搜索出图像A出现的地方了!
本文主要介绍
1 LeadTools使用CorrelationCommand对比图像的整体思路
2 CorrelationCommand的使用范围
3 使用LeadTools创建“对比图像”应用程序的具体步骤
下面,我们简单了解下LeadTools“对比图像”的思路:
1. 选择你想要搜索的图像,将其赋给CorrelationImage属性(或构造函数的CorrelationImage参数)。例如:
2. 选择目标图像,即要在目标图像中搜索,例如:
3. 调用CorrelationImage。
4. 这个命令会比较将要搜索的图像(或搜索图像的部分)与目标图像的所有区域进行对比,通过相关性的度量找到匹配的区域,在此过程中会更新Points数组属性。
5. 结果如下图所示。
CorrelationCommand使用范围:
此类只适应于Document和Medical工具包。支持12位和16位灰度图像、48位和64位彩色图像。
创建LeadTools“对比图像”应用程序的具体步骤:
1. 打开Visual Studio .NET。点击 文件->新建->项目…。打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“CompareImages”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
2. 在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32”,选择以下的DLL“:
- Leadtools.dll
- Leadtools.Codecs.dll
- Leadtools.Codecs.Tif.dll
- Leadtools.WinForms.dll
- Leadtools.ImageProcessing.Core.dll
点击“确定”按钮,将以上所有的DLL添加到应用程序中。
3. 从工具箱(视图->工具箱),添加一个Button控件(将button1的Text属性修改为“在目标图像中搜索第一幅图像的字符L”,两个Panel控件(Name分别修改为panelBefore和panelAfter)。如下图:
4. 切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:
1: using Leadtools;
2: using Leadtools.Codecs;
3: using Leadtools.WinForms;
4: using Leadtools.ImageProcessing.Core;
5: using Leadtools.Codecs.Tif;
5. 将以下变量添加至Form1类:
1: private RasterImageViewer beforePic;
2: private RasterImageViewer afterPic;
3: private RasterCodecs codecs;
6. 添加Form1 Load事件句柄,在其中添加以下代码:
1: beforePic = new RasterImageViewer();
2: beforePic.BackColor = Color.DarkCyan;3: beforePic.Dock = DockStyle.Fill;4: beforePic.HorizontalAlignMode = RasterPaintAlignMode.Center;5: beforePic.VerticalAlignMode = RasterPaintAlignMode.Center;6: beforePic.AutoResetScaleFactor = false;
7: beforePic.InteractiveMode = RasterViewerInteractiveMode.Region;8: beforePic.InteractiveRegionType = RasterViewerInteractiveRegionType.Rectangle;9: panelBefore.Controls.Add(beforePic);10: beforePic.BringToFront();11:12: afterPic = new RasterImageViewer();
13: afterPic.BackColor = beforePic.BackColor;14: afterPic.Dock = beforePic.Dock;15: afterPic.HorizontalAlignMode = beforePic.HorizontalAlignMode;16: afterPic.VerticalAlignMode = beforePic.VerticalAlignMode;17: afterPic.AutoResetScaleFactor = beforePic.AutoResetScaleFactor;18: panelAfter.Controls.Add(afterPic);19: afterPic.BringToFront();20:21: codecs = new RasterCodecs();
22: codecs.ThrowExceptionsOnInvalidImages = true;
23: //加载图像
24: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\L.tif"));
25: afterPic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\All.tif"));
7. 双击button1,在button1 Click事件句柄中添加以下代码:
1: LeadRect rc_cor = new LeadRect(5, 5, 35, 35);
2:3: CorrelationCommand cmd = new CorrelationCommand();
4: cmd.CorrelationImage = beforePic.Image;5: cmd.CorrelationImage.AddRectangleToRegion(null, rc_cor, RasterRegionCombineMode.Set);
6: cmd.Points = new LeadPoint[90];
7: cmd.Threshold = 90;8: cmd.XStep = 2;9: cmd.YStep = 1;10: cmd.Run(afterPic.Image);11:12: for (int i_cor = 0; i_cor < cmd.NumberOfPoints; i_cor++)13: {14: rc_cor = new LeadRect(cmd.Points[i_cor].X, cmd.Points[i_cor].Y, 35, 35);
15: if (i_cor == 0)
16: afterPic.Image.AddRectangleToRegion(null, rc_cor, RasterRegionCombineMode.Set);
17: else
18: afterPic.Image.AddRectangleToRegion(null, rc_cor, RasterRegionCombineMode.Or);
19: }
8.编译运行程序,结果如下图,从下图我们看到CorrelationCommand在目标图像中轻松找到了所有字符L!
DEMO下载:
如果你希望亲自体验一下LeadTools的对比图像效果,不妨下载LeadTools试用版自己去试试。
若您在使用LeadTools的过程中遇到任何问题,欢迎在葡萄城开发者社区LeadTools板块提问,我们的专业技术团队期待您的到来!