创建ThreSholdDef文件
创建命名空间Algorithm,包含手动和找到阈值分割参数
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;
}
}
---------------------------------------------------------------------------------------------------------------------------------
创建ImageHelper文件,包含图像处理算法
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;
}
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
}
}
最后在需要使用的窗体文件里调用
private void button_readImage_Click(object sender, EventArgs e)
{
string filename = "C:\\Users\\陈kk\\Pictures\\Screenshots\\屏幕截图 2024-06-13 153257.png";
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 = "C:\\Users\\陈kk\\Pictures\\Screenshots\\屏幕截图 2024-06-13 153257.png";
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);
}