C#与halcon联合函数封装----阈值分割与自动阈值分割封装为一个类

第一步:在窗体联合项目(不会请参考C#调用halcon)中创建两个类(名字自拟)

第二步:对窗体进行编辑,在之前的基础上加入一个按钮和两个ComboBox下拉框

第三步:在ThreSholdel类文件中进行编写代码,主要内容包括对阈值分割和自动阈值分割的数据编写(代码如下)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm
{
    public enum 阈值分割方式
    {
        Threshold = 0,//普通阈值分割
        BinaryThreshold = 1,//自动阈值分割
    }
    public enum 自动阈值分割方法
    {
        max_separability = 0,
        smooth_histo = 1,
    }
    public enum 亮或暗
    {
        dark = 0,
        light = 1
    }
    /// <summary>
    /// 阈值分割参数类
    /// </summary>
    public class ThreSholdDef
    {
        public ThreSholdDef(int minGray, int maxGray)
        {
            _Segment = 阈值分割方式.Threshold;
            _MaxGray = maxGray;
            _MinGray = minGray;
        }
        public ThreSholdDef(自动阈值分割方法 segmentMothd, 亮或暗 lightOrDark)
        {
            _Segment = 阈值分割方式.BinaryThreshold;
            _SegmentMothd = segmentMothd;
            _LightOrDark = lightOrDark;
        }
        //是普通阈值分割还是自动阈值分割
        public 阈值分割方式 _Segment;
        /// <summary>
        /// 普通阈值分割需要的灰度
        /// </summary>
        public int _MaxGray;
        public int _MinGray;
        //确实自动阈值分割为三角法还是大津法
        public 自动阈值分割方法 _SegmentMothd;
        //确定自动阈值分割是找亮还是找暗
        public 亮或暗 _LightOrDark;
    }
}

第四步:在ImageHelp类文件中对相应的方法进行编写,包括对阈值分割和自动阈值分割的方法编写以及读取图片的代码优化(代码及相关注释如下)

using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm
{
    /// <summary>
    /// 这个类就是我图像处理的算法类
    /// </summary>
    public class ImageHelp
    {
        /// <summary>
        /// 这是一个读取图片 并且在指定窗体上显示的方法
        /// </summary>
        /// <param name="hImage"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static bool ReadImage(out HObject hImage, string filename, HWindow window)
            {
                try
                {
                    HOperatorSet.ReadImage(out hImage, filename);
                    HTuple width, height;
                    //获得图片的宽高
                    HOperatorSet.GetImageSize(hImage, out width, out height);
                    //设置显示范围
                    //HWindowControl.HalconWindow -->控件的句柄  设置显示范围
                    HOperatorSet.SetPart(window, 0, 0, (height - 1), (width - 1));
                    //显示
                    HOperatorSet.DispObj(hImage, window);
                    return true;

                }
                catch
                {
                    hImage = null;
                    return false;
                }
            }




            #region 【阈值分割】
            public static bool Threshold(HObject hImage, out HObject Region, int Min, int Max)
            {
                HOperatorSet.Threshold(hImage, out Region, Min, Max);
                return true;
            }
            public static bool BinaryThreshold(HObject hImage, out HObject Region, 自动阈值分割方法 method, 亮或暗 lightAndDark)
            {
                HTuple use;
                HOperatorSet.BinaryThreshold(hImage, out Region, method.ToString(), lightAndDark.ToString(), out use);
                return true;
            }

            //private static bool ThresholdALL(HObject hImage, out HObject Region, int MinGray, int MaxGray, 阈值分割方式 method, 自动阈值分割方法 method2 , 亮或暗 method3 )
            //{
            //    Region = null;
            //    switch (method)
            //    {
            //        case 阈值分割方式.Threshold:

            //            return Threshold(hImage,out Region, MinGray, MaxGray);

            //        case 阈值分割方式.BinaryThreshold:
            //            return BinaryThreshold(hImage,out Region, method2, method3);

            //        default:
            //            return false;
            //    }
            //}

            public static bool ThresholdALL(HObject hImage, out HObject Region, ThreSholdDef threSholdDef)
            {
                Region = null;

                switch (threSholdDef._Segment)
                {
                    case 阈值分割方式.Threshold:
                        Console.WriteLine("我调用了普通阈值分割  Threshold");
                        return Threshold(hImage, out Region, threSholdDef._MinGray, threSholdDef._MaxGray);

                    case 阈值分割方式.BinaryThreshold:
                        Console.WriteLine("我调用了自动阈值分割  BinaryThreshold");
                        return BinaryThreshold(hImage, out Region, threSholdDef._SegmentMothd, threSholdDef._LightOrDark);
                    default:
                        return false;
                }
            }

            #endregion
     }
}

第五步:对之前窗体的代码的改写(代码如下*如有错误欢迎指出)

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // comboBoxs--->调用ThreSholdel方法
        comboBox1.Items.Add("area");
        comboBox1.Items.Add("circularity");
        comboBox1.Items.Add("rectangularity");
        comboBox2.Items.Add("max_separability");
        comboBox2.Items.Add("smooth_histo");
        comboBox3.Items.Add("dark");
        comboBox3.Items.Add("light");
        //选择首个选项
        comboBox1.SelectedIndex = 0;
        comboBox2.SelectedIndex = 0;
        comboBox3.SelectedIndex = 0;
    }
    HObject ho_image;
    HTuple MinGray;
    HTuple MaxGray;
    HObject Region;
    HTuple width, height;
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK )
        {
            string filename = openFileDialog.FileName;
            bool result = ImageHelp.ReadImage(out ho_image, filename, hWindowControl1.HalconWindow);
            if (result != true)
            {
                MessageBox.Show(" Algorithm.ImageHelper.ReadImage 失败");
            }
        }
    }
    private void button2_Click(object sender, EventArgs e)
    {
        hWindowControl2.HalconWindow.ClearWindow();
        if (ho_image == null)
        {
            button1_Click(null, null);
        }
        //设置颜色
        HOperatorSet.SetColor(hWindowControl2.HalconWindow, "white");
        //int.Parse(string)字符串类型转换成int类型
        MaxGray = int.Parse(textBox1.Text);
        MinGray = int.Parse(textBox2.Text);
        ThreSholdDef threSholdDef = new ThreSholdDef(MinGray, MaxGray);
        ImageHelp.ThresholdALL(ho_image, out Region, threSholdDef);
        HOperatorSet.GetImageSize (ho_image, out width,out height);
        //HWindowControl.HalconWindow --->窗体句柄 设置显示范围
        HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, (height - 1), (width - 1));
        //显示
        HOperatorSet.DispObj(Region, hWindowControl2.HalconWindow);
    }
    private void button3_Click(object sender, EventArgs e)
    {
        HOperatorSet.SetColor(hWindowControl3.HalconWindow, "green");
        HTuple width, height;
        //获得图片的宽高
        HOperatorSet.GetImageSize(ho_image, out width, out height);
        //设置显示范围
        //HWindowControl.HalconWindow -->控件的句柄  设置显示范围
        HOperatorSet.SetPart(hWindowControl3.HalconWindow, 0, 0, (height - 1), (width - 1));
        //打散
        HObject ConnectionRegion;
        HOperatorSet.Connection(Region, out ConnectionRegion);
        //特征筛选
        HObject SelectRegion;
        //HOperatorSet.SelectShape(ConnectionRegion,out SelectRegion, "area","and", 3227.85, 7897.33);
        HOperatorSet.SelectShape(ConnectionRegion, out SelectRegion, comboBox1.SelectedItem.ToString(), "and", Double.Parse(textBox4.Text), Double.Parse(textBox3.Text));
        //显示
        HOperatorSet.DispObj(ho_image, hWindowControl3.HalconWindow);
        HOperatorSet.DispObj(SelectRegion, hWindowControl3.HalconWindow);

    }

    private void button4_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog = new OpenFileDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            HOperatorSet.ReadImage(out ho_image, openFileDialog.FileName);
        }
    }

    private void button6_Click(object sender, EventArgs e)
    {
        hWindowControl2.HalconWindow.ClearWindow();
        if (ho_image == null)
        {
            button1_Click(null, null);
        }
        //设置颜色
        HOperatorSet.SetColor(hWindowControl2.HalconWindow, "white");
        ThreSholdDef threSholdDef = new ThreSholdDef(自动阈值分割方法.max_separability, 亮或暗.dark);
        ImageHelp.ThresholdALL(ho_image, out Region, threSholdDef);
        HOperatorSet.GetImageSize(ho_image, out width, out height);
        //HWindowControl.HalconWindow --->窗体句柄 设置显示范围
        HOperatorSet.SetPart(hWindowControl2.HalconWindow, 0, 0, (height - 1), (width - 1));
        HOperatorSet.DispObj(Region, hWindowControl2.HalconWindow);
    }

    private void button5_Click(object sender, EventArgs e)
    {
        MessageBox.Show(comboBox1.SelectedItem.ToString());
    }
}

出现的问题:无法运行代码(参考C#调用halcon.dll)取消勾选32位

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值