Halcon学习笔记:select_points_object_model_3d 3D对象模型阀值分割
-
This example program shows how to use the operators
-
connection_object_model_3d and select_object_model_3d
-
in HALCON. First, a threshold is applied to the 3D object
-
model. Then, the connected components are calculated
-
based on the geometric distance. Finally, the connected
-
components are selected by their volume and diameter and
-
the result is displayed.
-
这个示例程序展示了如何在HALCON中使用connection_object_model_3d和select_object_model_3d操作符。
-
首先,对3D对象模型应用一个阈值。然后,根据几何距离计算连通分量。
-
最后,根据连接部件的体积和直径选择连接部件并显示结果。
源码:
dev_update_off ()
dev_close_window ()
* 1、加载X,Y, z数据,缩放到米,并生成一个3D对象模型
ImagePath := 'time_of_flight/'
read_image (Image, ImagePath + 'engine_cover_xyz_01')
* 缩放图像的灰度值
* 将图像放大一个给定的倍数
scale_image (Image, Image, .001, .0)
zoom_image_factor (Image, Image, 2, 2, 'constant')
decompose3 (Image, X, Y, Z)
xyz_to_object_model_3d (X, Y, Z, ObjectModel3DID)
* 2、计算模型的网格(Delaunay三角测量)
* 为'segmentation',准备一个3D对象模型(如果要对同一操作多次使用3D对象模型,这样做会更快)
prepare_object_model_3d (ObjectModel3DID, 'segmentation', 'true', [], [])
*
* 3、准备可视化并显示3D对象模型
create_pose (0.058, -0.165, 0.660, 345.0, 355.0, 356.0, 'Rp+T', 'gba', 'point', Pose)
*
* Instructions for visualize_object_model_3d
Instructions[0] := 'Rotate: Left button'
Instructions[1] := 'Zoom: Shift + left button'
Instructions[2] := 'Move: Ctrl + left button'
* Configuration
gen_cam_par_area_scan_division (0.01, 0, 7e-6, 7e-6, 352, 288, 710, 576, CamParam)
GenParamName := ['color','disp_pose','alpha']
GenParamValue := ['green','false',0.8]
*
dev_open_window (0, 0, 710, 576, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
*
visualize_object_model_3d (WindowHandle, ObjectModel3DID, CamParam, Pose, GenParamName, GenParamValue, 'This scene will be segmented into single objects', [], Instructions, Pose)
*
* 4、阈值三维对象模型
MinValue := 0.500
MaxValue := 0.670
* 对3D对象模型应用阈值
select_points_object_model_3d (ObjectModel3DID, 'point_coord_z', MinValue, MaxValue, ObjectModel3DIDReduced)
visualize_object_model_3d (WindowHandle, ObjectModel3DIDReduced, CamParam, Pose, GenParamName, GenParamValue, 'Result after thresholding at z=' + (MaxValue * 1e3)$'.3' + 'mm from the camera', [], Instructions, Pose)
*
* Calculate the connected components and the volume and diameter
* of each of the resulting object
* 5、计算连接分量和每个结果对象的体积和直径
GenParamName[0] := 'colored'
GenParamValue[0] := 12
* 1)确定3D对象模型的连通分量
* 2)计算三维物体模型相对于平面[0,0,MaxValue,0,0,0,0]的体积 Volume
* 3)计算3D对象模型的最大直径 Diameter
connection_object_model_3d (ObjectModel3DIDReduced, 'distance_3d', 0.010, ObjectModel3DIDConnections)
volume_object_model_3d_relative_to_plane (ObjectModel3DIDConnections, [0,0,MaxValue,0,0,0,0], 'signed', 'true', Volume)
max_diameter_object_model_3d (ObjectModel3DIDConnections, Diameter)
*
* 6、显示结果
dev_open_window (0, 720, 400, 576, 'black', WindowHandle1)
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
Indices := [0:|ObjectModel3DIDConnections| - 1]
disp_message (WindowHandle1, ['Features of the connected components:',' ',' '], 'window', 12, 12, 'white', 'false')
ResultMessage := ' # Max. diameter Volume'
Sequence := [0:|ObjectModel3DIDConnections| - 1]
ResultMessage := [ResultMessage,Sequence$' 3' + ' ' + (Diameter * 1e3)$'7.1f' + ' mm ' + (Volume * 1e3)$'7.3f' + ' dm³']
disp_message (WindowHandle1, ResultMessage, 'window', 50, 12, 'white', 'false')
dev_set_window (WindowHandle)
visualize_object_model_3d (WindowHandle, ObjectModel3DIDConnections, CamParam, Pose, GenParamName, GenParamValue, 'Found ' + |ObjectModel3DIDConnections| + ' connected components', '#' + Indices, Instructions, Pose)
*
* 7、根据组件的体积和最大直径选择组件
*
MinVolume := 0.35e-003
MaxVolume := 1.0e-003
MinDiameter := 185.0e-003
MaxDiameter := 300.0e-003***** 8、注意:
* select_object_model_3d使用平面[0,0,0,0,0,0]进行体积计算!
* 因此,我们必须转换3d对象模型,以便参考平面(如前面对volume_object_model_3d_relative_to_plane的调用中定义的那样)与默认平面重合。
*
* 1)生成三维变换的齐次变换矩阵
* 2)增加平移变换
* 3)求齐次三维变换矩阵的逆
hom_mat3d_identity (HomMat3DIdentity)
hom_mat3d_translate (HomMat3DIdentity, 0, 0, -MaxValue, HomMat3DTranslation)
hom_mat3d_invert (HomMat3DTranslation, HomMat3DInvert)
affine_trans_object_model_3d (ObjectModel3DIDConnections, HomMat3DTranslation, ObjectModel3DTranslated)
* 4)为每个部分设置标签
for Index := 0 to |ObjectModel3DIDConnections| - 1 by 1set_object_model_3d_attrib_mod (ObjectModel3DTranslated[Index], '&Index', [], Index)
endfor
* 5)再次计算三维物体模型相对于平面[0,0,0,0,0,0,0]的体积 Volume1
* 6)从一组三维对象模型中选择三维对象模型('volume','diameter_object')
volume_object_model_3d_relative_to_plane (ObjectModel3DTranslated, [0,0,0,0,0,0,0], 'signed', 'true', Volume1)
select_object_model_3d (ObjectModel3DTranslated, ['volume','diameter_object'], 'and', [MinVolume,MinDiameter], [MaxVolume,MaxDiameter], ObjectModel3DSelected)
*
Title := ['Parts selected by using the following features: ',' ' + (MinVolume * 1e3)$'3.1f' + ' dm³ <= volume <= ' + (MaxVolume * 1e3)$'3.1f' + ' dm³','and: ' + (MinDiameter * 1e3)$'.1f' + ' mm <= max. diameter <= ' + (MaxDiameter * 1e3)$'.1f' + ' mm']
* 创建一个标签
Label := []
for Index := 0 to |ObjectModel3DSelected| - 1 by 1get_object_model_3d_params (ObjectModel3DSelected[Index], '&Index', FormerIndex)Label := [Label,'#' + FormerIndex]
endfor
*
if (|ObjectModel3DSelected| > 1)visualize_object_model_3d (WindowHandle, ObjectModel3DSelected, CamParam, [], GenParamName, GenParamValue, Title, Label, Instructions, Pose)
elseMessage := 'No object left after using the following features: 'Message[1] := ' ' + (MinVolume * 1e3)$'3.1f' + ' dm³ <= volume <= ' + (MaxVolume * 1e3)$'3.1f' + ' dm³'Message[2] := 'and: ' + (MinDiameter * 1e3)$'.1f' + ' mm <= max. diameter <= ' + (MaxDiameter * 1e3)$'.1f' + ' mm'disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
endif
*
* Clear the 3d object models
dev_set_window (WindowHandle1)
dev_close_window ()
clear_object_model_3d ([ObjectModel3DID,ObjectModel3DIDReduced,ObjectModel3DSelected,ObjectModel3DTranslated,ObjectModel3DIDConnections])
结果
1、生成3D model
2、计算连通分量的体积和最大直径
3、选择连通分量