Halcon Blob分析之焊球检查案例分析——ball.hdev

引言

ball.hdev案例分析


一、结果

1.1 读取原图

在这里插入图片描述

1.2 焊球检测

在这里插入图片描述


二、Halcon代码

* ball.hdev: Inspection of Ball Bonding
* 
//在程序执行期间打开或关闭图标输出对象的自动输出到图形窗口。
dev_update_window ('off') 
//Close the active graphics window.
dev_close_window ()
//Open a new graphics window.
dev_open_window (0, 0, 728, 512, 'black', WindowID)
//Read an image with different file formats.
read_image (Bond, 'die/die_03')
//Displays image objects in the current graphics window.
dev_display (Bond)

set_display_font (WindowID, 14, 'mono', 'true', 'false')
disp_continue_message (WindowID, 'black', 'true')
stop ()    //Stop program execution.
//threshold--使用全局阈值分割图像
threshold (Bond, Bright, 100, 255)
//变换区域的形状
shape_trans (Bright, Die, 'rectangle2')
//Set one or more output colors.
dev_set_color ('green')
//定义区域轮廓输出的线宽
dev_set_line_width (3)
//Define the region fill mode
dev_set_draw ('margin')
//Displays image objects in the current graphics window.
dev_display (Die)
disp_continue_message (WindowID, 'black', 'true')
stop ()
//Reduce the domain of an image.
reduce_domain (Bond, Die, DieGrey)
//threshold--使用全局阈值分割图像
threshold (DieGrey, Wires, 0, 50)
//填充具有给定形状特征的区域中的孔。
fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
//Displays image objects in the current graphics window.
dev_display (Bond)
Define the region fill mode
dev_set_draw ('fill')
// Set one or more output colors.
dev_set_color ('red')
//Displays image objects in the current graphics window.
dev_display (WiresFilled)
disp_continue_message (WindowID, 'black', 'true')
stop ()
//打开一个带有圆形结构元素的区域
opening_circle (WiresFilled, Balls, 15.5)
dev_set_color ('green')
dev_display (Balls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
// Compute connected components of a region.
connection (Balls, SingleBalls)
//Choose regions with the aid of shape features
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)
//根据区域的相对位置对区域进行排序
sort_region (IntermediateBalls, FinalBalls, 'first_point', 'true', 'column')
dev_display (Bond)
dev_set_colored (12)
dev_display (FinalBalls)
disp_continue_message (WindowID, 'black', 'true')
stop ()
//一个区域的最小包围圈
smallest_circle (FinalBalls, Row, Column, Radius)
NumBalls := |Radius|
Diameter := 2 * Radius
meanDiameter := sum(Diameter) / NumBalls
mimDiameter := min(Diameter)
dev_display (Bond)
disp_circle (WindowID, Row, Column, Radius)
dev_set_color ('white')
for i := 1 to NumBalls by 1
    if (fmod(i,2) == 1)
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] - 2.7 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    else
        disp_message (WindowID, 'D: ' + Diameter[i - 1], 'image', Row[i - 1] + 1.2 * Radius[i - 1], max([Column[i - 1] - 60,0]), 'white', 'false')
    endif
endfor
* dump_window (WindowID, 'tiff_rgb', './ball')
dev_set_color ('green')
dev_update_window ('on')
disp_continue_message (WindowID, 'black', 'true')
stop ()
dev_close_window ()

三、主要函数分析

1.threshold (Operator)

threshold — Segment an image using global threshold.

结果

...
threshold (Bond, Bright, 100, 255)
...

在这里插入图片描述

...
threshold (DieGrey, Wires, 0, 50)
...

在这里插入图片描述

函数

threshold(Image : Region : MinGray, MaxGray : )
//参数
	Image (input_object)     //Input image.
	Region (output_object)   //Segmented region.
	MinGray/MaxGray (input_control)  //灰度值的下限/下限
		Default value: 128.0/255.0
		Suggested values: 0.0, 10.0, 30.0, 64.0, 128.0, 200.0, 220.0, 255.0
		Restriction: MaxGray >= MinGray

阈值从输入图像中选择灰度值g满足以下条件的像素:
在这里插入图片描述

满足条件的图像的所有点作为一个区域返回。 如果传递了多个灰度值区间(MinGray 和 MaxGray 的元组),则为每个区间返回一个单独的区域。

相关函数

auto_threshold (Operator)
//auto_threshold — 使用从直方图确定的阈值对图像进行分割。
auto_threshold(Image : Regions : Sigma : )
binary_threshold (Operator)
//binary_threshold — 使用二值阈值分割图像
binary_threshold(Image : Region : Method, LightDark : UsedThreshold)
char_threshold (Operator)
//char_threshold — 执行阈值分割以提取字符。
char_threshold(Image, HistoRegion : Characters : Sigma, Percent : Threshold)
dual_threshold (Operator)
//dual_threshold — Threshold operator for signed images
dual_threshold(Image : RegionCrossings : MinSize, MinGray, Threshold : )
dyn_threshold (Operator)
//dyn_threshold — 使用局部阈值分割图像
dyn_threshold(OrigImage, ThresholdImage : RegionDynThresh : Offset, LightDark : )
fast_threshold (Operator)
//fast_threshold — 使用全局阈值对图像进行快速阈值处理
fast_threshold(Image : Region : MinGray, MaxGray, MinSize : )
hysteresis_threshold (Operator)
//hysteresis_threshold — 对图像执行滞后阈值操作。
hysteresis_threshold(Image : RegionHysteresis : Low, High, MaxLength : )
local_threshold (Operator)
//local_threshold — 使用局部阈值分割图像。
local_threshold(Image : Region : Method, LightDark, GenParamName, GenParamValue : )
threshold_sub_pix (Operator)
//threshold_sub_pix — 从具有亚像素精度的图像中提取平交点。
threshold_sub_pix(Image : Border : Threshold : )
var_threshold (Operator)
//var_threshold — 通过局部均值和标准差分析对图像进行阈值处理。
var_threshold(Image : Region : MaskWidth, MaskHeight, StdDevScale, AbsThreshold, LightDark : )
watersheds_threshold (Operator)
//watersheds_threshold — Extract watershed basins from an image using a threshold
watersheds_threshold(Image : Basins : Threshold : )

在这里插入图片描述
以上阈值函数算子后期再做详细分析,本文只做了解

2.shape_trans (Operator)

shape_trans — 变换区域的形状

结果

...
threshold (Bond, Bright, 100, 255)
shape_trans (Bright, Die, 'rectangle2')
...

在这里插入图片描述

函数

shape_trans(Region : RegionTrans : Type : )
//参数
	Region (input_object)        //Regions to be transformed.
	RegionTrans (output_object)  //Transformed regions.
	Type (input_control)  
		Type of transformation.
		Default value: 'convex'
		List of values: 'convex', 'ellipse', 'inner_center', 'inner_circle',
		 'inner_rectangle1', 'outer_circle', 'rectangle1', 'rectangle2'

shape_trans 根据参数类型转换输入区域的形状:

  1. ‘convex’ ——Convex hull.
  2. ‘ellipse’ ——与输入区域具有相同矩和面积的椭圆
  3. ‘outer_circle’——最小的封闭圆
  4. ‘inner_circle’——适合该区域的最大圆。
  5. ‘rectangle1’——平行于坐标轴的最小封闭矩形
  6. ‘rectangle2’ ——最小的封闭矩形
  7. ‘inner_rectangle1’——适合该区域的最大轴平行矩形
  8. ‘inner_center’——输入区域骨架上与输入区域重心距离最小的点

3.reduce_domain (Operator)

reduce_domain — Reduce the domain of an image

结果

相当于截取需要研究的主要部分

...
reduce_domain (Bond, Die, DieGrey)
threshold (DieGrey, Wires, 0, 50)
...

在这里插入图片描述

在这里插入图片描述

函数

reduce_domain(Image, Region : ImageReduced : : )
//参数
	Image (input_object)          //Input image.
	Region (input_object)         //New definition domain.
	ImageReduced (output_object)  //Image with reduced definition domain.
运算符reduce_domain 将给定图像的定义域缩小到指定区域。 
新定义域计算为旧定义域与区域的交集。 
因此,新定义域可以是该区域的子集。 矩阵的大小没有改变。

4.fill_up_shape (Operator)

fill_up_shape — 填充具有给定形状特征的区域中的孔。

结果

...
fill_up_shape (Wires, WiresFilled, 'area', 1, 100)
dev_display (Bond)
dev_set_draw ('fill')
dev_set_color ('red')
dev_display (WiresFilled)
...

在这里插入图片描述

函数

fill_up_shape(Region : RegionFillUp : Feature, Min, Max : )
//参数
	Region (input_object)         //Input region(s).
	RegionFillUp (output_object)  //Output region(s) with filled holes.
	Feature (input_control)       //使用的形状特征。
		Default value: 'area'
		List of values: 'anisometry', 'area', 'compactness', 'convexity', 
		'inner_circle', 'outer_circle', 'phi', 'ra', 'rb'
	Min (input_control)         //最小特征值
		Default value: 1.0
		Suggested values: 0.0, 1.0, 10.0, 50.0, 100.0, 500.0, 1000.0, 10000.0
		Typical range of values: 0.0 ≤ Min
	Max (input_control)        //最大特征值
		Default value: 100.0
		Suggested values: 10.0, 50.0, 100.0, 500.0, 1000.0, 10000.0, 100000.0
		Typical range of values: 0.0 ≤ Max
	

fill_up_shape 填充具有给定形状特征的输入区域区域中的那些空洞。 参数 Feature 确定要使用的形状特征,而 Min 和 Max 确定形状特征必须位于的范围,以便填充孔

5.opening_circle (Operator)

opens_circle — 打开一个带有圆形结构元素的区域。

结果

...
opening_circle (WiresFilled, Balls, 15.5)
dev_set_color ('green')
dev_display (Balls)
...

在这里插入图片描述

函数

opening_circle(Region : RegionOpening : Radius : )
//参数
	Region (input_object)           //Regions to be opened.
	RegionOpening (output_object)   //Opened regions
	Radius (input_control)          //圆形结构元素的半径
		Default value: 3.5
		Suggested values: 1.5, 2.5, 3.5, 4.5, 5.5, 7.5, 9.5, 12.5, 15.5, 
		19.5, 25.5, 33.5, 45.5, 60.5, 110.5
		Typical range of values: 0.5 ≤ Radius ≤ 511.5 (lin)
		Minimum increment: 1.0
		Recommended increment: 1.0

opens_circle 被定义为一个侵蚀,然后是一个带有圆形结构元素的 Minkowsi 加法(参见示例)。 开口用于消除小区域(小于圆形结构元素)并平滑区域的边界。

6.connection (Operator)

connection — Compute connected components of a region.

函数

connection(Region : ConnectedRegions : : )
//参数
	Region (input_object)      //Input region.
	ConnectedRegions (output_object)  //Connected components

connection 确定 Region 中给定的输入区域的连通分量。 用于此的邻域可以通过 set_system(‘neighborhood’,<4/8>) 设置。 默认是 8-neighborhood,这对于确定前景的连通分量很有用。 connection 返回的最大连接组件数可以通过 set_system(‘max_connection’,) 设置。 默认值 0 会导致返回所有连接的组件。 连接的逆运算符是 union1。

7.select_shape

select_shape — 借助形状特征选择区域

结果

connection (Balls, SingleBalls)
select_shape (SingleBalls, IntermediateBalls, 'circularity', 'and', 0.85, 1.0)

在这里插入图片描述

函数

select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
//参数
	Regions (input_object)           //Regions to be examined.
	SelectedRegions (output_object)  //满足条件的区域
	Features (input_control)         //要检查的形状特征
	Default value: 'area'
	List of values: 'anisometry', 'area', 'area_holes', 'bulkiness', 'circularity',
	 'column', 'column1', 'column2', 'compactness', 'connect_num', 'contlength',
	  'convexity', 'dist_deviation', 'dist_mean', 'euler_number', 'height', 
	 'holes_num', 'inner_height', 'inner_radius', 'inner_width', 'max_diameter',
	  'moments_i1', 'moments_i2', 'moments_i3', 'moments_i4', 'moments_ia',
	  'moments_ib', 'moments_m02', 'moments_m02_invar', 'moments_m03',
	  'moments_m03_invar', 'moments_m11', 'moments_m11_invar', 'moments_m12',
	  'moments_m12_invar', 'moments_m20', 'moments_m20_invar', 'moments_m21',
	  'moments_m21_invar', 'moments_m30', 'moments_m30_invar', 'moments_phi1',
	  'moments_phi2', 'moments_psi1', 'moments_psi2', 'moments_psi3', 
	  'moments_psi4', 'num_sides', 'orientation', 'outer_radius', 'phi', 
	  'ra', 'rb', 'rect2_len1', 'rect2_len2', 'rect2_phi', 'rectangularity',
	  'roundness', 'row', 'row1', 'row2', 'struct_factor', 'width'
	Operation (input_control)   //Linkage type of the individual features.
			Default value: 'and'
			List of values: 'and', 'or'
	Min (input_control)      //Lower limits of the features or 'min'.
			Default value: 150.0
			Typical range of values: 0.0 ≤ Min ≤ 99999.0
			Minimum increment: 0.001
			Recommended increment: 1.0
	Max (input_control)    //Upper limits of the features or 'max'.
			Default value: 99999.0
			Typical range of values: 0.0 ≤ Max ≤ 99999.0
			Minimum increment: 0.001
			Recommended increment: 1.0
			Restriction: Max >= Min

运算符 select_shape 根据形状选择区域。 对于来自 Regions 的每个输入区域,计算指示的特征(Features)。 如果每个 (Operation = ‘and’) 或至少一个 (Operation = ‘or’) 计算的特征在默认限制 (Min,Max) 内,则该区域将适应输出(重复)


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值