创建LeadTools“对比图像”应用程序的具体步骤

2026-02-28 05:29:09

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)。如下图:

创建LeadTools“对比图像”应用程序的具体步骤

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!

创建LeadTools“对比图像”应用程序的具体步骤

猜你喜欢