<Window x:Class="FirstSolver.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ha="http://schemas.mvtec.com/halcondotnet"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="16"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="ClearType"
Background="{DynamicResource MaterialDesignPaper}"
FontFamily="Microsoft YaHei Light"
Name="RootWindow" Title="Halcon边缘提取算子" WindowState="Maximized" WindowStartupLocation="CenterScreen">
<materialDesign:DialogHost Identifier="RootDialog" DialogTheme="Inherit">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Style="{StaticResource MaterialDesignFilledTextBox}" materialDesign:HintAssist.Hint="图像文件" Name="TextBoxImagePath" IsReadOnly="True"/>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button Margin="4" Style="{StaticResource MaterialDesignIconButton}" Name="ButtonSelectFile" Click="ButtonSelectFile_Click">
<Button.Content>
<materialDesign:PackIcon Kind="FolderOpenOutline"/>
</Button.Content>
</Button>
<Button Margin="4" Style="{StaticResource MaterialDesignIconButton}" Name="ButtonShowEdges" Click="ButtonShowEdges_Click">
<Button.Content>
<materialDesign:PackIcon Kind="HumanRunFast"/>
</Button.Content>
</Button>
</StackPanel>
</Grid>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ha:HSmartWindowControlWPF Grid.Column="0" Margin="0,0,4,0" Name="HWindowControlWPF1">
<ha:HIconicDisplayObjectWPF x:Name="HIconicDisplayObjectWPF1" />
</ha:HSmartWindowControlWPF>
<ha:HSmartWindowControlWPF Grid.Column="1" Margin="4,0,0,0" Name="HWindowControlWPF2"/>
</Grid>
</Grid>
</materialDesign:DialogHost>
</Window>
using HalconDotNet;
using System.IO;
using System.Windows;
using System.Windows.Controls.Primitives;
namespace FirstSolver
{
public partial class MainWindow : Window
{
/// <summary>
/// 对话框宿主标识符
/// </summary>
public const string DialogHostIdentifier = "RootDialog";
public MainWindow()
{
InitializeComponent();
}
private void ButtonSelectFile_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog()
{
Filter = "Image|*.jpg;*.bmp;*.png;*.tif;*.tga;*.ras;*.jp2;*.j2k;*.jpe",
DereferenceLinks = true
};
if (dlg.ShowDialog() == true)
{
TextBoxImagePath.Text = dlg.FileName;
ButtonShowEdges.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
}
}
private void ButtonShowEdges_Click(object sender, RoutedEventArgs e)
{
string path = TextBoxImagePath.Text;
if (string.IsNullOrEmpty(path) || !File.Exists(path)) return;
HImage img = new HImage(path);
img.GetImageSize(out int width, out int height);
HWindowControlWPF1.HalconWindow.SetPart(0, 0, height - 1, width - 1);
HIconicDisplayObjectWPF1.IconicObject = img;
HImage gray = img.Rgb1ToGray(); // 转换成灰度图像
HRegion edge = gray.SobelAmp("sum_abs", 3).Threshold(20d, 255d).Skeleton();
HWindowControlWPF2.HalconWindow.ClearWindow();
HWindowControlWPF2.HalconWindow.SetPart(0, 0, height - 1, width - 1);
HWindowControlWPF2.HalconWindow.DispObj(edge);
}
}
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。