LeadTools创建“检测边缘”应用程序的具体步骤
1、打开Visual Studio .NET。点击 文件->新建->项目…。打开新建项目对话框后,在模板中选择“Visual C#”或“Visual Basic”,随后选择“Windows窗体应用程序”。在名称栏中输入项目名称“DetectEdgesAndLines”,并使用“浏览”按钮选择您工程的存储路径,点击“确定”。
2、在“解决方案资源管理器”中,右击“引用”,选择“添加引用”。根据当前工程的 Framework 版本和生成目标平台,选择添加相应的LeadTools控件,例如工程中的版本为 Framework 4.0、生成目标平台是 x86,则浏览选择Leadtools For .NET文件夹” <LEADTOOLS_INSTALLDIR>\Bin\DotNet4\Win32”,选择以下的DLL“:
Leadtools.dll
Leadtools.Codecs.dll
Leadtools.Codecs.Cmp.dll
Leadtools.ImageProcessing.Color.dll
Leadtools.ImageProcessing.Effects.dll
Leadtools.ImageProcessing.SpecialEffects.dll
Leadtools.WinForms.dll
3、从工具箱(视图->工具箱),添加12个RadioButton控件(将RadioButton的Text属性依照下表修改),两个Panel控件(Name分别修改为panelBefore和panelAfter)。如下图:

4、切换至Form1的代码视图(右击Form1,选择查看代码),将下面几行代码添加到文件开始处:
1: using Leadtools;
2: using Leadtools.Codecs;
3: using Leadtools.WinForms;
4: using Leadtools.ImageProcessing.Effects;
5: using Leadtools.ImageProcessing.SpecialEffects;
6: using Leadtools.ImageProcessing.Color;
5、将以下变量添加至Form1类:
1: private RasterImageViewer beforePic;
2: private RasterImageViewer afterPic;
3: private RasterCodecs codecs;
4: private RasterImage temp;
6、添加Form1 Load事件句柄,在其中添加以下代码:
1: beforePic = new RasterImageViewer();
2: beforePic.BackColor = Color.DarkCyan;
3: beforePic.Dock = DockStyle.Fill;
4: beforePic.InteractiveMode = RasterViewerInteractiveMode.Pan;
5: beforePic.HorizontalAlignMode = RasterPaintAlignMode.Center;
6: beforePic.VerticalAlignMode = RasterPaintAlignMode.Center;
7: beforePic.AutoResetScaleFactor = false;
8: panelBefore.Controls.Add(beforePic);
9: beforePic.BringToFront();
10:
11: afterPic = new RasterImageViewer();
12: afterPic.BackColor = beforePic.BackColor;
13: afterPic.Dock = beforePic.Dock;
14: afterPic.InteractiveMode = beforePic.InteractiveMode;
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;
7、双击radioButton1,在radioButton1 CheckedChanged事件句柄中添加以下代码:
(本段代码为SharpenCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: // Prepare the command
4: SharpenCommand command = new SharpenCommand();
5: //Increase the sharpness by 25 percent of the possible range.
6: command.Sharpness = 950;
7: command.Run(temp);
8: afterPic.Image = temp;
9: codecs.Save(temp, Path.Combine(Application.StartupPath, @"..\..\Pic\SharpeCommand.jpg"), RasterImageFormat.Jpeg, 24);
8、双击radioButton2,在radioButton2 CheckedChanged事件句柄中添加以下代码:
(本段代码为SpatialFilterCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: SpatialFilterCommand command = new SpatialFilterCommand(SpatialFilterCommandPredefined.PrewittHorizontal);
5:
6: command.Run(temp);
7: afterPic.Image = temp;
9、双击radioButton3,在radioButton3 CheckedChanged事件句柄中添加以下代码:
(本段代码为EdgeDetectorCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: // Prepare the command
4: EdgeDetectorCommand command = new EdgeDetectorCommand();
5: command.Threshold = 60;
6: command.Filter = EdgeDetectorCommandType.SobelBoth;
7: //find the edges of the image.
8: command.Run(temp);
9: afterPic.Image = temp;
10: codecs.Save(temp, Path.Combine(Application.StartupPath, @"..\..\Pic\EdgeDetectorCommand.jpg"), RasterImageFormat.Jpeg, 24);
10、双击radioButton4,在radioButton4 CheckedChanged事件句柄中添加以下代码:
(本段代码为EdgeDetectStatisticalCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: EdgeDetectStatisticalCommand command = new EdgeDetectStatisticalCommand();
5: command.Dimension = 15;
6: command.Threshold = 50;
7: command.EdgeColor = new RasterColor(255, 255, 255);
8: command.BackGroundColor = new RasterColor(0, 0, 0);
9:
10: command.Run(temp);
11: afterPic.Image = temp;
12: codecs.Save(temp, Path.Combine(Application.StartupPath, @"..\..\Pic\EdgeDetectStatisticalCommand.jpg"), RasterImageFormat.Jpeg, 24);
13:
11、双击radioButton5,在radioButton5 CheckedChanged事件句柄中添加以下代码:
(本段代码为GlowCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: // Prepare the command
4: GlowCommand command = new GlowCommand();
5: command.Dimension = 5;
6: command.Brightness = 3;
7: command.Threshold = 0;
8: // Apply glow effect on the image.
9: command.Run(temp);
10: afterPic.Image = temp;
11: codecs.Save(temp, Path.Combine(Application.StartupPath, @"..\..\Pic\GlowCommand.jpg"), RasterImageFormat.Jpeg, 24);
12、双击radioButton6,在radioButton6 CheckedChanged事件句柄中添加以下代码:
(本段代码为SmoothEdgesCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: // Prepare the command
4: SmoothEdgesCommand command = new SmoothEdgesCommand();
5: command.Amount = 75;
6: command.Threshold = 0;
7: // Apply the Smooth Edge effect to the image.
8: command.Run(temp);
9: afterPic.Image = temp;
13、双击radioButton7,在radioButton7 CheckedChanged事件句柄中添加以下代码:
(本段代码为SkeletonCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: // Prepare the command
4: SkeletonCommand command = new SkeletonCommand();
5: command.Threshold = 128;
6: command.Run(temp);
7: afterPic.Image = temp;
14、双击radioButton8,在radioButton8 CheckedChanged事件句柄中添加以下代码:
(本段代码为IntensityDetectCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3: //准备命令
4: IntensityDetectCommand command = new IntensityDetectCommand();
5: //应用滤波器
6: command.LowThreshold = 128;
7: command.HighThreshold = 255;
8: command.InColor = new RasterColor(255, 255, 255);
9: command.OutColor = new RasterColor(0, 0, 0);
10: command.Channel = IntensityDetectCommandFlags.Master;
11: command.Run(temp);
12: afterPic.Image = temp;
15、双击radioButton9,在radioButton9 CheckedChanged事件句柄中添加以下代码:
(本段代码为UnsharpMaskCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: UnsharpMaskCommand command = new UnsharpMaskCommand();
5: command.Amount = 200;
6: command.Radius = 15;
7: command.Threshold = 50;
8: command.ColorType = UnsharpMaskCommandColorType.Rgb;
9: command.Run(temp);
10: afterPic.Image = temp;
16、双击radioButton10,在radioButton10 CheckedChanged事件句柄中添加以下代码:
(本段代码为DirectionEdgeStatisticalCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: DirectionEdgeStatisticalCommand command = new DirectionEdgeStatisticalCommand();
5: command.Dimension = 15;
6: command.Threshold = 128;
7: command.Angle = 4500;
8: command.EdgeColor = new RasterColor(255, 255, 255);
9: command.BackGroundColor = new RasterColor(0, 0, 0);
10: command.Run(temp);
11: afterPic.Image = temp;
17、双击radioButton11,在radioButton11 CheckedChanged事件句柄中添加以下代码:
(本段代码为ColoredPencilCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: ColoredPencilCommand command = new ColoredPencilCommand();
5: command.Ratio = 50;
6: command.Dimension = 3;
7: command.Run(temp);
8: afterPic.Image = temp;
18、双击radioButton12,在radioButton12 CheckedChanged事件句柄中添加以下代码:
(本段代码为ColoredPencilExtendedCommand类的使用)
1: beforePic.Image = codecs.Load(Path.Combine(Application.StartupPath, @"..\..\Pic\Master.jpg"));
2: temp = beforePic.Image.Clone();
3:
4: ColoredPencilExtendedCommand command = new ColoredPencilExtendedCommand();
5: command.Size = 5;
6: command.Strength = 4;
7: command.Threshold = 0;
8: command.PencilRoughness = 250;
9: command.StrokeLength = 15;
10: command.PaperRoughness = 100;
11: command.Flags = ColoredPencilExtendedCommandFlags.Artistic;
12: command.Run(temp);
13: afterPic.Image = temp;
19、编译运行程序,本DEMO分别使用了LeadTools的12个类检测增强图像的边缘和线条,结果如下图:

