这段代码是一个HALCON脚本,用于创建和使用一个可变形的二维模型来检测图像中的特定物体。
dev_close_window ()
read_image (ImageInit, ‘automotive/engine_parts_01’)
get_image_size (ImageInit, Width, Height)
dev_open_window_fit_image (ImageInit, 0, 0, Width, Height, WindowHandle)
dev_update_off ()
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_set_line_width (1)
dev_set_color (‘green’)
*
*
- Create the ROI of the model from the first image.
- Here, the upper part of the engine part, consisting of 6 boreholes can be used. dev_clear_window () read_image (Image, ‘automotive/engine_parts_01’) dev_display (Image) gen_rectangle2 (Rectangle1, 285.69, 284.554, -1.66101, 58.923, 17.3983) gen_rectangle2 (Rectangle2, 282.154, 373.738, -1.50165, 56.3435, 14.8286) union2 (Rectangle1, Rectangle2, RegionUnion) reduce_domain (Image, RegionUnion, ImageReduced) threshold (ImageReduced, Region, 110, 255) connection (Region, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 500, 3000) union1 (SelectedRegions, RegionUnion) dilation_circle (RegionUnion, RegionDilation, 3.5) reduce_domain (ImageReduced, RegionDilation, ImageReduced) disp_message (WindowHandle, ‘ROI created’, ‘window’, 10, 10, ‘black’, ‘true’) disp_continue_message (WindowHandle, ‘black’, ‘true’)
- Create the metric deformable model from the reduced image.
- Here the plane of the object is extracted from measured well defined points.
- Note that it is also possible to place a calibration plate on the object and correct for
- the thickness of the calibration plate.
- Consult example locate_car_door.hdev in the same directory. dev_display (Image)
- A camera calibration has been done beforehand (e.g. with the calibration assistant). CamParam := [0.0124068,-268.149,9.30326e-006,9.3e-006,290.95,268.404,640,512]
- The boreholes are defining a 3D plane. ObjectRow := [254.344,253.433,311.613,316.545] ObjectCol := [284.269,368.903,373.356,286.666] gen_cross_contour_xld (Cross, ObjectRow, ObjectCol, 6, 0.785398) WorldX := [-29.0,26,27,-30.0] / 1000.0 WorldY := [-21.5,-19,19,19] / 1000.0
- The Z coordinate of the points is set to 0.0 as it is a planar object. WorldZ := [0.0,0.0,0.0,0.0]
- Given the 3D-2D correspondences the plane of the object is extracted with vector_to_pose. vector_to_pose (WorldX, WorldY, WorldZ, ObjectRow, ObjectCol, CamParam, ‘planar_analytic’, ‘error’, PoseModelPlane, Quality) disp_3d_coord_system (WindowHandle, CamParam, PoseModelPlane, 0.05) create_planar_calib_deformable_model (ImageReduced, CamParam, PoseModelPlane, 3, 0, rad(360), ‘auto’, 1, 1, ‘auto’, 1, 1, ‘auto’, ‘none’, ‘use_polarity’, ‘auto’, ‘auto’, [], [], ModelID)
- Project contour model into world coordinate system for later visualization. get_deformable_model_contours (ModelContours, ModelID, 1) get_deformable_model_params (ModelID, ‘model_row’, ModelRow) get_deformable_model_params (ModelID, ‘model_col’, ModelCol) get_deformable_model_params (ModelID, ‘model_pose’, ModelPose) get_deformable_model_params (ModelID, ‘cam_param_rect’, CamParamRect)
- Due to radial distortion a simple translation does not suffice.
- Instead we have to project the metric model into the world coordinate and
- project this back into the camera that contains the radial distortion. affine_trans_contour_xld (ModelContours, ContoursAffinTrans, [1,0,ModelRow,0,1,ModelCol]) contour_to_world_plane_xld (ContoursAffinTrans, ContoursTrans, CamParamRect, ModelPose, ‘m’) count_obj (ContoursTrans, NumberContour) pose_to_hom_mat3d (ModelPose, HomMat3D)
- Project model into 3D world coordinates and project it into the camera. gen_empty_obj (FoundContour) for Index2 := 1 to NumberContour by 1 select_obj (ContoursTrans, ObjectSelected, Index2) get_contour_xld (ObjectSelected, Y, X) Z := gen_tuple_const(|X|,0.0) affine_trans_point_3d (HomMat3D, X, Y, Z, Xc, Yc, Zc) project_3d_point (Xc, Yc, Zc, CamParam, R, C) gen_contour_polygon_xld (ModelWorld, R, C) concat_obj (FoundContour, ModelWorld, FoundContour) endfor
disp_message (WindowHandle, ‘Deformable model created’, ‘window’, 10, 10, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
*
- Because some models are close to the border of the image, border_shape_models
- must be set to true. get_system (‘border_shape_models’, BorderShapeModels) set_system (‘border_shape_models’, ‘true’)
- Detect the model in new images with potentially tilted object instances. for Index := 1 to 25 by 1 TotalTime := 0 read_image (Image, ‘automotive/engine_parts_’ + Index$‘.02’) * - NumMatches = 0 returns not a specified number of found- instances but all found instances. Use NumMatches = 0- if you don’t know how many instances are to be found. count_seconds (Seconds1) find_planar_calib_deformable_model (Image, ModelID, rad(0), rad(360), 1, 1, 1, 1, 0.65, 0, 0, 3, 0.75, [], [], Pose, CovPose, Score) count_seconds (Seconds2) dev_display (Image) Time := Seconds2 - Seconds1 TotalTime := TotalTime + Time for Index1 := 0 to |Score| - 1 by 1 tuple_select_range (Pose, Index1 * 7, ((Index1 + 1) * 7) - 1, PoseSelected) disp_3d_coord_system (WindowHandle, CamParam, PoseSelected, 0.04) pose_to_hom_mat3d (PoseSelected, HomMat3D) gen_empty_obj (FoundContour) for Index2 := 1 to NumberContour by 1 select_obj (ContoursTrans, ObjectSelected, Index2) get_contour_xld (ObjectSelected, Y, X) Z := gen_tuple_const(|X|,0.0) affine_trans_point_3d (HomMat3D, X, Y, Z, Xc, Yc, Zc) project_3d_point (Xc, Yc, Zc, CamParam, R, C) gen_contour_polygon_xld (ModelWorld, R, C) concat_obj (FoundContour, ModelWorld, FoundContour) endfor dev_display (FoundContour) disp_message (WindowHandle, |Score| + ’ models found in ’ + (TotalTime * 1000)$‘.4’ + ’ ms’, ‘window’, 10, 10, ‘black’, ‘true’) endfor disp_continue_message (WindowHandle, ‘black’, ‘true’) stop () endfor dev_display (Image) disp_message (WindowHandle, ‘Program finished\nPress ‘Run’ to clear deformable model’, ‘window’, 10, 10, ‘black’, ‘true’) stop ()
- Set border_shape_models to original value. set_system (‘border_shape_models’, BorderShapeModels)
- Free memory of the model. clear_deformable_model (ModelID)
以下是程序运行结果:
下面是对这段代码的详细解释:
初始化和图像读取:
dev_close_window() 关闭当前窗口。
read_image(ImageInit, ‘automotive/engine_parts_1’) 读取初始图像。
get_image_size(ImageInit, Width, Height) 获取图像尺寸。
dev_open_window_fit_image(…) 打开一个窗口以适应图像尺寸。
ROI(感兴趣区域)的创建:
使用gen_rectangle2定义两个矩形区域。
union2将两个矩形合并为一个区域。
reduce_domain将图像限制在合并后的区域。
threshold和connection用于二值化图像并连接区域。
select_shape选择特定面积的区域。
union1再次合并区域。
dilation_circle对区域进行膨胀操作。
创建可变形模型:
使用gen_cross_contour_xld生成交叉轮廓。
定义世界坐标系中的点WorldX, WorldY, WorldZ。
使用vector_to_pose提取对象的平面姿态。
create_planar_calib_deformable_model创建可变形模型。
模型投影和变换:
将模型轮廓投影到世界坐标系。
使用affine_trans_contour_xld和contour_to_world_plane_xld进行坐标变换。
模型检测:
循环读取一系列图像。
使用find_planar_calib_deformable_model在图像中检测模型。
显示检测结果和计时信息。
模型显示和清理:
disp_message显示信息。
set_system(‘border_shape_models’, ‘true’)设置系统参数以允许模型边界超出图像。
清理模型并关闭窗口。
脚本结束:
stop()结束脚本执行。
set_system恢复系统参数。
clear_deformable_model清理模型占用的内存。
这个脚本展示了如何使用HALCON软件进行图像处理和模式识别,特别是在工业自动化和质量控制领域中检测特定物体的应用。通过创建可变形模型,可以适应物体的不同姿态和形状变化。
版权归原作者 Happy Monkey 所有, 如有侵权,请联系我们删除。