小言_互联网的博客

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件

1445人阅读  评论(0)

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件

在C#与Halcon联合编程时,一般先需要在工具箱中加入Halcon自带的HWindowControl控件。最近在研究用PictureBox控件显示Halcon算子生成的图像或区域时,发现可用PictureBox控件来替代HWindowControl控件。

示例:

我自己创建了一个窗体(如下图),可实现一下三个功能:

(1)读图:打开一个文件夹,读取里面的一张图像;

(2)分割:画一个任意大小矩形,将矩形内的区域分割出来,并放大显示;

(3)阈值:利用二值化阈值算子将(2)中的矩形图像进行阈值处理;

 代码:

窗体构造函数

创建窗口,并作为全局变量供其它功能使用;


  
  1. HTuple _hWind; //窗口 全局变量
  2. public Form1()
  3. {
  4. InitializeComponent();
  5. //用pictureBox创建窗口
  6. HOperatorSet.OpenWindow( 0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "visible", "", out _hWind);
  7. }

功能一:读图

读取图像作为全局变量,供功能二使用;


  
  1. HObject ho_Image;
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. HTuple hv_Width, hv_Height;
  5. OpenFileDialog openDlg = new OpenFileDialog();
  6. openDlg.RestoreDirectory = true;
  7. if (openDlg.ShowDialog() == DialogResult.OK)
  8. {
  9. HOperatorSet.ReadImage( out ho_Image, openDlg.FileName);
  10. HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
  11. HOperatorSet.SetPart(_hWind, 0, 0, hv_Height - 1, hv_Width - 1);
  12. HOperatorSet.DispObj(ho_Image, _hWind);
  13. }
  14. }

效果图:

功能二:分割

将放大显示图像作为全局变量,供功能三使用;


  
  1. HObject ho_ImagePart;
  2. private void button2_Click(object sender, EventArgs e)
  3. {
  4. HTuple row1, column1, row2, column2;
  5. HTuple hv_Width, hv_Height;
  6. HObject rect, ho_ImageReduced;
  7. HOperatorSet.SetColor(_hWind, "red");
  8. //画矩形
  9. HOperatorSet.DrawRectangle1(_hWind, out row1, out column1, out row2, out column2);
  10. HOperatorSet.GenRectangle1( out rect, row1, column1, row2, column2);
  11. //分割图像
  12. HOperatorSet.ReduceDomain(ho_Image, rect, out ho_ImageReduced);
  13. //将分割后的图像放大显示
  14. HOperatorSet.CropDomain(ho_ImageReduced, out ho_ImagePart);
  15. HOperatorSet.GetImageSize(ho_ImagePart, out hv_Width, out hv_Height);
  16. HOperatorSet.SetPart(_hWind, 0, 0, hv_Height - 1, hv_Width - 1);
  17. HOperatorSet.ClearWindow(_hWind);
  18. HOperatorSet.DispObj(ho_ImagePart, _hWind);
  19. }

效果图 :

功能三:阈值

二值化阈值处理并显示处理后的区域


  
  1. private void button3_Click(object sender, EventArgs e)
  2. {
  3. HObject ho_Region;
  4. HTuple hv_UsedThreshold;
  5. HOperatorSet.BinaryThreshold(ho_ImagePart, out ho_Region, "max_separability", "dark", out hv_UsedThreshold);
  6. HOperatorSet.DispRegion(ho_Region, _hWind);
  7. }

效果图:

 


转载:https://blog.csdn.net/Kevin_Sun777/article/details/116143790
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场