halcon与C#混合编程(二)读取并处理图片

转载:http://www.cnblogs.com/hanzhaoxin/archive/2013/02/03/2890871.html


示例:读取并处理图片

halcon源码:

   1: *读取图片
   2: read_image(Image,'D:/MyFile/C#/HalconCsExample/示例图片.jpg')
   3: get_image_size(Image,Width,Height)
   4:  
   5: *图像处理
   6: rgb1_to_gray(Image,GrayImage)

导出的代码(ImagePro):

   1: //
   2: //  File generated by HDevelop for HALCON/DOTNET (C#) Version 10.0
   3: //
   4: //  This file is intended to be used with the HDevelopTemplate or
   5: //  HDevelopTemplateWPF projects located under %HALCONEXAMPLES%\c#
   6:  
   7: using System;
   8: using HalconDotNet;
   9:  
  10: public partial class HDevelopExport
  11: {
  12:   public HTuple hv_ExpDefaultWinHandle;
  13:  
  14:   // Main procedure 
  15:   private void action()
  16:   {
  17:  
  18:     // Local iconic variables 
  19:  
  20:     HObject ho_Image, ho_GrayImage;
  21:  
  22:  
  23:     // Local control variables 
  24:  
  25:     HTuple hv_Width, hv_Height;
  26:  
  27:     // Initialize local and output iconic variables 
  28:     HOperatorSet.GenEmptyObj(out ho_Image);
  29:     HOperatorSet.GenEmptyObj(out ho_GrayImage);
  30:  
  31:     //读取图片
  32:     //dev_close_window(...);
  33:     ho_Image.Dispose();
  34:     HOperatorSet.ReadImage(out ho_Image, "D:/MyFile/C#/HalconCsExample/示例图片.jpg");
  35:     HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
  36:     //dev_open_window(...);
  37:     HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
  38:  
  39:     //图像处理
  40:     ho_GrayImage.Dispose();
  41:     HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
  42:     HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
  43:  
  44:     ho_Image.Dispose();
  45:     ho_GrayImage.Dispose();
  46:  
  47:   }
  48:  
  49:   public void InitHalcon()
  50:   {
  51:     // Default settings used in HDevelop 
  52:     HOperatorSet.SetSystem("do_low_error", "false");
  53:   }
  54:  
  55:   public void RunHalcon(HTuple Window)
  56:   {
  57:     hv_ExpDefaultWinHandle = Window;
  58:     action();
  59:   }
  60:  
  61: }
  62:  
C#工程:
   1: using System;
   2: using System.Collections.Generic;
   3: using System.ComponentModel;
   4: using System.Data;
   5: using System.Drawing;
   6: using System.Linq;
   7: using System.Text;
   8: using System.Windows.Forms;
   9:  
  10: using HalconDotNet; //添加引用
  11:  
  12: namespace HalconCsExample
  13: {
  14:     public partial class Form1 : Form
  15:     {
  16:         //增加代码:
  17:         HDevelopExport HD = new HDevelopExport();
  18:         string ImagePath;
  19:  
  20:         public Form1()
  21:         {
  22:             InitializeComponent();
  23:             this.btnImagePro.Enabled = false;
  24:         }
  25:  
  26:         private void btnReadImage_Click(object sender, EventArgs e)
  27:         {
  28:             openFileDialog1.Filter = "JPEG文件|*.jpg*|BMP文件|*.bmp*|TIFF文件|*.tiff*";
  29:             openFileDialog1.RestoreDirectory = true;
  30:             openFileDialog1.FilterIndex = 1;
  31:             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  32:             {
  33:                 ImagePath = openFileDialog1.FileName;
  34:                 HD.ReadImage(hWindowControl1.HalconWindow, ImagePath);
  35:                 this.btnImagePro.Enabled = true;
  36:             }   
  37:         }
  38:  
  39:         private void btnImagePro_Click(object sender, EventArgs e)
  40:         {
  41:             HD.ImagePro();
  42:             this.btnImagePro.Enabled = false;
  43:         }
  44:     }
  45:  
  46:     //从导出的ReadImage.cs中拷贝HDevelopExport类到命名空间 HalconCsExample
  47:     public partial class HDevelopExport
  48:     {
  49:         public HTuple hv_ExpDefaultWinHandle;
  50:         // Local iconic variables 
  51:         HObject ho_Image, ho_GrayImage;
  52:  
  53:  
  54:         public void InitHalcon()
  55:         {
  56:             // Default settings used in HDevelop 
  57:             HOperatorSet.SetSystem("do_low_error", "false");
  58:         }
  59:  
  60:         public void ReadImage(HTuple Window,string path)
  61:         {
  62:             hv_ExpDefaultWinHandle = Window;
  63:  
  64:             // Initialize local and output iconic variables 
  65:             HOperatorSet.GenEmptyObj(out ho_Image);
  66:             HOperatorSet.GenEmptyObj(out ho_GrayImage);
  67:             
  68:             //读取图片
  69:             //dev_close_window(...);
  70:             ho_Image.Dispose();
  71:             HOperatorSet.ReadImage(out ho_Image, path);
  72:            
  73:             //dev_open_window(...);
  74:             HOperatorSet.DispObj(ho_Image, hv_ExpDefaultWinHandle);
  75:         }
  76:  
  77:         public void ImagePro()
  78:         {
  79:             //图像处理
  80:             ho_GrayImage.Dispose();
  81:             HOperatorSet.Rgb1ToGray(ho_Image, out ho_GrayImage);
  82:             HOperatorSet.DispObj(ho_GrayImage, hv_ExpDefaultWinHandle);
  83:  
  84:             ho_Image.Dispose();
  85:             ho_GrayImage.Dispose();
  86:  
  87:         }
  88:  
  89:     }
  90: }

运行结果:

halcon与C#混合编程(二)读取并处理图片_第1张图片

单击读取图片按钮并选取图片:

halcon与C#混合编程(二)读取并处理图片_第2张图片

点击图像处理按钮:

halcon与C#混合编程(二)读取并处理图片_第3张图片


你可能感兴趣的:(halcon,学习,halcon,学习)