首先我们在模板上面布置好相应的格式,用一个Window来展示我们阈值分割的图像,用两个Button来展示阈值分割前后的图片
随后我们通过两个Button点击事件将原图和阈值分割的相应代码完善好,以下代码附上
using Algorithm;
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 第一个联合项目
{
public partial class FormTest : Form
{
public FormTest()
{
InitializeComponent();
}
private HObject ho_image;
private void button_readImage_Click(object sender, EventArgs e)
{
string filename = "D:\\shixun\\1 (4).bmp";
bool result= Algorithm.ImageHelper.ReadImage(out ho_image, filename, hWindowControl_yuanshi.HalconWindow);
if (result!=true)
{
MessageBox.Show(" Algorithm.ImageHelper.ReadImage 失败");
}
}
private void button_threshold_Click(object sender, EventArgs e)
{
string filename = "D:\\shixun\\1 (4).bmp";
bool result = Algorithm.ImageHelper.ReadImage(out ho_image, filename, hWindowControl_yuanshi.HalconWindow);
HObject Region;
//Algorithm.ImageHelper.Threshold(ho_image, out Region, 0, 100);
//Algorithm.ImageHelper.BinaryThreshold(ho_image, out Region, Algorithm.自动阈值分割方法.max_separability, Algorithm.亮或暗.light);
// 0, 100, Algorithm.阈值分割方式.BinaryThreshold, Algorithm.自动阈值分割方法.max_separability, Algorithm.亮或暗.dark
//Algorithm.ThreSholdDef threSholdDef = new Algorithm.ThreSholdDef(0,128);
// 自动阈值分割方法 segmentMothd, 亮或暗 lightOrDark
Algorithm.ThreSholdDef threSholdDef = new Algorithm.ThreSholdDef(自动阈值分割方法.max_separability, 亮或暗.dark);
Algorithm.ImageHelper.ThresholdALL(ho_image, out Region, threSholdDef);
HOperatorSet.DispObj(Region, hWindowControl_yuanshi.HalconWindow);
}
}
}
随后我们开始读取图片,用try catch函数捕获异常,如果代码正常运行则会在try函数里面返回true,如果代码有异常则不会使代码死机以及软件崩溃,在catch函数里面返回false,然后使用阈值分割来调用普通阈值分割和自动阈值分割,实现代码如下
using HalconDotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Algorithm
{
/// <summary>
/// 这个类就是我图像处理的算法类
/// </summary>
public class ImageHelper
{
/// <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._MinGray);
case 阈值分割方式.BinaryThreshold:
Console.WriteLine("我调用了自动阈值分割 BinaryThreshold(");
return BinaryThreshold(hImage, out Region, threSholdDef._SegmentMothd, threSholdDef._LightOrDark);
default:
return false;
}
}
#endregion
}
}
至此,就已经成功封装到了一个函数当中