dev_update_off()** Parameters of the 2D camera.* 设置相机参数
gen_cam_par_area_scan_division(0.003,-22215.7,1e-6,1e-6,629.3,486.95,1280,960, CameraParam)get_cam_par_data(CameraParam,'image_width', Width)get_cam_par_data(CameraParam,'image_height', Height)* The camera is located in the origin of the 3D sensor.create_pose(0,0,0,0,0,0,'Rp+T','gba','point', CameraPose)* Matching parameters.* 最小匹配得分参数
MinScore :=0.2* Visualization parameters.* 创建位姿
create_pose(-0.075,-0.56,4.4,308,6,335,'Rp+T','gba','point', VisPose)
VisParamNames :=['color','color_0','alpha','alpha_0']
VisParamValues :=['red','white',0.5,1]
WidthW1 :=400
LineLength :=32
VisRowNextStep :=300
VisInstructions[0]:='Rotate: Left button'
VisInstructions[1]:='Zoom: Shift + left button'
VisInstructions[2]:='Move: Ctrl + left button'* Prepare windows.dev_close_window()dev_open_window_fit_size(0, WidthW1 +10, Width, Height,640,480, WindowHandle2)get_window_extents(WindowHandle2, Row, Column, WidthW2, HeightW2)dev_open_window(0,0, WidthW1, HeightW2,'white', WindowHandle1)set_display_font(WindowHandle1,16,'mono','true','false')set_display_font(WindowHandle2,16,'mono','true','false')** Read image of the scene.
SceneIdx :=4* 读取图片
read_image(Image,'boxes/cardboard_boxes_02')** Display first message and the image of the scene.dev_set_window(WindowHandle1)dev_clear_window()
Text := 'This example demonstrates edge-supported surface-based matching. The goal is to find cardboard boxes in 3D scans of the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')dev_set_window(WindowHandle2)dev_display(Image)** Create a 3D model of a box.dev_set_window(WindowHandle1)*gen_box_object_model_3d([0,0,0,0,0,0,0],0.103,0.082,0.05, ObjectModel3DBox)* 平面三角化,加快匹配速度,获取包含三角剖分表面的3D对象模型的句柄ObjectModel3DModel
triangulate_object_model_3d(ObjectModel3DBox,'greedy',[],[], ObjectModel3DModel, Information)* 清除3D模型
clear_object_model_3d(ObjectModel3DBox)* Prepare a model for edge-supported surface-based matching.
FileName :='box_edge_supported.sfm'file_exists(FileName, FileExists)if(not FileExists)* 如果文件不存在 创建表面匹配模版
dev_disp_text('Creating new surface model.\nThis might take some minutes...', 'window', 150, 'left', 'black', 'box', 'false')******************************训练3D边缘信息
create_surface_model(ObjectModel3DModel,0.03,'train_3d_edges','true', SurfaceModel)* 将模版写入
write_surface_model(SurfaceModel, FileName)elsedev_disp_text('Reading pre-created surface model...', 'window', 150, 'left', 'black', 'box', 'false')* 读取表面模型
read_surface_model(FileName, SurfaceModel)
endif
** Create a 3D model of a box for surface-based matching without edge support.** 在没有边缘支持的情况下,为基于表面的匹配创建一个盒子的三维模型。
create_surface_model(ObjectModel3DModel,0.03,[],[], SurfaceModelWithoutEdgeSupport)** Read the 3D object model of the search scene and prepare it for display.* 读取搜索场景的 3D 物体模型并准备显示。
read_image(ImageXYZ,'boxes/cardboard_boxes_xyz_02')decompose3(ImageXYZ, XScene, YScene, ZScene)* 将点云转换为3D模型
xyz_to_object_model_3d(XScene, YScene, ZScene, ObjectModel3DScene)* 准备3D
prepare_object_model_3d(ObjectModel3DScene,'segmentation','true',[],[])* 获取3D模型参数
get_object_model_3d_params(ObjectModel3DScene,'center', VisRotationCenter)** Display search scene.dev_set_window(WindowHandle1)
Text := 'First, let\'s have a look at the 3D data of the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window', VisRowNextStep,'left','black','box','false')dev_disp_text('Press Run (F5) to continue','window','bottom','right','black',[],[])stop()dev_clear_window()*在三维数据中,这两个方框显示为平面部分,明显位于地平面之上
Text := 'In the 3D data, the two boxes appear as planar parts that lie clearly above the ground plane.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')visualize_object_model_3d(WindowHandle2, ObjectModel3DScene,[], VisPose,[],[],'3D scene',[], VisInstructions, VisPose)** Show results of surface-based matching without edge support.dev_set_window(WindowHandle1)dev_clear_window()******不使用用边缘信息*************************如果不使用边缘信息,有时会错误地找到平面物体。特别是如果搜索场景中的物体表面是破碎的,例如,由于表面反射的原因。
Text := 'Objects with a planar surface are sometimes found incorrectly if no edge information is used. Especially,if the surface of the object in the search scene is fragmented, e.g., because of reflections on the surface.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')* Perform surface-based matching without edge support.** 执行基于表面的匹配,无需边缘支持。
find_surface_model(SurfaceModelWithoutEdgeSupport, ObjectModel3DScene,0.05,0.1, MinScore,'false','num_matches',1, Pose, Score, SurfaceMatchingResult)* Display results.* 显示匹配结果
rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[ObjectModel3DScene,ObjectModel3DRigidTrans],[], VisPose, VisParamNames, VisParamValues, 'Without edge support, a wrong match is found.',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)*************************************************************************************使用边缘信息
* Show results of edge-supported surface-based matching.* 显示边缘支持的模版匹配结果
dev_set_window(WindowHandle1)dev_clear_window()*在这种情况下,使用 3D 边缘(绿色显示)有助于找到正确的匹配。请注意,'num_matched/'已设置为 1,因此只找到一个方框。
Text := 'In those cases, the use of 3D edges(displayed in green) helps to find the correct match. Note that \'num_matches\' has been set to 1, therefore only one box is found.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')*'要使用基于边缘支持的曲面匹配,请在创建曲面模型时将通用参数 ('train_3d_edges/')设置为 ('true/')。
Text := 'To use edge-supported surface-based matching, create the surface model with the generic parameter \'train_3d_edges\' set to \'true\'.'wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',150,'left','black','box','false')* Perform edge-supported surface-based matching.find_surface_model(SurfaceModel, ObjectModel3DScene,0.05,0.1, MinScore,'false','num_matches',1, Pose, Score, SurfaceMatchingResult)* Extract edges for visualization.* 提取边缘场景
get_object_model_3d_params(ObjectModel3DScene, 'diameter_axis_aligned_bounding_box', Diameter)edges_object_model_3d(ObjectModel3DScene,0.01* Diameter,[],[], ObjectModel3DEdges)* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)
VisParamValues[0]:='forest green'visualize_object_model_3d(WindowHandle2,[ObjectModel3DScene,ObjectModel3DEdges,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1'],[VisParamValues,'green'], 'With edge support, the match is found correctly.',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)** Show results of edge-supported surface-based matching when searching for multiple matches.* 显示匹配结果
dev_set_window(WindowHandle1)dev_clear_window()*如果我们搜索到多个匹配项,第二个方框也会被找到。
Text := 'If we search for more than one match, the second box is found as well.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')
Text := 'Note that additional matches may also be found in the corners of the 3D scene. The top plane of the box fits perfectly to the background plane of the scene and two of the top plane\'s edges match the 3D edges of the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',150,'left','black','box','false')* Perform edge-supported surface-based matching.** 执行边缘支持的曲面匹配。
find_surface_model(SurfaceModel, ObjectModel3DScene,0.05,0.1, MinScore,'false','num_matches',5, Pose, Score, SurfaceMatchingResult)* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[ObjectModel3DScene,ObjectModel3DEdges,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1','color_4'],[VisParamValues,'green','red'], 'The boundaries of the scene lead to additional matches.',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)** 如果三维对象模型具有映射信息,则演示如何移除场景边界上的边缘。
* Show how to remove edges at the boundary of the scene if the 3D object model has mapping information.dev_set_window(WindowHandle1)dev_set_window(WindowHandle1)dev_clear_window()*********************************** 我们可以消除源于场景边界的三维边缘(红色显示),以避免场景角落出现额外的匹配。
Text := 'We can eliminate the 3D edges stemming from the boundary of the scene(displayed in red) to avoid the additional matches in the corners of the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')* Eliminate 3D edges at the boundary of the scene.
Border :=50* 将点云图片转换为图像
object_model_3d_to_xyz(XEdges, YEdges, ZEdges, ObjectModel3DEdges,'from_xyz_map',[],[])get_image_size(XEdges, WidthMapping, HeightMapping)select_points_object_model_3d(ObjectModel3DEdges,['mapping_row','mapping_col'],[Border,Border],[HeightMapping - Border,WidthMapping - Border], ObjectModel3DEdgesWithoutBorder)* Display results.visualize_object_model_3d(WindowHandle2,[ObjectModel3DEdgesWithoutBorder,ObjectModel3DEdges],[], VisPose,['color_0','color_1'],['green','red'], 'Scene edges with removed edges at the boundary',[], VisInstructions, VisPose)** Show results of edge-supported surface-based matching when using manipulated 3D edges.dev_set_window(WindowHandle1)dev_clear_window()* 如果没有场景边界的边缘,在场景的角落里就找不到额外的匹配点。
Text := 'Without the edges at the boundary of the scene, no additional matches are found in the corners of the scene.'
wrap_text(Text, LineLength, TextWrapped)*您可以以任何方式修改 3D 边缘。要使用修改后的边缘,必须将它们作为 find_surface_model 运算符的通用参数 ('3d_edges/')的值。
dev_disp_text(TextWrapped,'window','top','left','black','box','false')
Text := 'You can modify the 3D edges in any way. To use modified edges, they must be given as value of the generic parameter \'3d_edges\' of the operator find_surface_model.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',150,'left','black','box','false')* Perform edge-supported surface-based matching.* 发现模版
find_surface_model(SurfaceModel, ObjectModel3DScene,0.05,0.1, MinScore,'false',['num_matches','3d_edges'],[5,ObjectModel3DEdgesWithoutBorder], Pose, Score, SurfaceMatchingResult)* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[ObjectModel3DScene,ObjectModel3DEdgesWithoutBorder,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1'],[VisParamValues,'green'], 'Matching result with cropped edges',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)** Show how to remove the background plane from the scene.dev_set_window(WindowHandle1)dev_clear_window()*避免背景平面匹配的更好方法是将背景平面从场景中完全移除。
Text := 'An even better way to avoid matches in the background plane is to completely remove the background plane from the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')
Text := 'This is sometimes not possible, but if it is, you should do it!'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',150,'left','black','box','false')* Remove the background plane from the scene.* 移除背景
select_points_object_model_3d(ObjectModel3DScene,'point_coord_z',0.1,0.73, SceneWithoutBackground)* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[SceneWithoutBackground,ObjectModel3DScene],[], VisPose,['color_0','color_1','alpha_1'],['white','red',0.5],'Scene and removed background',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)dev_set_window(WindowHandle2)dev_clear_window()** Show results of edge-supported surface-based matching when using a scene without the background plane.dev_set_window(WindowHandle1)dev_clear_window()*在没有场景背景平面的情况下,在场景的角落里找不到额外的匹配结果。此外,如果将背景从场景中移除,匹配速度会明显加快。
Text := 'Without the background plane of the scene, no additional matches are found in the corners of the scene. Furthermore, the matching is significantly faster if the background has been removed from the scene.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')dev_disp_text('Determining speedup...','window',200,'left','black','box','false')* Determine speedup.
Num :=5count_seconds(S1)for I :=1 to Num by 1find_surface_model(SurfaceModel, ObjectModel3DScene,0.05,0.1, MinScore,'false','num_matches',3, Pose, Score, SurfaceMatchingResult)
endfor
count_seconds(S2)
TimeWithBackground :=(S2 - S1)/real(Num)*1000.0count_seconds(S1)for I :=1 to Num by 1find_surface_model(SurfaceModel, SceneWithoutBackground,0.05,0.1, MinScore,'false','num_matches',3, Pose, Score, SurfaceMatchingResult)
endfor
count_seconds(S2)
TimeNoBackground :=(S2 - S1)/real(Num)*1000.0*
Text[0]:='Runtime:'
Text[1]:=' With background: '+ TimeWithBackground$'4.0f'+' ms'
Text[2]:=' Without background: '+ TimeNoBackground$'4.0f'+' ms'dev_disp_text(Text,'window',250,'left','black','box','false')* 显示匹配结果
* Display results.edges_object_model_3d(SceneWithoutBackground,0.01* Diameter,[],[], ObjectModel3DEdgesWithoutBackground)rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[SceneWithoutBackground,ObjectModel3DEdgesWithoutBackground,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1'],[VisParamValues,'green'], 'Matching result in scene with removed background',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)** Speedup edge-supported surface-based matching by reducing the number of points used for pose refinement.****************************************姿态细化加快匹配速度
dev_set_window(WindowHandle1)dev_clear_window()*如果搜索场景中的点与物体的大小相比比较密集,也就是说,如果姿态细化必须使用大量的点,就有可能进一步提高速度。
Text := 'Further speedup is possible,if the points in the search scene are dense compared to the size of the object, i.e.,if the pose refinement has to use a large number of points.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')*可以通过增加通用参数 \'pose_ref_sub_sampling(')来减少用于姿态细化的点数。'
Text := 'The number of points used for the pose refinement can be reduced by increasing the generic parameter \'pose_ref_sub_sampling\'. 'wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',150,'left','black','box','false')dev_disp_text('Determining speedup...','window',270,'left','black','box','false')* Determine speedup.
Num :=5count_seconds(S1)for I :=1 to Num by 1find_surface_model(SurfaceModel, ObjectModel3DScene,0.05,0.1, MinScore,'false',['num_matches','pose_ref_sub_sampling'],[3,20], Pose, Score, SurfaceMatchingResult)
endfor
count_seconds(S2)
TimeWithBackgroundPoseRefSubSampling :=(S2 - S1)/real(Num)*1000.0count_seconds(S1)for I :=1 to Num by 1find_surface_model(SurfaceModel, SceneWithoutBackground,0.05,0.1, MinScore,'false',['num_matches','pose_ref_sub_sampling'],[3,20], Pose, Score, SurfaceMatchingResult)
endfor
count_seconds(S2)
TimeNoBackgroundPoseRefSubSampling :=(S2 - S1)/real(Num)*1000.0*
Text[0]:='Runtime:'
Text[1]:= ' pose_ref_sub_sampling =2(default):'
Text[2]:=' With background: '+ TimeWithBackground$'4.0f'+' ms'
Text[3]:=' Without background: '+ TimeNoBackground$'4.0f'+' ms'
Text[4]:=' pose_ref_sub_sampling = 20:'
Text[5]:=' With background: '+ TimeWithBackgroundPoseRefSubSampling$'4.0f'+' ms'
Text[6]:=' Without background: '+ TimeNoBackgroundPoseRefSubSampling$'4.0f'+' ms'dev_disp_text(Text,'window',300,'left','black','box','false')* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)visualize_object_model_3d(WindowHandle2,[SceneWithoutBackground,ObjectModel3DEdgesWithoutBackground,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1'],[VisParamValues,'green'], 'Matching result in scene with pose_ref_sub_sampling =20',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)** Show results of edge-supported surface-based matching with use of image edges.dev_set_window(WindowHandle1)dev_clear_window()*您甚至可以使用图像数据来改进匹配结果。如果场景的 3D 边缘模糊或扭曲,这一点尤其有用。图像边缘通常更清晰,从而提高了匹配的准确性。
Text := 'You can even use image data to improve the matching results. This is especially helpful if the 3D edges from the scene are blurry or distorted. The typically better defined image edges improve the matching accuracy.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window','top','left','black','box','false')
Text := 'To use image edges, use the operator find_surface_model_image.'
wrap_text(Text, LineLength, TextWrapped)dev_disp_text(TextWrapped,'window',200,'left','black','box','false')* Perform edge-supported surface-based matching.set_surface_model_param(SurfaceModel,'camera_parameter', CameraParam)set_surface_model_param(SurfaceModel,'camera_pose', CameraPose)find_surface_model_image(Image, SurfaceModel, SceneWithoutBackground,0.05,0.1, MinScore,'false',['num_matches','pose_ref_sub_sampling'],[5,20], Pose, Score, SurfaceMatchingResult)* Display results.rigid_trans_object_model_3d(ObjectModel3DModel, Pose, ObjectModel3DRigidTrans)* Get gray values from image.object_model_3d_to_xyz(X, Y, Z, SceneWithoutBackground,'from_xyz_map',[],[])get_domain(Z, Domain)get_region_points(Domain, Rows, Columns)get_grayval(Image, Rows, Columns, AttribValues)* Set the gray values as attributes of the 3D object model.set_object_model_3d_attrib_mod(SceneWithoutBackground,'&gray','points', AttribValues)visualize_object_model_3d(WindowHandle2,[SceneWithoutBackground,ObjectModel3DEdgesWithoutBackground,ObjectModel3DRigidTrans],[], VisPose,[VisParamNames,'color_1','color_attrib_0'],[VisParamValues,'green','&gray'], 'Matching result with 2D and 3D edge support',[], VisInstructions, VisPose)clear_object_model_3d(ObjectModel3DRigidTrans)* Display results on image.set_window_param(WindowHandle2,'flush','false')dev_set_lut('cubic_root')dev_set_line_width(3)dev_set_color('green')dev_display(Image)dev_disp_text('Matching results projected into the image', 'window', 'top', 'left', 'black',[],[])gen_empty_obj(AllContours)for Index :=0 to |Score|-1 by 1
CurrPose := Pose[7* Index:7* Index +6]project_object_model_3d(Contours, ObjectModel3DModel, CameraParam, CurrPose,[],[])concat_obj(AllContours, Contours, AllContours)
endfor
dev_display(AllContours)dev_set_line_width(1)flush_buffer(WindowHandle2)set_window_param(WindowHandle2,'flush','true')*dev_set_window(WindowHandle2)dev_disp_text(' End of program ','window','bottom','right','black',[],[])clear_object_model_3d([ObjectModel3DModel,ObjectModel3DScene,ObjectModel3DEdges,ObjectModel3DEdgesWithoutBorder,SceneWithoutBackground,ObjectModel3DEdgesWithoutBackground])clear_surface_model([SurfaceModel,SurfaceModelWithoutEdgeSupport])stop()*dev_set_lut('default')