.net6 wpf 联合 halcon 编程
1、halcon导出函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | *读取图片 read_image (Image, 'C:/Users/admin/Desktop/halcondemo1/claudia.png' ) get_image_size (Image, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, 'black' , WindowHandle) dev_display (Image) *灰度 rgb1_to_gray (Image, GrayImage) dev_display (GrayImage) *边缘 edges_image (Image, ImaAmp, ImaDir, 'canny' , 1, 'nms' , 10, 20) threshold (ImaAmp, Edges, 1 , 255) dev_display (Edges) |
halconl导出代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | // // File generated by HDevelop for HALCON/.NET (C#) Version 17.12 // using HalconDotNet; public partial class HDevelopExport { #if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN) public HDevelopExport() { // Default settings used in HDevelop HOperatorSet.SetSystem( "width" , 512); HOperatorSet.SetSystem( "height" , 512); if (HalconAPI.isWindows) HOperatorSet.SetSystem( "use_window_thread" , "true" ); action(); } #endif #if !NO_EXPORT_MAIN // Main procedure private void action() { // Local iconic variables HObject ho_Image, ho_GrayImage, ho_ImaAmp; HObject ho_ImaDir, ho_Edges; // Local control variables HTuple hv_Width = null , hv_Height = null , hv_WindowHandle = null ; // Initialize local and output iconic variables HOperatorSet.GenEmptyObj( out ho_Image); HOperatorSet.GenEmptyObj( out ho_GrayImage); HOperatorSet.GenEmptyObj( out ho_ImaAmp); HOperatorSet.GenEmptyObj( out ho_ImaDir); HOperatorSet.GenEmptyObj( out ho_Edges); //读取图片 ho_Image.Dispose(); HOperatorSet.ReadImage( out ho_Image, "C:/Users/admin/Desktop/halcondemo1/claudia.png" ); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr( "background_color" , "black" ); HOperatorSet.OpenWindow(0,0,hv_Width,hv_Height,0, "visible" , "" , out hv_WindowHandle); HDevWindowStack.Push(hv_WindowHandle); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } //灰度 ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_GrayImage, HDevWindowStack.GetActive()); } //边缘 ho_ImaAmp.Dispose();ho_ImaDir.Dispose(); HOperatorSet.EdgesImage(ho_Image, out ho_ImaAmp, out ho_ImaDir, "canny" , 1, "nms" , 10, 20); ho_Edges.Dispose(); HOperatorSet.Threshold(ho_ImaAmp, out ho_Edges, 1, 255); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Edges, HDevWindowStack.GetActive()); } ho_Image.Dispose(); ho_GrayImage.Dispose(); ho_ImaAmp.Dispose(); ho_ImaDir.Dispose(); ho_Edges.Dispose(); } #endif } #if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN) public class HDevelopExportApp { static void Main( string [] args) { new HDevelopExport(); } } #endif |
halcon显示
2、使用导出的C#代码
4、修改导出的 cs程序
核心:把导出来的打开新窗口函数 删除!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | // // File generated by HDevelop for HALCON/.NET (C#) Version 17.12 // using HalconDotNet; using System; public partial class HDevelopExport { #if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN) public HDevelopExport(IntPtr intPtr) { // Default settings used in HDevelop HOperatorSet.SetSystem( "width" , 512); HOperatorSet.SetSystem( "height" , 512); if (HalconAPI.isWindows) HOperatorSet.SetSystem( "use_window_thread" , "true" ); //设置窗体 if (HDevWindowStack.IsOpen()) { HOperatorSet.CloseWindow(HDevWindowStack.Pop()); } HOperatorSet.SetWindowAttr( "background_color" , "black" ); this .hv_WindowHandle = intPtr; HDevWindowStack.Push(hv_WindowHandle); } // Local iconic variables private HObject ho_Image = null ; private HObject ho_GrayImage = null ; private HObject ho_ImaAmp = null ; private HObject ho_ImaDir = null ; private HObject ho_Edges = null ; // Local control variables private HTuple hv_Width = null ; private HTuple hv_Height = null ; private HTuple hv_WindowHandle = null ; #endif // Main procedure public void action() { // Initialize local and output iconic variables HOperatorSet.GenEmptyObj( out ho_Image); //读取图片 ho_Image.Dispose(); HOperatorSet.ReadImage( out ho_Image, "C:/Users/admin/Desktop/halcondemo1/claudia.png" ); HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height); //显示图片 if (HDevWindowStack.IsOpen()) { HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Width - 1, hv_Height - 1); } if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive()); } } /// <summary> /// 灰度 /// </summary> public void ActionGray() { HOperatorSet.GenEmptyObj( out ho_GrayImage); ho_GrayImage.Dispose(); HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_GrayImage, HDevWindowStack.GetActive()); } ho_GrayImage.Dispose(); } /// <summary> /// 边缘 /// </summary> public void ActionEdge() { HOperatorSet.GenEmptyObj( out ho_ImaAmp); HOperatorSet.GenEmptyObj( out ho_ImaDir); HOperatorSet.GenEmptyObj( out ho_Edges); //边缘 ho_ImaAmp.Dispose(); ho_ImaDir.Dispose(); HOperatorSet.EdgesImage(ho_Image, out ho_ImaAmp, out ho_ImaDir, "canny" , 1, "nms" , 10, 20); ho_Edges.Dispose(); HOperatorSet.Threshold(ho_ImaAmp, out ho_Edges, 1, 255); if (HDevWindowStack.IsOpen()) { HOperatorSet.DispObj(ho_Edges, HDevWindowStack.GetActive()); } } } |
5、使用
1 | MainWindow.xaml |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <Window x:Class= "WpfApp3HalconTestDemo.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:local= "clr-namespace:WpfApp3HalconTestDemo" xmlns:hs= "clr-namespace:HalconDotNet;assembly=halcondotnet" mc:Ignorable= "d" Title= "MainWindow" Height= "450" Width= "800" > <Grid> <Grid.RowDefinitions> <RowDefinition Height= "auto" ></RowDefinition> <RowDefinition ></RowDefinition> </Grid.RowDefinitions> <StackPanel Orientation= "Horizontal" HorizontalAlignment= "Left" > <Button Content= "读取图片" Margin= "3" MinWidth= "130" Click= "ReadImage_Click" ></Button> <Button Content= "灰度" Margin= "3" MinWidth= "130" Click= "GrayImage_Click" ></Button> <Button Content= "边缘检测" Margin= "3" MinWidth= "130" Click= "EdgeImage_Click" ></Button> </StackPanel> <hs:HSmartWindowControlWPF x:Name= "hs" Grid.Row= "1" Loaded= "hs_Loaded" > </hs:HSmartWindowControlWPF> </Grid> </Window> |
1 | MainWindow.xaml.cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp3HalconTestDemo { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private HDevelopExport hDevelopExport; public MainWindow() { InitializeComponent(); } private void hs_Loaded( object sender, RoutedEventArgs e) { hDevelopExport = new HDevelopExport( this .hs.HalconID); } private void ReadImage_Click( object sender, RoutedEventArgs e) { hDevelopExport.action(); } private void GrayImage_Click( object sender, RoutedEventArgs e) { hDevelopExport.ActionGray(); } private void EdgeImage_Click( object sender, RoutedEventArgs e) { hDevelopExport.ActionEdge(); } } } |
显示
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探