halcon三维点云数据处理(七)find_shape_model_3d_recompute_score
目录
- 一、find_shape_model_3d_recompute_score例程代码
- 二、set_object_model_3d_attrib_mod函数
- 三、prepare_object_model_3d 函数
- 四、create_cube_shape_model_3d函数
- 五、获得CamPose
- 六、project_cube_image函数
- 七、find_shape_model_3d函数
- 八、project_shape_model_3d函数
一、find_shape_model_3d_recompute_score例程代码
gen_cam_par_area_scan_division (0.01, 0, 7e-6, 7e-6, 320, 240, 640, 480, CamParam)
get_cam_par_data (CamParam, 'image_width', Width)
get_cam_par_data (CamParam, 'image_height', Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_update_off ()
*
* Generate a cube
X := [-1, -1, 1, 1, -1, -1, 1, 1]
Y := [1, -1, -1, 1, 1, -1, -1, 1]
Z := [-1, -1, -1, -1, 1, 1, 1, 1]
Polygons := [4, 0, 1, 2, 3, 4, 4, 5, 6, 7, 4, 0, 1, 5, 4, 4, 3, 2, 6, 7, 4, 0, 3, 7, 4, 4, 1, 2, 6, 5]
gen_object_model_3d_from_points (X, Y, Z, ObjectModel3DID)
set_object_model_3d_attrib_mod (ObjectModel3DID, 'polygons', [], Polygons)
*
* Prepare the 3D object model for matching
prepare_object_model_3d (ObjectModel3DID, 'shape_based_matching_3d', 'true', [], [])
*
* Display the 3D object model
disp_object_model_3d_safe (WindowHandle, ObjectModel3DID, CamParam, [0, 0, 11, 30, 40, 0, 0], [], [])
disp_message (WindowHandle, '3D Object Model of a Cube', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
*
* Generate the 3D shape model
create_cube_shape_model_3d (WindowHandle, ObjectModel3DID, CamParam, ShapeModel3DID)
*
Scores1 := []
Scores2 := []
Times1 := []
Times2 := []
Longitudes := []
Latitude := rad(45)
Distance := 11.5
dev_set_color ('green')
dev_set_line_width (2)
for Longitude := rad(30) to rad(60) by rad(0.25)
convert_point_3d_spher_to_cart (Longitude, Latitude, Distance, '-y', '-z', CamX, CamY, CamZ)
create_cam_pose_look_at_point (CamX, CamY, CamZ, 0, 0, 0, '-y', 0, CamPose)
* Prevent flickering
set_system ('flush_graphic', 'false')
*
* Generate an artificial image of the cube
project_cube_image (Image, CamPose, CamParam, X, Y, Z, Polygons)
*
* Find the cube by using 3D shape-based matching
* ('recompute_score' = 'false')
count_seconds (Seconds1)
find_shape_model_3d (Image, ShapeModel3DID, 0.7, 0.9, 0, [], [], Pose1, CovPose1, Score1)
count_seconds (Seconds2)
Time1 := (Seconds2 - Seconds1) * 1000.0
* Find the cube by using 3D shape-based matching
* ('recompute_score' = 'true')
count_seconds (Seconds1)
find_shape_model_3d (Image, ShapeModel3DID, 0.7, 0.9, 0, 'recompute_score', 'true', Pose2, CovPose2, Score2)
count_seconds (Seconds2)
Time2 := (Seconds2 - Seconds1) * 1000.0
*
* Display the matching result
dev_display (Image)
if (|Score1| == 1 and |Score2| == 1)
* Display the match
project_shape_model_3d (ModelContours, ShapeModel3DID, CamParam, Pose1, 'true', rad(30))
dev_display (ModelContours)
* Display the matching time and score
disp_message (WindowHandle, 'Matching at Longitude: ' + deg(Longitude)$'3.1f' + ' deg (from 30 to 60 deg)', 'window', 12, 12, 'white', 'false')
gen_matching_result_message (Time1, Time2, Score1, Score2, Message)
disp_message (WindowHandle, Message, 'window', 420, 12, ['white', 'yellow', 'green'], 'false')
* Remember the score values
Longitudes := [Longitudes,deg(Longitude)]
Scores1 := [Scores1,Score1]
Scores2 := [Scores2,Score2]
Times1 := [Times1,Time1]
Times2 := [Times2,Time2]
endif
* Prevent flickering
set_system ('flush_graphic', 'true')
* Show what we painted in the loop by drawing a invisible line
disp_line (WindowHandle, -1, -1, -1, -1)
endfor
*
* Display the score values
dev_clear_window ()
Title := ['\'recompute_score\'=\'false\'', '\'recompute_score\'=\'true\'']
Colors := ['yellow', 'green']
plot_tuple (WindowHandle, Longitudes, [Scores1,Scores2], 'Longitude [deg]', 'Matching score', Colors, ['ticks_x', 'ticks_y', 'end_y'], [6, .01, 1])
disp_message (WindowHandle, Title, 'window', 12, 350, Colors, 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_clear_window ()
plot_tuple (WindowHandle, Longitudes, [Times1,Times2], 'Longitude [deg]', 'Matching time [ms]', Colors, 'ticks_x', 6)
disp_message (WindowHandle, Title, 'window', 12, 350, Colors, 'false')
二、set_object_model_3d_attrib_mod函数
set_object_model_3d_attrib_mod( : : ObjectModel3D, AttribName, AttachExtAttribTo, AttribValues : )
set_object_model_3d_attrib_mod 函数将给定的 AttribName 中的标准属性或扩展属性设置为 AttribValues 中的值。set_object_model_3d_attrib_mod 与 set_object_model_3d_attrib 函数相同,不同之处在于它不会创建新的 3D 对象模型,而是修改给定的对象模型。通过设置参数 AttachExtAttribTo,可以将扩展属性的值附加到 3D 对象模型已有的标准属性上。对于标准属性,AttachExtAttribTo 参数将被忽略。
如果在 AttribName 中的属性不存在,则在可能的情况下创建它们。如果已存在的属性被设置,那么 AttribValues 的长度必须与现有的属性值长度相匹配。在这种情况下,现有的属性值将被替换。如果使用 AttachExtAttribTo 将扩展属性附加到已存在的标准属性上,那么 AttribValues 的长度必须与现有的属性值长度相匹配
‘polygons’:
上面的程序中设置的’polygons’,我个人认为是很难理解的。
Polygons := [4, 0, 1, 2, 3, 4, 4, 5, 6, 7, 4, 0, 1, 5, 4, 4, 3, 2, 6, 7, 4, 0, 3, 7, 4, 4, 1, 2, 6, 5]
set_object_model_3d_attrib_mod (ObjectModel3DID, 'polygons', [], Polygons)
这里的Polygons 是点数+ 点的索引。4个点(0,1,2,3),接下来的 4个点(4,5,6,7),这里的Polygons 表示的是正方体的6个面
三、prepare_object_model_3d 函数
为某一操作准备三维对象模型
它计算操作所需的值并将其存储在ObjectModel3D中,从而加快了后续操作。没有必要调用prepare_object_model_3d。但是,如果要多次使用3D对象模型进行相同的操作,则可以更快。
prepare_object_model_3d( : : ObjectModel3D, Purpose, OverwriteData, GenParamName, GenParamValue : )
prepare_object_model_3d (ObjectModel3DID, 'shape_based_matching_3d', 'true', [], [])
Purpose: ‘shape_based_matching_3d’, ‘segmentation’, ‘distance_computation’, ‘gen_xyz_mapping’
四、create_cube_shape_model_3d函数
生成3D形状模型,这是一个自定义函数
五、获得CamPose
在球坐标系上选取一点作为相机位置,转换为笛卡尔坐标。再通过create_cam_pose_look_at_point 函数确定相机位姿。
convert_point_3d_spher_to_cart (Longitude, Latitude, Distance, '-y', '-z', CamX, CamY, CamZ)
create_cam_pose_look_at_point (CamX, CamY, CamZ, 0, 0, 0, '-y', 0, CamPose)
六、project_cube_image函数
把立方体的三个面,投影到图像上。
七、find_shape_model_3d函数
在图像中找到3D形状模型的最佳匹配
八、project_shape_model_3d函数
将三维形状模型的边缘投影到图像坐标中。