* 这个示例程序展示了如何使用HALCON的3D基于形状的匹配
* 来找到瓷砖间隔片的3D姿态* 由于背景纹理较强,在create_shape_model_3d()中使用'ignore_part_polarity'度量
* 以加快搜索速度。为了展示速度提升,创建了两个3D形状模型:
* 一个使用'ignore_local_polarity'度量,另一个使用'ignore_part_polarity'度量* 然后,为每个找到的对象显示轮廓并比较搜索时间* 本示例还使用了'lowest_model_level'选项,这在示例程序
*3d_matching_lowest_model_level.hdev中有更详细的解释* 关闭显示更新
dev_update_off()* 获取并设置异常处理偏好(抑制已处理异常的对话框)dev_get_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)dev_set_preferences('suppress_handled_exceptions_dlg','true')* 设置相机参数(可以通过相机标定获得)gen_cam_par_area_scan_division(0.0269462,-354.842,1.27964e-005,1.28e-005,254.24,201.977,512,384, CamParam)* 从相机参数中获取图像宽度和高度
get_cam_par_data(CamParam,'image_width', IWidth)get_cam_par_data(CamParam,'image_height', IHeight)* 关闭并重新打开显示窗口
dev_close_window()dev_open_window(0,0,512*1.5,384*1.5,'black', WindowHandle)* 设置显示字体
set_display_font(WindowHandle,14,'mono','true','false')* 设置线宽为2dev_set_line_width(2)* 定义颜色数组
Colors :=['blue','forest green','black']* 获取用于搜索的两个形状模型
acquire_shape_models(WindowHandle, CamParam, ShapeModel3DID1, ShapeModel3DID2)* 显示继续消息并暂停
disp_continue_message(WindowHandle,'black','true')stop()* 设置匹配参数
MatchingParameters :=['num_matches','pose_refinement']
MatchingParameterValues :=[2,'least_squares_high']
MinScore :=0.65
Greediness :=0.9
NumLevels :=0* 匹配循环
NumImages :=9for I :=1 to NumImages by 1* 读取图像read_image(Image,'tile_spacers/tile_spacers_'+ I$'02')* 显示图像(使用蓝色)dev_set_color(Colors[0])dev_display(Image)* 使用'ignore_local_polarity'度量的模型1查找最多两个实例Message :='Search model with \'ignore_local_polarity\' ...'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 记录开始时间count_seconds(Seconds1)* 执行3D形状匹配find_shape_model_3d(Image, ShapeModel3DID1, MinScore, Greediness, NumLevels, MatchingParameters, MatchingParameterValues, Pose, CovPose, Score1)* 记录结束时间count_seconds(Seconds2)Time1 := Seconds2 - Seconds1* 通过投影3D形状模型来可视化找到的匹配for J :=0 to |Score1|-1 by 1PoseTmp := Pose[J *7:J *7+6]project_shape_model_3d(ModelContours, ShapeModel3DID1, CamParam, PoseTmp,'true',rad(30))dev_display(ModelContours)endfor* 显示匹配结果信息Message :='Search model with \'ignore_local_polarity\': '+|Score1|+' Match(es) found in '+ Time1$'.1f'+' s'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 使用'ignore_part_polarity'度量的模型2查找最多两个实例dev_set_color(Colors[1])Message[1]:='Search model with \'ignore_part_polarity\' ...'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 记录开始时间count_seconds(Seconds1)* 执行3D形状匹配find_shape_model_3d(Image, ShapeModel3DID2, MinScore, Greediness, NumLevels, MatchingParameters, MatchingParameterValues, Pose, CovPose, Score2)* 记录结束时间count_seconds(Seconds2)Time2 := Seconds2 - Seconds1* 通过投影3D形状模型来可视化找到的匹配(使用虚线样式)set_line_style(WindowHandle,[10,10])for J :=0 to |Score2|-1 by 1PoseTmp := Pose[J *7:J *7+6]project_shape_model_3d(ModelContours, ShapeModel3DID1, CamParam, PoseTmp,'true',rad(30))dev_display(ModelContours)endforset_line_style(WindowHandle,[])* 显示匹配结果信息和速度提升因子Message[1]:='Search model with \'ignore_part_polarity\': '+|Score2|+' Match(es) found in '+ Time2$'.1f'+' s'Message[2]:='Speed-up factor with \'ignore_part_polarity\': '+(Time1 / Time2)$'.1f'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 如果不是最后一张图像,显示继续消息并暂停if(I < NumImages)disp_continue_message(WindowHandle,'black','true')stop()endif
endfor* 暂停程序
stop()* 清除3D形状模型
clear_shape_model_3d(ShapeModel3DID1)clear_shape_model_3d(ShapeModel3DID2)* 恢复异常处理偏好设置
dev_set_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)* 这个示例程序展示了如何使用HALCON的3D基于形状的匹配
* 来找到瓷砖间隔片的3D姿态* 由于背景纹理较强,在create_shape_model_3d()中使用'ignore_part_polarity'度量
* 以加快搜索速度。为了展示速度提升,创建了两个3D形状模型:
* 一个使用'ignore_local_polarity'度量,另一个使用'ignore_part_polarity'度量* 然后,为每个找到的对象显示轮廓并比较搜索时间* 本示例还使用了'lowest_model_level'选项,这在示例程序
*3d_matching_lowest_model_level.hdev中有更详细的解释* 关闭显示更新
dev_update_off()* 获取并设置异常处理偏好(抑制已处理异常的对话框)dev_get_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)dev_set_preferences('suppress_handled_exceptions_dlg','true')* 设置相机参数(可以通过相机标定获得)gen_cam_par_area_scan_division(0.0269462,-354.842,1.27964e-005,1.28e-005,254.24,201.977,512,384, CamParam)* 从相机参数中获取图像宽度和高度
get_cam_par_data(CamParam,'image_width', IWidth)get_cam_par_data(CamParam,'image_height', IHeight)* 关闭并重新打开显示窗口
dev_close_window()dev_open_window(0,0,512*1.5,384*1.5,'black', WindowHandle)* 设置显示字体
set_display_font(WindowHandle,14,'mono','true','false')* 设置线宽为2dev_set_line_width(2)* 定义颜色数组
Colors :=['blue','forest green','black']* 获取用于搜索的两个形状模型
acquire_shape_models(WindowHandle, CamParam, ShapeModel3DID1, ShapeModel3DID2)* 显示继续消息并暂停
disp_continue_message(WindowHandle,'black','true')stop()* 设置匹配参数
MatchingParameters :=['num_matches','pose_refinement']
MatchingParameterValues :=[2,'least_squares_high']
MinScore :=0.65
Greediness :=0.9
NumLevels :=0* 匹配循环
NumImages :=9for I :=1 to NumImages by 1* 读取图像read_image(Image,'tile_spacers/tile_spacers_'+ I$'02')* 显示图像(使用蓝色)dev_set_color(Colors[0])dev_display(Image)* 使用'ignore_local_polarity'度量的模型1查找最多两个实例Message :='Search model with \'ignore_local_polarity\' ...'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 记录开始时间count_seconds(Seconds1)* 执行3D形状匹配find_shape_model_3d(Image, ShapeModel3DID1, MinScore, Greediness, NumLevels, MatchingParameters, MatchingParameterValues, Pose, CovPose, Score1)* 记录结束时间count_seconds(Seconds2)Time1 := Seconds2 - Seconds1* 通过投影3D形状模型来可视化找到的匹配for J :=0 to |Score1|-1 by 1PoseTmp := Pose[J *7:J *7+6]project_shape_model_3d(ModelContours, ShapeModel3DID1, CamParam, PoseTmp,'true',rad(30))dev_display(ModelContours)endfor* 显示匹配结果信息Message :='Search model with \'ignore_local_polarity\': '+|Score1|+' Match(es) found in '+ Time1$'.1f'+' s'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 使用'ignore_part_polarity'度量的模型2查找最多两个实例dev_set_color(Colors[1])Message[1]:='Search model with \'ignore_part_polarity\' ...'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 记录开始时间count_seconds(Seconds1)* 执行3D形状匹配find_shape_model_3d(Image, ShapeModel3DID2, MinScore, Greediness, NumLevels, MatchingParameters, MatchingParameterValues, Pose, CovPose, Score2)* 记录结束时间count_seconds(Seconds2)Time2 := Seconds2 - Seconds1* 通过投影3D形状模型来可视化找到的匹配(使用虚线样式)set_line_style(WindowHandle,[10,10])for J :=0 to |Score2|-1 by 1PoseTmp := Pose[J *7:J *7+6]project_shape_model_3d(ModelContours, ShapeModel3DID1, CamParam, PoseTmp,'true',rad(30))dev_display(ModelContours)endforset_line_style(WindowHandle,[])* 显示匹配结果信息和速度提升因子Message[1]:='Search model with \'ignore_part_polarity\': '+|Score2|+' Match(es) found in '+ Time2$'.1f'+' s'Message[2]:='Speed-up factor with \'ignore_part_polarity\': '+(Time1 / Time2)$'.1f'disp_message(WindowHandle, Message,'window',12,12, Colors,'true')* 如果不是最后一张图像,显示继续消息并暂停if(I < NumImages)disp_continue_message(WindowHandle,'black','true')stop()endif
endfor* 暂停程序
stop()* 清除3D形状模型
clear_shape_model_3d(ShapeModel3DID1)clear_shape_model_3d(ShapeModel3DID2)* 恢复异常处理偏好设置
dev_set_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)
'lowest_model_level’参数可减少内存需求
* 本示例展示如何使用HALCON基于形状的3D匹配技术
* 来定位瓷砖间隔片的3D姿态。由于物体在图像中较大,
*3D模型需要许多不同视角的内部表示,导致模型内存消耗很高。
* 使用'lowest_model_level'参数可减少内存需求:
*- 默认级别为1,提高级别可减少内存(每级约减少3倍)*- 同时模型创建时间减少2倍/级
*- 代价是匹配鲁棒性可能降低,搜索时间略增(5-10%)** 本例设置'lowest_model_level'为3,结果是:
*- 模型内存需求减少6倍
*- 模型创建时间减少4倍* 关闭显示更新
dev_update_off()* 获取并设置异常处理偏好(抑制已处理异常的对话框)dev_get_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)dev_set_preferences('suppress_handled_exceptions_dlg','true')* 设置相机参数(可通过相机标定获得)gen_cam_par_area_scan_division(0.0269462,-354.842,1.27964e-005,1.28e-005,254.24,201.977,512,384, CamParam)* 获取图像尺寸
get_cam_par_data(CamParam,'image_width', IWidth)get_cam_par_data(CamParam,'image_height', IHeight)* 读取示例图像并初始化窗口
read_image(Image,'tile_spacers/tile_spacers_color_01')dev_close_window()dev_open_window(0,0,512*1.5,384*1.5,'white', WindowHandle)set_display_font(WindowHandle,16,'mono','true','false')dev_set_line_width(3)* 尝试从磁盘读取3D形状模型
dev_clear_window()disp_message(WindowHandle,'Reading the 3D shape model file from disk ...','window',12,12,'black','false')
tryread_shape_model_3d('tile_spacer.sm3', ShapeModel3DID)catch(Exception)* 如果读取失败,则创建新模型* 从DXF文件读取3D对象模型read_object_model_3d('tile_spacer.dxf',0.0001,[],[], ObjectModel3DID, DXFStatus)prepare_object_model_3d(ObjectModel3DID,'shape_based_matching_3d','true',[],[])disp_message(WindowHandle,'Reading the 3D shape model file from disk ... not found!','window',12,12,'red','false')disp_message(WindowHandle,'Creating the 3D shape model (may take a few seconds) ...','window',42,12,'black','false')* 记录模型创建时间count_seconds(S1)* 关键参数设置:使用'lowest_model_level'=3来优化内存和创建时间create_shape_model_3d(ObjectModel3DID, CamParam,0,0,0,'gba',-rad(60),rad(60),-rad(60),rad(60),0,rad(360),0.26,0.27,10,'lowest_model_level',3, ShapeModel3DID)count_seconds(S2)T := S2 - S1* 清理原始3D对象模型clear_object_model_3d(ObjectModel3DID)* 显示创建时间disp_message(WindowHandle,'Creation time: '+ T$'.3'+' s','window',72,12,'black','false')* 尝试保存模型trydisp_message(WindowHandle,'Writing model to disk ...','window',102,12,'black','false')write_shape_model_3d(ShapeModel3DID,'tile_spacer.sm3')catch(Exception)disp_message(WindowHandle,'Writing model to disk ... failed!','window',102,12,'red','false')disp_continue_message(WindowHandle,'black','true')stop()endtry
endtry* 显示模型级别信息
disp_lowest_model_level_info(WindowHandle)disp_continue_message(WindowHandle,'black','true')stop()* 匹配阶段
Times :=[]
NumImages :=12for I :=1 to NumImages by 1read_image(Image,'tile_spacers/tile_spacers_color_'+ I$'02')dev_display(Image)* 查找最多3个实例(设置'border_model'为'true'因为物体可能接触图像边界)count_seconds(Seconds1)find_shape_model_3d(Image, ShapeModel3DID,0.7,0.85,0,['num_matches','max_overlap','border_model'],[3,0.75,'true'], Pose, CovPose, Score)count_seconds(Seconds2)Time := Seconds2 - Seconds1Times :=[Times,Time]* 可视化匹配结果for J :=0 to |Score|-1 by 1* 显示轮廓PoseTmp := Pose[J *7:J *7+6]project_shape_model_3d(ModelContours, ShapeModel3DID, CamParam, PoseTmp,'true',rad(30))dev_set_color('yellow')dev_display(ModelContours)* 显示3D坐标系dev_set_colored(3)disp_3d_coord_system(WindowHandle, CamParam, PoseTmp,0.015)endfor* 显示姿态参数for K :=0 to |Score|-1 by 1PoseTmp := Pose[K *7:K *7+6]display_match_pose(ShapeModel3DID, PoseTmp, WindowHandle)endfor* 显示匹配结果信息disp_message(WindowHandle,|Score|+' Match(es) found in '+ Time$'4.2f'+' s','window',12,12,'dark green',['white','false'])if(I < NumImages)disp_continue_message(WindowHandle,'black',['white','false'])stop()endif
endfor* 清理资源
clear_shape_model_3d(ShapeModel3DID)dev_set_preferences('suppress_handled_exceptions_dlg', SuppressHandledExceptionsDlg)disp_end_of_program_message(WindowHandle,'black',['white','false'])