Halcon一维函数的使用之auto_threshol

347 阅读5分钟

引言

1、一维函数的使用——auto_threshold.hdev

以下是halcon12.0中的例程代码

dev_close_window ()                     //关闭窗口
read_image (Aegypt1, 'egypt1')			//读取图像
get_image_size (Aegypt1, Width, Height) //获取图像的宽高
dev_open_window (0, 0, Width, Height, 'black', WindowID)   //打开一个图像大小的窗口
set_display_font (WindowID, 14, 'mono', 'true', 'false')   //窗体
dev_set_colored (6)            //设置多种输出颜色
dev_clear_window ()            //清除活动图形窗口的内容。
Sigma := 4
auto_threshold (Aegypt1, Regions, Sigma)   //使用从直方图确定的阈值分割图像
gray_histo (Aegypt1, Aegypt1, AbsoluteHisto, RelativeHisto) //计算灰度值分布
disp_continue_message (WindowID, 'black', 'true')
stop ()             //停止程序执行
dev_clear_window () //清除活动图形窗口的内容
create_funct_1d_array (AbsoluteHisto, Function)  //创建一个一维函数
smooth_funct_1d_gauss (Function, Sigma, SmoothedFunction) //使用高斯平滑一维函数
dev_set_color ('red')           //设置一种或多种输出颜色
//将输入函数 Function 拆分为 x 和 y 值的元组
funct_1d_to_pairs (SmoothedFunction, XValues, YValues) 
gen_region_histo (Histo1, YValues, 255, 255, 1)  //将直方图转换为区域
dev_display (Aegypt1)    //在当前图形窗口中显示图像对象
dev_set_color ('white')  //设置多种输出颜色
gen_region_histo (Histo2, RelativeHisto, 255, 255, 1)  //将直方图转换为区域

2、分析关键函数功能作用

2.1 auto_threshold (Operator)——根据直方图确定的阈值分割图像

函数原型:

auto_threshold(Image : Regions : Sigma : )
//其中,
Image (input_object)
Regions (output_object)  --在自动确定的间隔内具有灰度值的区域
Sigma (input_control)    --Sigma 用于直方图的高斯平滑
	//Default value: 2.0
	//Suggested values: 0.0, 0.5, 1.0, 2.0, 3.0, 4.0, 5.0
	//Typical range of values: 0.0 ≤ Sigma ≤ 100.0 (lin)
	//Minimum increment: 0.01
	//Recommended increment: 0.3
	//Restriction: Sigma >= 0.0

解析: auto_threshold 使用多个阈值分割单通道图像。 首先,确定灰度值的绝对直方图。 然后,从直方图中提取相关的最小值,这些最小值被连续用作阈值操作的参数。用于字节图像的阈值为 0、255,以及从直方图中提取的所有最小值(在直方图使用标准差为 Sigma 的高斯滤波器平滑后)。 对于每个灰度值区间,生成一个区域。因此,区域的数量是最小值的数量 + 1。对于 uint2 图像,类似地使用上述过程。然而,这里的最高阈值是 65535。此外,对于 uint2 图像,Sigma 的值(实际上)是指具有 256 个值的直方图,尽管内部使用了更高分辨率的直方图。这样做是为了方便在图像类型之间切换,而无需更改参数 Sigma。 对于浮点图像,阈值是图像中的最小和最大灰度值以及从直方图中提取的所有最小值。 这里,参数Sigma的缩放是指图像的原始灰度值。 Sigma 的值越大,提取的区域就越少。如果要提取的区域表现出相似的灰度值(同质区域),则此运算符很有用。

example

read_image (Image, 'fabrik')
median_image (Image, Median, 'circle', 3, 'mirrored')
auto_threshold (Median, Seg, 2.0)
connection (Seg, Connected)

在这里插入图片描述

2.2 gray_histo (Operator)——计算灰度值分布

函数原型

gray_histo(Regions, Image : : : AbsoluteHisto, RelativeHisto)
//其中
Regions (input_object)   --要计算直方图的区域  
Image (input_object)     --将要计算的灰度值分布的图像
inglechannelimage → object (byte* / cyclic* / direction* / int1* / int2 / uint2 / int4 / real) *allowed for compute devices
AbsoluteHisto (output_control)  --灰度值的绝对频率
RelativeHisto (output_control)  --频率,归一化为该区域的面积。

在这里插入图片描述

解析: 算子 gray_histo 为区域内的图像 (Image) 计算灰度值的绝对 (AbsoluteHisto) 和相对 (RelativeHisto) 直方图。

两个直方图都是 256 个值的元组,其中 --- 从 0 开始---包含图像各个灰度值的频率。

AbsoluteHisto 用整数表示灰度值的绝对频率,RelativeHisto 表示相对值,即绝对频率除以图像面积的浮点数。

'real'-, 'int2'-, 'uint2'-, 和 'int4'-images 转换成'byte'-images(首先确定图像中最大和最小的灰度值,然后原始灰度值是 线性映射到区域 0..255),然后如上所述进行处理。 直方图也可以通过运算符 set_paint(::WindowHandle,'histogram' : ) 和 disp_image 直接作为图形返回。

2.3 create_funct_1d_array (Operator)

函数原型

create_funct_1d_array( : : YValues : Function)
//其中
YValues (input_control)    --X value for function points
Function (output_control)  --Created function

create_funct_1d_array 从一组 y 值 YValues 创建一个一维函数。然后可以使用一维函数的运算符处理和分析生成的函数。 YValues 解释如下:YValues 的第一个值是零处的函数值,第二个值是一处的函数值,依此类推。因此,这些值定义了等距 x 值(距离为 1)处的函数,从 0 开始.

或者,运算符 create_funct_1d_pairs 可用于创建函数。 create_funct_1d_pairs 还允许通过明确指定它们来定义具有非等距 x 值的函数。因此,要获得与 create_funct_1d_array 相同的定义,可以将 x 值的元组传递给 create_funct_1d_pairs,该元组的长度与 YValues 相同,并且包含从 0 开始并在每个位置增加 1 的值。但是请注意,create_funct_1d_pairs 导致函数的不同内部表示需要更多存储(因为存储了所有 (x,y) 对),有时无法像 create_funct_1d_array 创建的函数那样有效地处理。

2.4 smooth_funct_1d_gauss (Operator)——使用高斯函数平滑等距一维函数。

函数原型

smooth_funct_1d_gauss( : : Function, Sigma : SmoothedFunction)
//其中
Function (input_control)   --Function to be smoothed
Sigma (input_control)      --用于平滑的高斯函数的 Sigma
	Default value: 2.0
	Suggested values: 0.5, 1.0, 2.0, 3.0, 4.0, 5.0
	Typical range of values: 0.1 ≤ Sigma ≤ 50.0 (lin)
	Minimum increment: 0.01
	Recommended increment: 0.2

解析 算子 smooth_funct_1d_gauss 用高斯函数平滑一维函数。 该函数必须是等距的,即使用 create_funct_1d_array、sample_funct_1d 或类似方法创建。 在函数边界,函数值被镜像。

注意平滑参数Sigma不能大于(Length-2)/7.8,Length是Function的控制点数。 Length 的值可以,例如,用 num_points_funct_1d 确定。

2.5 funct_1d_to_pairs (Operator) ——访问函数的 x/y 值

函数原型

funct_1d_to_pairs( : : Function : XValues, YValues)
//其中
Function (input_control)    --Input function
XValues (output_control)    --X values of the function
YValues (output_control)    --Y values of the function

功能: funct_1d_to_pairs 将输入函数 Function 拆分为 x 和 y 值的元组。

2.6 gen_region_histo (Operator)——将直方图转换为区域

在这里插入图片描述 函数原型

gen_region_histo( : Region : Histogram, Row, Column, Scale : )
//其中
Region (output_object)      --Region containing the histogram
Histogram (input_control)   --Input histogram
Row (input_control)  
	/*直方图中心的行坐标。
	  默认值:255
      建议值:100、200、255、300、400
      值的典型范围:0 ≤ Row ≤ 511
    */
Column (input_control) 
/*
Column coordinate of the center of the histogram.
Default value: 255
Suggested values: 100, 200, 255, 300, 400
Typical range of values: 0 ≤ Column ≤ 511
*/
    
Scale (input_control)  
/*
直方图的比例因子。
默认值:1
值列表:1、2、3、4、5、6、7
典型值范围:1 ≤ Scale ≤ 10 (lin)
最小增量:1
推荐增量:1
*/
  

功能: gen_region_histo 将使用 gray_histo、gray_histo_range 或 tuple_histo_range 创建的直方图转换为区域。 三个控制参数的作用与disp_image和set_paint中相同。