halcon的代码
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image, 'C:/Users/CF/Desktop/image.jpg')
dev_display (Image)draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
reduce_domain (Image, Rectangle, ImageReduced)
crop_domain (ImageReduced, ImagePart)create_shape_model (ImagePart, 'auto', -0.39, 0.79, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)write_shape_model (ModelID, 'C:/Users/CF/Desktop/shapeModel1.shm')find_shape_model (ImagePart, ModelID, -0.39, 0.79, 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
halcon结果
WPF代码:
xaml:
<Window x:Class="HalconWPF.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:halcon="clr-namespace:HalconDotNet;assembly=halcondotnet"xmlns:local="clr-namespace:HalconWPF"mc:Ignorable="d"WindowStartupLocation="CenterScreen"Title="WPF/Halcon_TemplateMatchingExample" Height="450" Width="800"><Grid><Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition/><RowDefinition Height="40"/></Grid.RowDefinitions><StackPanel Orientation="Horizontal"><Button Content="读取图片" Click="BtnReadImage_Click" Margin="5" Padding="5"/><Button Content="定位结果" Click="BtnRun_Click" Margin="5" Padding="5"/></StackPanel><halcon:HSmartWindowControlWPF x:Name="HSmart" Grid.Row="1"/><TextBlock x:Name="txtResult" Background="LightBlue" Grid.Row="2" Margin="5"/></Grid>
</Window>
cs代码:
using HalconDotNet;
using System.Windows;namespace HalconWPF
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();}// 创建HImage对象用于图像处理HImage hImage = new HImage();// 创建HTuple对象用于存储模型IDHTuple modelID = new HTuple();// 创建HTuple对象用于存储匹配结果的位置、角度和得分HTuple hv_Row = new HTuple(), hv_Column = new HTuple();HTuple hv_Angle = new HTuple(), hv_Score = new HTuple();// 读取图片private void BtnReadImage_Click(object sender, RoutedEventArgs e){ // 读取图像文件hImage.ReadImage("C:/Users/CF/Desktop/Image.jpg");// 在Halcon的 HSmartWindowControlWPF 控件中显示图像HSmart.HalconWindow.DispObj(hImage);}// 模板匹配 private void BtnRun_Click(object sender, RoutedEventArgs e){// 从指定路径读取形状模型文件HOperatorSet.ReadShapeModel("C:/Users/CF/Desktop/shapeModel1.shm", out modelID);// 执行模板匹配操作 HOperatorSet.FindShapeModel(hImage, // 输入图像modelID, // 形状模型ID-0.39, 0.79, // 搜索角度范围0.5, 1, // 搜索尺度范围0.5, // 最小重叠比例"least_squares", // 匹配方法0, // 优化参数0.9, // 最低得分阈值out hv_Row, // 输出匹配位置的行坐标out hv_Column, // 输出匹配位置的列坐标out hv_Angle, // 输出匹配位置的角度out hv_Score // 输出匹配得分); // 将匹配结果显示在文本框中txtResult.Text = $"分数:{hv_Score.DArr[0]} , Row坐标: {hv_Row.DArr[0]} , Column坐标:{hv_Column.DArr[0]}";// 设置Halcon窗口的颜色为红色HOperatorSet.SetColor(HSmart.HalconWindow, "red");// 在Halcon窗口中绘制匹配位置的十字标记HOperatorSet.DispCross(HSmart.HalconWindow, hv_Row.DArr[0], hv_Column.DArr[0], 30, hv_Angle);}}
}