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

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

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

示例:

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

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

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

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

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件_第1张图片

 代码:

窗体构造函数

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

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

功能一:读图

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

        HObject ho_Image;
        private void button1_Click(object sender, EventArgs e)
        {          
            HTuple hv_Width, hv_Height;

            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.RestoreDirectory = true;
            if (openDlg.ShowDialog() == DialogResult.OK)
            {
                HOperatorSet.ReadImage(out ho_Image, openDlg.FileName);

                HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
                HOperatorSet.SetPart(_hWind, 0, 0, hv_Height - 1, hv_Width - 1);
                HOperatorSet.DispObj(ho_Image, _hWind);
            }
        }

效果图:

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件_第2张图片

功能二:分割

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

        HObject ho_ImagePart;
        private void button2_Click(object sender, EventArgs e)
        {
            HTuple row1, column1, row2, column2;
            HTuple hv_Width, hv_Height;
            HObject rect, ho_ImageReduced;

            HOperatorSet.SetColor(_hWind, "red");
            //画矩形
            HOperatorSet.DrawRectangle1(_hWind, out row1, out column1, out row2, out column2);
            HOperatorSet.GenRectangle1(out rect, row1, column1, row2, column2);
            //分割图像
            HOperatorSet.ReduceDomain(ho_Image, rect, out ho_ImageReduced);
            //将分割后的图像放大显示
            HOperatorSet.CropDomain(ho_ImageReduced, out ho_ImagePart);
            HOperatorSet.GetImageSize(ho_ImagePart, out hv_Width, out hv_Height);
            HOperatorSet.SetPart(_hWind, 0, 0, hv_Height - 1, hv_Width - 1);
            HOperatorSet.ClearWindow(_hWind);            
            HOperatorSet.DispObj(ho_ImagePart, _hWind);
        }

效果图 :

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件_第3张图片C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件_第4张图片

功能三:阈值

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

        private void button3_Click(object sender, EventArgs e)
        {
            HObject ho_Region;
            HTuple hv_UsedThreshold;

            HOperatorSet.BinaryThreshold(ho_ImagePart, out ho_Region, "max_separability", "dark", out hv_UsedThreshold);
            HOperatorSet.DispRegion(ho_Region, _hWind);
        }

效果图:

C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件_第5张图片

 

你可能感兴趣的:(C#与Halcon联合编程之用PictureBox控件替代HWindowControl控件)