前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >生成十字形

生成十字形

原创
作者头像
秦建辉
发布2024-09-05 20:14:02
860
发布2024-09-05 20:14:02
举报
文章被收录于专栏:Halcon
代码语言: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"/>
                </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 Width="Auto"/>
                </Grid.ColumnDefinitions>

                <ha:HSmartWindowControlWPF Grid.Column="0" Margin="0,0,4,0" Name="HWindowControlWPF1">
                    <ha:HIconicDisplayObjectWPF x:Name="HIconicDisplayObjectWPF1" />
                </ha:HSmartWindowControlWPF>
            </Grid>
        </Grid>
    </materialDesign:DialogHost>
</Window>
代码语言:txt
复制
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);
            HWindowControlWPF1.HalconWindow.AttachBackgroundToWindow(img);

            HRegion region = img.BinaryThreshold("max_separability", "dark", out int usedThreshold);
            HRegion connectedRegions = region.Connection();
            HWindowControlWPF1.HalconWindow.SetLineWidth(3);
            HWindowControlWPF1.HalconWindow.SetColor("red");

            HRegion objectSelected = connectedRegions.SelectObj(1);
            objectSelected.GetRegionPolygon(5.0, out HTuple rows, out HTuple cols);
            HOperatorSet.GenCrossContourXld(out HObject cross, rows, cols, 30, 0.785398);
            cross.DispObj(HWindowControlWPF1.HalconWindow);

            HWindowControlWPF1.HalconWindow.SetFont("Microsoft YaHei UI-Normal-16");
            HWindowControlWPF1.HalconWindow.DispText(
                new HTuple(new string[] { "Hello", "Gong!" }),
                "window",
                new HTuple(12),
                new HTuple(12),
                new HTuple(new string[] { "black", "blue" }),
                new HTuple("box"),
                new HTuple("true"));
            HWindowControlWPF1.HalconWindow.DispText(
                "How are you!",
                "window",
                12,
                120,
                "red",
                new HTuple(new string[] { "box_color", "box" }),
                new HTuple(new string[] { "green", "false" }));
        }
    }
}

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

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

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

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

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