- 这个程序展示了如何使用计量模型来获得平面上各种几何形状的校准测量值。
- 几何形态参数由模型图像导出。
- 要在新图像中找到要测量的对象,三个不同的对齐方法如下:
*a)使用基于形状的匹配
*b)使用区域处理
*c)使用点对应的刚性变换
*测量结果以等高线形式返回
*作为给定几何对象的调整参数
*对象通过激活所需的线来打开对齐方法
AlignmentMode := ‘shape-based matching’
- AlignmentMode := ‘region processing’
- AlignmentMode := ‘rigid transformation’
*AlignmentMode:=“基于形状的匹配”
*AlignmentMode:=“区域处理”
*AlignmentMode:=“刚性变换”
*初始化
dev_update_off ()
dev_close_window ()
dev_set_draw (‘margin’)
gen_empty_obj (EmptyObject)
read_image (Image, ‘metal-parts/circle_plate_01’)
get_image_size (Image, Width, Height)
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)
-
设备更新关闭()
-
开发关闭窗口()
-
设置绘图边缘
-
生成空对象(空对象)
-
读取图像(图像,“ 地址 ”)
-
获取图像大小(图像、宽度、高度)
-
按指定图像的大小来打开一个窗口(目标图片,新窗口左上角列标,新窗口宽度高度限制,新图形窗口的窗口句柄)
-
设置显示字体(窗口句柄,字体大小,字体,粗体,斜体)
-
Part I:
***初始化计量模型
*定义相机参数
CameraParam := [0.0128649,-661.434,5.30004e-006,5.3e-006,620.043,497.402,Width,Height]
*测量平面的姿态是通过在被测物体的平面上放置一个校准板并通过确定其姿态来获得的
MeasurementPlane := [0.00940956,-0.00481017,0.29128,0.478648,359.65,0.785,0]
*测量模板
- *根据零件高度和校准板高度调整测量平面的姿态
- 校准板的厚度
CalibPlateThickness := 0.006
*校准板厚度:=0.006
*被测物体的厚度
PartHeight := 0.005
AdjustThickness := CalibPlateThickness - PartHeight
*零件高度:=0.005
*调整厚度:=校准板厚度-零件高度
set_origin_pose (MeasurementPlane, 0, 0, AdjustThickness, MeasurementPlaneAdjusted)
- 设置原点姿态(测量平面,0,0,调整厚度,测量平面调整)
*创建计量模型并为校准测量做好准备
create_metrology_model (MetrologyHandle)
*提前设置图像大小,以加速第一次调用apply_metrology_model.
set_metrology_model_image_size (MetrologyHandle, Width, Height)
*设置相机参数
set_metrology_model_param (MetrologyHandle, ‘camera_param’, CameraParam)
*设置被测物体所在的平面
set_metrology_model_param (MetrologyHandle, ‘plane_pose’, MeasurementPlaneAdjusted)
*
*将待测对象添加到计量模型中
*
*添加几个完整圆
*CircleParam := [354,274,53]
- CircleParam := [CircleParam,350,519,53]
*CircleParam := [CircleParam,345,764,52]
*CircleParam := [CircleParam,596,523,53]
dev_display (Image)
draw_circle (WindowHandle, Row3, Column3, Radius)
draw_circle (WindowHandle, Row4, Column4, Radius1)
draw_circle (WindowHandle, Row5, Column5, Radius2)
draw_circle (WindowHandle, Row6, Column6, Radius3)
CircleParam:=[Row3, Column3, Radius,Row4, Column4, Radius1,Row5, Column5, Radius2,Row6, Column6, Radius3]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [], [], CircleIndices1)
*添加两个部分圆
*CircleParam1 := [583,1010,79]
*CircleParam2 := [336,1005,77]
draw_circle (WindowHandle, Row8, Column8, Radius5)
draw_circle (WindowHandle, Row7, Column7, Radius4)
CircleParam:=[Row7, Column7, Radius4,Row8, Column8, Radius5]
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [0,rad(185)], CircleIndices2)
add_metrology_object_generic (MetrologyHandle, ‘circle’, CircleParam, 20, 5, 1, 30, [‘start_phi’,‘end_phi’], [rad(45),rad(185)], Index3)
CircleIndices2 := [CircleIndices2,Index3] //圆指数 //20,5垂直于边界的测量区域的一半长度.1用于平滑的高斯函数的.30最低边缘幅度,泛型参数的名称,泛型参数的值
*添加一个矩形
*RectangleParam := [599,279,rad(90),62,51]
draw_rectangle2 (WindowHandle, Row9, Column9, Phi, Length1, Length2)//中心的行坐标,列坐标.较大的半轴的方向以弧度表示,大的半轴.小的半轴.
RectangleParam :=[ Row9, Column9, Phi, Length1, Length2]
add_metrology_object_generic (MetrologyHandle, ‘rectangle2’, RectangleParam, 20, 5, 1, 30, [], [], RectIndices)
- 添加两条线
*Line1 := [143,1122,709,1132]
*Line2 := [151,153,136,1115]
draw_line (WindowHandle, Row11, Column11, Row21, Column21)
draw_line (WindowHandle, Row12, Column12, Row22, Column22)
Line1:=[Row11, Column11, Row21, Column21]
Line2:=[Row12, Column12, Row22, Column22]
add_metrology_object_generic (MetrologyHandle, ‘line’, [Line1,Line2], 20, 5, 1, 30, [], [], LineIndices)
- 检查已添加到计量模型的形状
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)//all计量对象的索引,1.5相邻等高线点之间的距离。
get_metrology_object_measures (MeasureContour, MetrologyHandle, ‘all’, ‘all’, Row, Column)//all计量对象的索引,all选择浅色/深色或深色/浅色边缘,行,lie
Message := ‘This example shows how to measure geometric shapes using a’//此示例显示如何使用a测量几何形状
Message[1] := 'metrology model. As preparation, their roughly known '//消息[1]:='计量模型。作为准备,他们大概知道
Message[2] := ‘dimensions and tolerances are specified by the user.’//消息[2]:=“尺寸和公差由用户指定。”
show_contours (Image, ModelContour, MeasureContour, EmptyObject, WindowHandle, Message)//显示轮廓(图像、模型轮廓、测量轮廓、空对象、WindowHandle、消息)
stop ()
- Part 2:
*准备对准
*
*a)基于形状的匹配
if (AlignmentMode == ‘shape-based matching’)//基于形状的匹配
dev_set_part (-Height / 2 - 100, -Width / 2, 1.5 * Height - 100, 1.5 * Width)
*
*创建用于在图像中校准该计量模型的形状模型,
*其中对象发生的位置和方向与用于创建该模型的图像中的位置和方向不同
get_system (‘border_shape_models’, BorderShapeModel)
set_system (‘border_shape_models’, ‘true’)
threshold (Image, Region, 0, 50)//使用全局阈值分割图像(输入图像,分割区域,灰度值阈值最低值,灰度值的最高值)
dilation_rectangle1 (Region, ModelRegion, 5, 5)//膨胀:用矩形结构元素扩展区域(用矩形结构元素扩展区域,扩张的地区,结构矩形的宽度,规整矩形的高度)
reduce_domain (Image, ModelRegion, ImageReduced)//缩小图像的域(输入图像,新定义的域,图像与简化的定义域)
create_shape_model (ImageReduced, 6, 0, rad(360), ‘auto’, ‘auto’, ‘use_polarity’, ‘auto’, 20, ShapeModelID)
*准备一个形状模型进行匹配(输入图像其域将用于创建模型,金字塔层的最大数目,最小的图案旋转,旋转角度的范围,角度步长(分辨率),一种优化和可选的方法用于生成模型,路由的度量)
*形状模型的原点是create_shape_model中使用的输入区域的中心
area_center (ModelRegion, Area, RowModel, ColumnModel)
get_shape_model_contours (ShapeModelContours, ShapeModelID, 1)
Message := ‘A shape model will be used for the alignment of the metrology’//将使用一个形状模型来校准计量
Message[1] := ‘model. The contours of the shape model (white) and of the’//形状模型的轮廓(白色)和模型的轮廓
Message[2] := ‘metrology model (blue) are shown.’//显示计量模型(蓝色)
show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)
*
*改变定义计量模型的参考系,使其与形状模型所使用的参考系相对应。
*为了能够直接使用find_shape_model的结果来校准计量模型,这是必要的
set_metrology_model_param (MetrologyHandle, ‘reference_system’, [RowModel,ColumnModel,0])//设置对整个计量模型有效的参数(计量模型的处理,泛型参数的名称,泛型参数的值)
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)//以图像坐标查询计量对象的模型轮廓(模型轮廓,计量模型的处理,计量对象的索引,相邻等高线点之间的距离)
Message := ‘To prepare the alignment, the origin of the shape model’//信息:= ‘要准备对齐,请选择形状模型的原点’
Message[1] := ‘is set as the reference system of the metrology model.’//信息[1]:= ‘设置为计量模型参考系统。’
show_contours (Image, ModelContour, EmptyObject, ShapeModelContours, WindowHandle, Message)
stop ()
dev_set_part (0, 0, Height - 1, Width - 1)
endif
*
*
- 在线阶段
for I := 2 to 5 by 1
read_image (CurrentImage, ‘metal-parts/circle_plate_’ + I$‘02d’)
dev_set_line_width (1)//定义区域轮廓输出的线宽
dev_display (CurrentImage)//在当前图形窗口中显示图像对象
*
-
a) Shape-based匹配
if (AlignmentMode == ‘shape-based matching’)//如果(对齐方式 == ‘基于形状的匹配’)’
*
*使用形状模型测量当前图像中物体的位置和方向
find_shape_model (CurrentImage, ShapeModelID, 0, rad(360), 0.5, 1, 0, ‘least_squares’, 5, 0.9, RowAlign, ColumnAlign, AngleAlign, Score)
*在图像中找到形状模型的最佳匹配(输入图像其中应该找到模型,模型手柄,起始角,旋转角度的范围,要找到的模型实例的最小得分,要找到的模型实例数(对于所有匹配项,为0),要找到的模型实例的最大重叠,列,行,旋转角度,找到的模型实例的得分)
dev_display_shape_matching_results (ShapeModelID, ‘white’, RowAlign, ColumnAlign, AngleAlign, 1, 1, 0)
endif
*使用计算的位置和方向将计量模型与其当前引用对齐
align_metrology_model (MetrologyHandle, RowAlign, ColumnAlign, AngleAlign)//校准计量模型(计量模型的处理,行,列,旋转角)
if (I <= 2)
get_metrology_object_model_contour (ModelContour, MetrologyHandle, ‘all’, 1.5)//以图像坐标查询计量对象的模型轮廓(模型轮廓,计量模型的处理,计量对象的索引,相邻等高线点之间的距离)
dev_set_color (‘blue’)
dev_set_line_width (2)
dev_display (ModelContour)
Message := ‘In each image, the object is matched and aligned’
Message[1] := ‘before the metrology measurement.’
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
*
*在一次命令中对所有计量对象进行测量
apply_metrology_model (CurrentImage, MetrologyHandle)//测量并拟合一个计量模型中所有计量对象的几何形状()
- 获取用于可视化的度量区域
get_metrology_object_measures (Contour, MetrologyHandle, ‘all’, ‘all’, Row, Column)//(计量对象的索引,选择浅色/深色或深色/浅色边缘)
- 得到实际用于拟合几何图形的边缘点
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))
*获取自摄像机参数设定后的测量结果,
*所有结果均以公制坐标系给出,由测量平面定义坐标系,得到被测几何图形的可视化
get_metrology_object_result_contour (ResultContours, MetrologyHandle, ‘all’, ‘all’, 1.5)
- 提取较小圆的半径
get_metrology_object_result (MetrologyHandle, CircleIndices1, ‘all’, ‘result_type’, ‘radius’, RadiusC1)
- 提取较大的不完整圆的半径
get_metrology_object_result (MetrologyHandle, CircleIndices2, ‘all’, ‘result_type’, ‘radius’, RadiusC2)
- 提取矩形边的长度
get_metrology_object_result (MetrologyHandle, RectIndices, ‘all’, ‘result_type’, ‘length1’, Length1R)
get_metrology_object_result (MetrologyHandle, RectIndices, ‘all’, ‘result_type’, ‘length2’, Length2R)
- 得到每一条被测线的起点和终点
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)
- 显示结果
dev_display (CurrentImage)
dev_set_line_width (1)
dev_set_color (‘light gray’)
dev_display (Contour)
dev_set_color (‘green’)
dev_set_line_width (2)
dev_display (ResultContours)
dev_set_line_width (1)
dev_set_color (‘white’)
dev_display (UsedEdges)
*显示圆心处每个圆的半径获取圆心的公制坐标
get_metrology_object_result (MetrologyHandle, CircleIndices1, ‘all’, ‘result_type’, ‘x’, XC1)
get_metrology_object_result (MetrologyHandle, CircleIndices1, ‘all’, ‘result_type’, ‘y’, YC1)
*将圆心的度量坐标投影到图像中,得到圆心的图像坐标
project_xy_to_image (XC1, YC1, MeasurementPlaneAdjusted, CameraParam, Row1, Column1)
get_metrology_object_result (MetrologyHandle, CircleIndices2, ‘all’, ‘result_type’, ‘x’, XC2)//得到一个计量模型的测量结果
get_metrology_object_result (MetrologyHandle, CircleIndices2, ‘all’, ‘result_type’, ‘y’, YC2)
project_xy_to_image (XC2, YC2, MeasurementPlaneAdjusted, CameraParam, Row2, Column2)
for J := 0 to |RadiusC1| - 1 by 1
disp_message (WindowHandle, ‘r=’ + (RadiusC1[J] * 1000) ′ . 2 f ′ , ′ i m a g e ′ , R o w 1 [ J ] , C o l u m n 1 [ J ] − 80 , ′ b l a c k ′ , ′ t r u e ′ ) e n d f o r f o r J : = 0 t o ∣ R a d i u s C 2 ∣ − 1 b y 1 d i s p m e s s a g e ( W i n d o w H a n d l e , ′ r = ′ + ( R a d i u s C 2 [ J ] ∗ 1000 ) '.2f', 'image', Row1[J], Column1[J] - 80, 'black', 'true') endfor for J := 0 to |RadiusC2| - 1 by 1 disp_message (WindowHandle, 'r=' + (RadiusC2[J] * 1000) ′.2f′,′image′,Row1[J],Column1[J]−80,′black′,′true′)endforforJ:=0to∣RadiusC2∣−1by1dispmessage(WindowHandle,′r=′+(RadiusC2[J]∗1000)’.2f’, ‘image’, Row2[J], Column2[J] - 80, ‘black’, ‘true’)
endfor
get_metrology_object_result (MetrologyHandle, RectIndices, ‘all’, ‘result_type’, ‘x’, XRectangle)//RectIndices矩形指数的意思,计量对象的实例,泛型参数的名称,泛型参数的值,结果值)
get_metrology_object_result (MetrologyHandle, RectIndices, ‘all’, ‘result_type’, ‘y’, YRectangle)
project_xy_to_image (XRectangle, YRectangle, MeasurementPlaneAdjusted, CameraParam, RowR, ColumnR)
Area := Length1R * Length2R * 4 * 1000 * 1000
disp_message (WindowHandle, ‘area=’ + Area$’.2f’, ‘image’, RowR, ColumnR - 120, ‘black’, ‘true’)
Message := ‘Measured metric results after alignment (r in mm, area in mm^2):’//信息:= ‘对齐后测量的公制结果(r单位为mm,面积单位为mm^2):’
*
disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)//这个程序写一条短信(12行,列,每个文本循环使用的颜色,如果设置为“true”,文本将写入一个白框中)
if (I < 5)
disp_continue_message (WindowHandle, ‘black’, ‘true’)//此过程将在屏幕右下角显示“单击”Run“继续(定义文本颜色,如果设置为“true”,文本将写入一个白框中)
endif
stop ()
endfor
- 清理内存
clear_metrology_model (MetrologyHandle)//删除度量模型并释放分配的内存
if (AlignmentMode == ‘shape-based matching’)//if (AlignmentMode == ‘基于形状的匹配’)
clear_shape_model (ShapeModelID)//释放形状模型的内存
set_system (‘border_shape_models’, BorderShapeModel)//设置HALCON系统参数
endif