多语言展示
当前在线:832今日阅读:167今日分享:16

WPF使用Halcon打开图片

有时候图像视觉需要使用Halcon会更方便些。这里介绍一种WPF采用WPF使用Halcon打开图片的例子,以供参考。
工具/原料
1

VS2013

2

RGB图片一张,命名'1.jpeg'

3

了解WPF、C#语法

4

Halcon相关文件halcondotnet.dll、halcon.dll、halcondotnet.xml、halconxl.dll

方法/步骤
1

新建WPF工程,在解决方案资源管理器中选中当前工程项目,鼠标右击,弹出右键菜单,移动鼠标点击【属性】,弹出属性界面,选择【生成】项,勾选中【允许不安全代码】

2

添加引用halcondotnet.dll,下载halcondotnet.dll、halcon.dll、halcondotnet.xml、halconxl.dll到生成目录下。并准备一张RGB图片,重命名为'1.jpeg'

3

在MainWindow.xaml页面中添加如下代码:                    

4

在MainWindow.xaml.cs页面中添加如下代码:using HalconDotNet; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; 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 HalconTest {     ///

     /// MainWindow.xaml 的交互逻辑     ///      public partial class MainWindow : Window     {         public unsafe MainWindow()         {             InitializeComponent();             HObject image;             HOperatorSet.ReadImage(out image,  '1.jpeg');            HImage h_image = new HImage();             //HTuple pointer;             HTuple t;             HTuple w;             HTuple h;             HTuple pointerRed;             HTuple pointerGreen;             HTuple pointerBlue;             HOperatorSet.GetImagePointer3(image, out pointerRed, out pointerGreen, out pointerBlue, out t, out w, out h);             h_image.GenImage3(t, w, h, pointerRed, pointerGreen, pointerBlue);             string t2;             int w2;             int h2;             IntPtr pr, pg, pb;             h_image.GetImagePointer3(out pr, out pg, out pb, out t2, out w2, out h2);             int len = w * h;             byte[] datar = new byte[len];             byte[] datag = new byte[len];             byte[] datab = new byte[len];             Marshal.Copy(pr, datar, 0, len);             Marshal.Copy(pg, datag, 0, len);             Marshal.Copy(pb, datab, 0, len);             h_image.Dispose();             byte[] data = new byte[len * 3];             fixed (byte* bp = data, bbp = datab, brp = datar, bgp = datag)             {                 byte* b = bp;                 byte* bb = bbp;                 byte* bg = bgp;                 byte* br = brp;                 for (int i = 0; i < len; i++)                 {                     *b = *bb; b++; bb++;                     *b = *bg; b++; bg++;                     *b = *br; b++; br++;                 }             }             WriteableBitmap wb = new WriteableBitmap(w, h, 96, 96, System.Windows.Media.PixelFormats.Bgr24, null);             wb.WritePixels(new System.Windows.Int32Rect(0, 0, w, h), data, w * 3, 0);             mygrid.Background = new System.Windows.Media.ImageBrush(wb);         }    } }

5

编译代码,生成解决方案。编译成功后,按F5执行。出现图像画面。

推荐信息