首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >边缘检测算子

边缘检测算子

原创
作者头像
秦建辉
修改2024-08-30 10:19:52
修改2024-08-30 10:19:52
2140
举报
文章被收录于专栏:HalconHalcon
代码语言:xml
复制
<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/>
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>

                <TextBox Grid.Column="0" Style="{StaticResource MaterialDesignFilledTextBox}" materialDesign:HintAssist.Hint="图像文件" Name="TextBoxImagePath" IsReadOnly="True"/>
                <Button Grid.Column="1" Margin="4" Style="{StaticResource MaterialDesignIconButton}" Name="ButtonSelectFile" Click="ButtonSelectFile_Click">
                    <Button.Content>
                        <materialDesign:PackIcon Kind="FolderOpenOutline"/>
                    </Button.Content>
                </Button>
                <Button Grid.Column="2" Margin="4" Style="{StaticResource MaterialDesignIconButton}" Name="ButtonShowEdges" Click="ButtonShowEdges_Click">
                    <Button.Content>
                        <materialDesign:PackIcon Kind="HumanRunFast"/>
                    </Button.Content>
                </Button>
            </Grid>

            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <ha:HWindowControlWPF Grid.Column="0" Margin="0,0,4,0" Name="HWindowControlWPF1"/>
                <ha:HWindowControlWPF Grid.Column="1" Margin="0,0,4,0" Name="HWindowControlWPF2"/>
            </Grid>
        </Grid>
    </materialDesign:DialogHost>
</Window>
代码语言:cs
复制
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 srcImage = new HImage(path);
            HXLDCont edgesImage = srcImage.EdgesSubPix("canny", 1, 20, 40);

            srcImage.GetImageSize(out int width, out int height);
            HWindowControlWPF1.HalconWindow.SetPart(0, 0, height - 1, width - 1);
            HWindowControlWPF1.HalconWindow.ClearWindow();
            HWindowControlWPF1.HalconWindow.DispObj(srcImage);

            HWindowControlWPF2.HalconWindow.SetPart(0, 0, height - 1, width - 1);
            HWindowControlWPF2.HalconWindow.ClearWindow();
            HWindowControlWPF2.HalconWindow.DispObj(edgesImage);
        }
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档