Halcon基础视频教程
——带你进入图像处理的世界
1.方法1 轮廓线拟合测量
1.1.测量1–Measure pump
* step: create and select relevant contours
*
threshold_sub_pix (ImageReduced, Border, 80)
select_shape_xld (Border, SelectedXLD, [‘contlength’,‘outer_radius’], ‘and’, [70,15], [99999,99999])
segment_contours_xld (SelectedXLD, ContoursSplit, ‘lines_circles’, 4, 2, 2)
select_shape_xld (ContoursSplit, SelectedXLD3, [‘outer_radius’,‘contlength’], ‘and’, [15,30], [45,99999])
union_cocircular_contours_xld (SelectedXLD3, UnionContours2, 0.5, 0.1, 0.2, 2, 10, 10, ‘true’, 1)
sort_contours_xld (UnionContours2, SortedContours, ‘upper_left’, ‘true’, ‘column’)
1.2 钻石角度测量 -XLD
2.2D测量–卡尺测量工具
序号 名称 案例
1 add_metrology_object_rectangle2_measure Apply_metrology_model.hdev案例
2 add_metrology_object_circle_measure Apply_metrology_model.hdev案例
3 add_metrology_object_ellipse_measure
4 add_metrology_object_line_measure Apply_model_diamond.hdev钻石角度测量案例(形态学找到位置,然后应用2维线测量)
5 add_metrology_object_generic Measure_stamping_part匹配+2维测量案例(应用匹配找到位置)
6 add_metrology_object_line_measure Point_line_to_hom_mat2d(对halcon图像进行点线变换)
apply_metrology_model.hdev案例
2.1二维计量 create_metrology_model
Metrology: 测量
MetrologyHandle 手柄
create_metrology_model(算子)
create_metrology_model - 创建测量几何形状所需的数据结构。
create_metrology_model(::: MetrologyHandle)
描述
create_metrology_model创建计量模型,即通过2D计量测量具有特定几何形状(计量对象)的对象所需的数据结构,并将其返回到手柄MetrologyHandle中。
二维计量的基本原理
通常,对于2D度量,必须提供要测量的对象的位置,方向和几何形状的近似值。在显示对象的图像内,这些近似对象的边界用于定位对象的真实边缘以适应几何形状的参数,使得它们最佳地适合图像数据。测量结果是优化的参数。计量模型用于存储所有必要的信息,例如计量对象的位置和几何形状的初始参数,控制测量的参数以及测量结果。可以通过2D计量测量的几何形状包括圆形,椭圆形,矩形和线条。
图像中对象的边缘位于所谓的测量区域内。这些是矩形区域,其垂直于近似物体的边界布置,使得它们的中心位于边界上。调整测量区域的尺寸和分布的参数与每个测量对象的近似形状参数一起指定。
当应用测量时,使用RANSAC算法确定所有测量区域内的边缘位置并使其适合几何形状。
2.2 set_metrology_model_image_size
设置被测量图像的尺寸大小
2.3增加一个形状(矩形,圆形)到测量模型
共有5种类型
1 add_metrology_object_rectangle2_measure
2 add_metrology_object_circle_measure
3 add_metrology_object_ellipse_measure
4 add_metrology_object_line_measure
5 add_metrology_object_generic
//2.3.1增加一个矩形到测量模型 矩形2
//参数说明: MetrologyHandle指测量模型的句柄, Row为矩形中心的y坐标值, Column为矩形中心的x坐标值, Phi为矩形的长半轴的方向,
//Length1为矩形长半轴的长度, Length2为矩形短半轴的长度,
MeasureLength1相对于长半轴垂直边界的检测区域一半长度, RectangleTolerance 10
//MeasureLength2相对于短半轴垂直边界的检测区域一半长度, 5
MeasureSigma高斯平滑用到的参数, .5
MeasureThreshold最小边界的阈值, 1
// GenParamName指定通用参数的名称, []
GenParamValue指定通用参数的值, []
Index指定创建测量对象的索引 -MetrologyRectangleIndice
add_metrology_object_rectangle2_measure (MetrologyHandle, RectangleInitRow, RectangleInitColumn, RectangleInitPhi, RectangleInitLength1, RectangleInitLength2, RectangleTolerance, 5, .5, 1, [], [], MetrologyRectangleIndices)
// 增加一个圆形到测量模型 圆形2
矩形
//measure_stamping part
测量冲压件
2.3.2 add_metrology_object_generic(:: MetrologyHandle,Shape,ShapeParam,MeasureLength1,MeasureLength2,MeasureSigma,MeasureThreshold,GenParamName,GenParamValue:Index)
- Add several full circles
CircleParam := [354,274,53]
CircleParam := [CircleParam,350,519,53]
CircleParam := [CircleParam,345,764,52]
CircleParam := [CircleParam,596,523,53]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [], [], CircleIndices1) - Add two partial circles
CircleParam1 := [583,1010,79]
CircleParam2 := [336,1005,77]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam1, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam2, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3] - Add a retangle
RectangleParam := [599,279,rad(90),62,51]
add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices) - Add two lines
Line1 := [143,1122,709,1132]
Line2 := [151,153,136,1115]
add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
描述
add_metrology_object_generic将类型为Shape的计量对象添加到计量模型中并准备矩形度量区域(有关2D度量的基本原理,请参阅create_metrology_model)。模型的句柄在MetrologyHandle中传递。 Shape指定将哪种类型的对象添加到计量模型中。运算符add_metrology_object_generic在参数Index中返回添加的度量对象的索引。请注意,add_metrology_object_generic在一个运算符中提供运算符add_metrology_object_circle_measure,add_metrology_object_ellipse_measure,add_metrology_object_rectangle2_measure和add_metrology_object_line_measure的功能。
序号 名称 解释
1 可能的形状 ‘圆形’:‘旋转矩形’:‘椭圆’:‘线’:
2 测量区域 ShapeParam
3 测量区域的定义 MeasureLength1 MeasureLength2
4 高斯平滑用到的参数 MeasureSigma
5 最小边缘阈值 MeasureThreshold
根据Shape中指定的对象,需要以下值:
‘圆形’:
类型圆的计量对象的几何形状由其中心(行,列)和半径指定。
ShapeParam = [行,列,半径]
‘旋转矩形’:
矩形类型的计量对象的几何形状由其中心(行,列),主轴Phi的方向以及半边长度Length1和Length2指定。 Phi的输入值自动映射到间隔。
ShapeParam = [行,列,Phi,长度1,长度2]
‘椭圆’:
椭圆类型的计量对象的几何形状由其中心(行,列),主轴Phi的方向,较大的半轴Radius1的长度以及较小的半轴Radius2的长度来指定。 Phi的输入值自动映射到间隔。
ShapeParam = [行,列,Phi,Radius1,Radius2]
‘线’:
类型线的度量对象的几何形状由其起点(RowBegin,ColumnBegin)的坐标和其终点的坐标(RowEnd,ColumnEnd)描述。
ShapeParam = [RowBegin,ColumnBegin,RowEnd,ColumnEnd]
测量区域的定义
add_metrology_object_generic还准备矩形度量区域。矩形测量区域垂直于物体的边界。与对象边界垂直和相切的测量区域的半边长度在MeasureLength1和MeasureLength2中设置。测量区域的中心位于物体的边界上。参数MeasureSigma指定运算符apply_metrology_model用于平滑图像灰度值的标准偏差。可以使用参数MeasureThreshold选择突出边缘,该参数构成幅度的阈值,即边缘的一阶导数的绝对值。
通用参数
可以使用GenParamName和GenParamValue指定通用参数及其值。运算符set_metrology_object_param中可用的所有通用参数也可以在add_metrology_object_generic中设置。但请注意,对于许多应用程序,默认值足够,无需进行任何调整。此外,GenParamName和GenParamValue的以下值仅适用于Shape =‘circle’和’ellipse’:
多线程类型:可重入(与非独占运算符并行运行)。
多线程范围:全局(可以从任何线程调用)。
没有并行化处理。
此运算符修改以下输入参数的状态:
MetrologyHandle
没有外部同步,可能无法在多个线程之间共享此参数的值。
参数
MetrologyHandle(input_control,状态被修改)metrology_model→(整数)
处理计量模型。
Shape(input_control)attribute.name(-array)→(string)
要添加的计量对象的类型。
默认值:‘circle’
值列表:‘circle’,‘ellipse’,‘line’,‘rectangle2’
ShapeParam(input_control)attribute.value-array→(实/整数)
要添加的计量对象的参数。
MeasureLength1(input_control)数字→(实/整数)
垂直于边界的测量区域的一半长度。
默认值:20.0
建议值:10.0,20.0,30.0
典型值范围:1.0≤MeasureLength1≤511.0(lin)
最小增量:1.0
建议增量:10.0
MeasureLength2(input_control)数字→(实/整数)
与边界相切的测量区域的一半长度。
默认值:5.0
建议值:3.0,5.0,10.0
典型值范围:1.0≤MeasureLength2≤511.0(lin)
最小增量:1.0
建议增量:10.0
MeasureSigma(input_control)数字→(实/整数)
Sigma的高斯函数用于平滑。
默认值:1.0
建议值:0.4,0.6,0.8,1.0,1.5,2.0,3.0,4.0,5.0,7.0,10.0
典型值范围:0.4≤MeasureSigma≤100(lin)
最小增量:0.01
建议增量:0.1
限制:(0.4 <= MeasureSigma)&&(MeasureSigma <= 100)
MeasureThreshold(input_control)数字→(实/整数)
最小边缘幅度。
默认值:30.0
建议值:5.0,10.0,20.0,30.0,40.0,50.0,60.0,70.0,90.0,110.0
典型值范围:1≤MeasureThreshold≤255(lin)
最小增量:0.5
建议增量:2
GenParamName(input_control)attribute.name(-array)→(string)
通用参数的名称。
默认值: []
值列表:‘distance_threshold’,‘end_phi’,‘instances_outside_measure_regions’,‘max_num_iterations’,‘measure_distance’,‘measure_interpolation’,‘measure_select’,‘measure_transition’,‘min_score’,‘num_instances’,‘num_measures’,'point_order ',‘rand_seed’,‘start_phi’
‘start_phi’:
该参数指定圆弧或椭圆弧的起点处的角度。对于椭圆,起点处的角度是相对于正主轴测量的,并且对应于椭圆的最小周围圆。椭圆的实际起点是椭圆与相应圆点在主轴上的正交投影的交点。要创建闭合圆或椭圆,参数’start_phi’的值设置为0,参数’end_phi’的值设置为(带正点顺序)。输入值自动映射到间隔。
值列表:0.0,0.78,6.28318
默认值:0.0
‘end_phi’:
该参数指定圆弧或椭圆弧的终点处的角度。对于椭圆,终点处的角度是相对于正主轴测量的,并且对应于椭圆的最小周围圆。椭圆的实际终点是椭圆与相应圆点在主轴上的正交投影的交点。要创建闭合圆或椭圆,参数’start_phi’的值设置为0,参数’end_phi’的值设置为(带正点顺序)。输入值自动内部映射到间隔。
值列表:0.0,0.78,6.28318
默认值:6.28318
‘point_order’:
该参数指定圆弧或椭圆弧的方向。对于值“正”,弧在数学正方向(逆时针方向)的’start_phi’和’end_phi’之间定义。对于值“负”,弧在数学上为负方向(顺时针)定义在’start_phi’和’end_phi’之间。
价值表:‘正面’,‘负面’
默认值:‘正面’
GenParamValue(input_control)attribute.value(-array)→(real / integer / string)
通用参数的值。
默认值: []
建议值:1,2,3,4,5,10,20,‘all’,‘true’,‘false’,‘first’,‘last’,‘positive’,‘negative’,‘uniform’,’ nearest_neighbor’,‘bilinear’,‘bicubic’
索引(output_control)整数→(整数)
创建的计量对象的索引。
2.3.3 Halcon找线找圆的设置参数用法总结
set_metrology_object_param
2.3.3.1
- 可以在一次调用中为每个计量对象测量多个圆 / 矩形 / 线 / 椭圆 * 实例。 * 因为测量两个圆,* 我们将‘ num instances’设置为2。
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘num_instances’, 2) - 设定「测量过渡」至「均匀」可确保只会返回唯一的圆圈,即只有由亮到暗的边缘或由亮到暗的边缘。 因为一致性检查增加了运行时间,所以从默认切换为均匀姿态uniform。 在这个例子中,因为同时存在正面和负面的边缘,打开它更安全。
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘measure_transition’, ‘uniform’) - Setting the minimum score can make the results more robust 确保结果更可靠
set_metrology_object_param (MetrologyHandle, MetrologyCircleIndices, ‘min_score’, .9)
2.3.4对一个图像进行测量并且匹配指定的测量对象
//对一个图像进行测量并且匹配指定的测量对象
- Perform the measurement进行测量
apply_metrology_model (Image, MetrologyHandle)
2.3.5 获取测量结果
序号 算子 注释
1 get_metrology_object_fuzzy_param 模糊参数
2 get_metrology_object_indices 指数
3 get_metrology_object_measures 获取测量点和矩形外观
4 get_metrology_object_model_contour 模型轮廓
5 get_metrology_object_num_instances 数字实例
6 get_metrology_object_param 参数
7 get_metrology_object_result 结果
8 get_metrology_object_result_contour 获取结果轮廓
矩形:
输入:MetrologyHandle, MetrologyRectangleIndices, ‘all’, ‘result_type’, ‘all_param’
输出:RectangleParameter
get_metrology_object_result (MetrologyHandle, MetrologyRectangleIndices, ‘all’, ‘result_type’, ‘all_param’, RectangleParameter)
对 句柄 MetrologyHandle中的 [0,1] 进行测量
Sequence1 := [0:5:|RectangleParameter| - 1]
*这里是5个数值
RectangleRow := RectangleParameter[Sequence1]
RectangleColumn := RectangleParameter[Sequence1 + 1]
RectanglePhi := RectangleParameter[Sequence1 + 2]
RectangleLength1 := RectangleParameter[Sequence1 + 3]
RectangleLength2 := RectangleParameter[Sequence1 + 4]
圆形:
- Access the results of the circle measurement
get_metrology_object_result (MetrologyHandle, MetrologyCircleIndices, ‘all’, ‘result_type’, ‘all_param’, CircleParameter) - Extract the parameters for better readability
Sequence2 := [0:3:|CircleParameter| - 1]
*这里是3个数值
CircleRow := CircleParameter[Sequence2]
CircleColumn := CircleParameter[Sequence2 + 1]
CircleRadius := CircleParameter[Sequence2 + 2]
2.3.6 get_metrology_object_measures
*获取测量区域的轮廓和坐标,这些坐标是拟合圆和矩形的基础
获取测量点和矩形外观
get_metrology_object_measures (Contour, MetrologyHandle, ‘all’, ‘all’, Row1, Column1)
- get_metrology_object
dev_clear_window()
dev_set_draw(‘margin’)
dev_display(Image)
dev_display(Contour)
2.3.7显示结果
*清除模型
- Clean up memory
clear_metrology_model (MetrologyHandle)
3.应用案列-实战讲解1
钻石角度测量讲解
通过形态学 找到基准。
*设置矩形框的宽度
Tolerance := 120
-
Create two metrology line objects and set parameters
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1) -
Create two metrology line objects and set parameters
add_metrology_object_line_measure (MetrologyHandle, LineRow1, LineColumn1, LineRow2, LineColumn2, Tolerance, 10, 1, 20, [], [], Index1) -
Create region of interest for the alignment
gen_rectangle1 (Rectangle, LineRow1[0] - 40, LineColumn1[0] - 50, LineRow1[0] + 20, LineColumn1[0] + 50) -
Change the reference coordinate system in which the
-
metrology model is given to be situated at the top of the diamond
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 128, 255)
*区域换算成点
get_region_points (Region, Rows, Columns)
*设置参考系统
dev_set_color(‘red’)
gen_cross_contour_xld(Cross4,Rows[0],Columns[0],16, 0.785398)
set_metrology_model_param (MetrologyHandle, ‘reference_system’, [Rows[0],Columns[0],0])
read_image (Image, 'diamond/diamond_' + I$'02')
* Roughly segment the diamond's position
reduce_domain (Image, Rectangle, ImageReduced)
threshold (ImageReduced, Region, 128, 255)
* Extract the top of the diamond
get_region_points (Region, Rows, Columns)
*
* Use the top of the diamond to align the metrology model in
* the current image
*
align_metrology_model (MetrologyHandle, Rows[0], Columns[0], 0)
*找到测量的结果,求取曲线
4.案例讲解2
测量冲压件
-
Choose an alignment method by activating the desired line
*通过选择所需的模式来选择一个校准方法
AlignmentMode := ‘shape-based matching’ -
AlignmentMode := ‘region processing’
-
AlignmentMode := ‘rigid transformation’
-
Add several full circles
*生成4个圆形
CircleParam := [354,274,53]
CircleParam := [CircleParam,350,519,53]
CircleParam := [CircleParam,345,764,52]
CircleParam := [CircleParam,596,523,53]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [], [], CircleIndices1)
*
-
Add two partial circles
*生成2个圆弧
CircleParam1 := [583,1010,79]
CircleParam2 := [336,1005,77]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam1, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam2, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3] -
Add a retangle
*生成1个矩形
RectangleParam := [599,279,rad(90),62,51]
add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices) -
Add two lines
*生成2条线
Line1 := [143,1122,709,1132]
Line2 := [151,153,136,1115]
add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
第2步 * 检查添加到测量模型内的形状
dev_clear_window()
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)
get_metrology_object_measures (MeasureContour, MetrologyHandle, ‘all’, ‘all’, Row, Column)
第3步 显示 Message := ‘This example shows how to measure geometric shapes using a’
Message[1] := 'metrology model. As preparation, their roughly known ’
Message[2] := ‘dimensions and tolerances are specified by the user.’
*作为准备工作,它们大致上已知的尺寸和公差由用户指定
show_contours (Image, ModelContour, MeasureContour, EmptyObject, WindowHandle, Message)
第4步 两个算子之间的区别
*
dev_clear_window()
dev_display(Image)
get_metrology_object_measures (Contour, MetrologyHandle, ‘all’, ‘all’, MRow, MColumn)
gen_cross_contour_xld (Cross, MRow, MColumn, 6, rad(45))
stop()
* Get the edge points that were actually used to fit the
* geometric forms
*方法2 —获取edges 上的点
dev_clear_window()
dev_display(Image)
get_metrology_object_result (MetrologyHandle, ‘all’, ‘all’, ‘used_edges’, ‘row’, UsedRow)
get_metrology_object_result (MetrologyHandle, ‘all’, ‘all’, ‘used_edges’, ‘column’, UsedColumn)
gen_cross_contour_xld (UsedEdges, UsedRow, UsedColumn, 10, rad(45))、
方法1 —杂乱的点较多
方法2 —没有干扰的点 (‘used_edges’)
第5步 获取结果并且显示
* Get a visualization of the measured geometric forms
*查询计量对象的结果轮廓
get_metrology_object_result_contour (ResultContours, MetrologyHandle, ‘all’, ‘all’, 1.5)
*获取结果和上面的初始设置 一一对应
*生成4个圆形
*前两位是圆心的Row值和Column值,后一位是半径
CircleParam := [354,274,53]
CircleParam := [CircleParam,350,519,53]
CircleParam := [CircleParam,345,764,52]
CircleParam := [CircleParam,596,523,53]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [], [], CircleIndices1)
*
-
Add two partial circles
*生成2个圆弧
CircleParam1 := [583,1010,79]
CircleParam2 := [336,1005,77]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam1, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam2, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3] -
Add a retangle
*生成1个矩形
RectangleParam := [599,279,rad(90),62,51]
add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices) -
Add two lines
*生成2条线
Line1 := [143,1122,709,1132]
Line2 := [151,153,136,1115]
add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
get_metrology_object_result (MetrologyHandle, CircleIndices1, 'all', 'result_type', 'radius', RadiusC1)
* Extract the radius of the larger incomplete circles
get_metrology_object_result (MetrologyHandle, CircleIndices2, 'all', 'result_type', 'radius', RadiusC2)
* Extract the lengths of the rectangle sides
get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length1', Length1R)
get_metrology_object_result (MetrologyHandle, RectIndices, 'all', 'result_type', 'length2', Length2R)
* Get the start and end point of each measured line
get_metrology_object_result (MetrologyHandle, LineIndices[0], 'all', 'result_type', 'all_param', ParamLine1)
get_metrology_object_result (MetrologyHandle, LineIndices[1], 'all', 'result_type', 'all_param', ParamLine2)
转载:https://blog.csdn.net/weixin_45247360/article/details/105932873