处理图像识别和3D模型匹配
CSDN 2024-08-19 16:31:03 阅读 52
以下代码是使用Halcon软件编写的,用于处理图像识别和3D模型匹配。Halcon是一个用于机器视觉和图像处理的高级软件包,广泛应用于工业自动化领域。
read_image (Image, ‘metal-parts/bracket_tilted_01.png’)
dev_update_off ()
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 480, WindowHandle)
set_display_font (WindowHandle, 18, ‘mono’, ‘true’, ‘false’)
dev_set_color (‘lime green’)
dev_set_line_width (1)
CamParam := [0.0131933,-1039.78,8.3e-006,8.3e-006,313.191,253.978,640,480]
ReferencePose := [0.0185843,-0.015084,0.372976,359.872,0.989562,359.381,0]
dev_display (Image)
dev_set_colored (3)
disp_3d_coord_system (WindowHandle, CamParam, ReferencePose, 0.05)
*
Read the dxf model of a metal part. Note that the scaling ofthe original dxf model is one mm for one pixel. The operatorcreate_planar_calib_deformable_model_xld assumes that thedxf model is provided in world coordinates, i.e., thatone pixel corresponds to one meter. Hence, the model mustbe scaled accordingly.
disp_message (WindowHandle, ‘Prepare and create the Model’, ‘window’, 12, 84, ‘lime green’, ‘false’)
get_system (‘example_dir’, HalconExamples)
read_contour_xld_dxf (Contours, HalconExamples + ‘/hdevelop/Applications/Object-Recognition-2D/metal-part-01.dxf’, [], [], DxfStatus)
dev_set_color (‘cyan’)
dev_display (Contours)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, 0.001, 0.001, 0.5, 0.5, HomMat2DScale)
affine_trans_contour_xld (Contours, ContoursTrans, HomMat2DScale)
pose_to_hom_mat3d (ReferencePose, HomMat3D)
gen_empty_obj (FoundContour)
count_obj (ContoursTrans, NumberContour)
for I := 1 to NumberContour by 1
select_obj (ContoursTrans, ObjectSelected, I)
get_contour_xld (ObjectSelected, Y, X)
Z := gen_tuple_const(|X|,0.0)
Transform the metric model into world coordinates.
affine_trans_point_3d (HomMat3D, X, Y, Z, Xc, Yc, Zc)Project the 3D points of the model, which are availableas world coordinates, into the image coordinate system.
project_3d_point (Xc, Yc, Zc, CamParam, R, C)
gen_contour_polygon_xld (ModelWorld, R, C)
concat_obj (FoundContour, ModelWorld, FoundContour)
endfor
dev_set_color (‘cyan’)
dev_display (FoundContour) Create the planar calibrated deformable model from the XLDcontour. For that, the model contours in world coordinates,the camera parameters, and the reference pose of the objectare needed. The reference pose can, e.g., be estimated usinga calibration plate.
create_planar_calib_deformable_model_xld (ContoursTrans, CamParam, ReferencePose, ‘auto’, -0.39, 0.79, ‘auto’, 0.9, 1.1, ‘auto’, 0.9, 1.1, ‘auto’, ‘none’, ‘ignore_local_polarity’, 5, [], [], ModelID)Query the model contours from the model. Note that in contrastto the uncalibrated case, the model contours are returned inworld coordinates.
get_deformable_model_contours (ModelContours, ModelID, 1)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()Try to find the metal parts in the search images.
for I := 1 to 9 by 1
read_image (Image, ‘metal-parts/bracket_tilted_’ + IKaTeX parse error: Undefined control sequence: \n at position 1303: … 'Object found \̲n̲ ̲X: ' + PoseSele…‘.4f’ + ’ m\n Y: ’ + PoseSelected[1]KaTeX parse error: Undefined control sequence: \n at position 12: '.4f' + ' m\̲n̲ ̲Z: ' + PoseSele…‘.4f’ + ’ m’, ‘window’, 48, 12, ‘black’, ‘true’)
dev_set_color (‘cyan’)
dev_display (FoundContour)
dev_set_colored (3)
disp_3d_coord_system (WindowHandle, CamParam, PoseSelected, 0.05)
endfor
disp_message (WindowHandle, |Score| + ’ Matches found in ’ + ((Seconds2 - Seconds1) * 1000.0)$‘.1f’ + ’ ms’, ‘window’, -1, -1, ‘lime green’, ‘false’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endforExtract the polarity information of the model using theimage and the respective pose of the last match.
dev_display (Image)
dev_set_color (‘cyan’)
dev_display (FoundContour)
disp_message (WindowHandle, ‘Use polarity of an image to update the model’, ‘window’, -1, -1, ‘lime green’, ‘false’)
set_planar_calib_deformable_model_metric (Image, ModelID, PoseSelected, ‘use_polarity’)Note that the model contours are not affected by the changes,i.e., ModelContoursMetric corresponds to ModelContours
get_deformable_model_contours (ModelContoursMetric, ModelID, 1)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()Locate the metal parts in the search images using the adaptedpolarity setting.
for I := 1 to 9 by 1
read_image (Image, ‘metal-parts/bracket_tilted_’ + IKaTeX parse error: Undefined control sequence: \n at position 1315: … 'Object found \̲n̲ ̲X: ' + PoseSele…‘.4f’ + ’ m\n Y: ’ + PoseSelected[1]KaTeX parse error: Undefined control sequence: \n at position 12: '.4f' + ' m\̲n̲ ̲Z: ' + PoseSele…‘.4f’ + ’ m’, ‘window’, 48, 12, ‘black’, ‘true’)
dev_set_color (‘cyan’)
dev_display (FoundContour)
dev_set_colored (3)
disp_3d_coord_system (WindowHandle, CamParam, PoseSelected, 0.05)
endfor
disp_message (WindowHandle, |Score| + ’ Matches found in ’ + ((Seconds2 - Seconds1) * 1000.0)$‘.1f’ + ’ ms’, ‘window’, -1, -1, ‘lime green’, ‘false’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endforThe origin (reference point) of the model can be changed inthe world coordinate system, although this is in most casesnot recommended, because it might lead to a less accuratematching result.
dev_clear_window ()
dev_set_color (‘cyan’)
disp_message (WindowHandle, ‘Change the origin of the model’, ‘window’, -1, -1, ‘lime green’, ‘false’)
set_deformable_model_origin (ModelID, 0.010, 0.0425)The model contours are affected by the new reference point
get_deformable_model_contours (ModelContoursNewOrigin, ModelID, 1)
dev_display (ModelContoursNewOrigin)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()Locate the metal parts in the search images using the changedreference point.
for I := 1 to 9 by 1
read_image (Image, ‘metal-parts/bracket_tilted_’ + IKaTeX parse error: Undefined control sequence: \n at position 1321: … 'Object found \̲n̲ ̲X: ' + PoseSele…‘.4f’ + ’ m\n Y: ’ + PoseSelected[1]KaTeX parse error: Undefined control sequence: \n at position 12: '.4f' + ' m\̲n̲ ̲Z: ' + PoseSele…‘.4f’ + ’ m’, ‘window’, 48, 12, ‘black’, ‘true’)
dev_set_color (‘cyan’)
dev_display (FoundContour)
dev_set_colored (3)
disp_3d_coord_system (WindowHandle, CamParam, PoseSelected, 0.05)
endfor
disp_message (WindowHandle, |Score| + ’ Matches found in ’ + ((Seconds2 - Seconds1) * 1000.0)$‘.1f’ + ’ ms’, ‘window’, -1, -1, ‘lime green’, ‘false’)
if (I < 9)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
endif
endfor
clear_deformable_model (ModelID)
以下是程序运行结果:
以下是代码的主要功能和步骤的概述:
图像读取和窗口设置:代码首先读取一个图像文件,并设置显示窗口的大小和字体。
相机参数和参考姿态:定义了相机参数(CamParam)和参考姿态(ReferencePose),这些参数用于后续的3D模型投影和匹配。
模型读取和变换:读取DXF格式的金属零件模型,并根据相机参数将其从毫米级像素单位转换为米级世界坐标。
创建可变形模型:使用create_planar_calib_deformable_model_xld函数创建一个平面校准的可变形模型,这个模型可以适应图像中的不同变形。
模型轮廓提取:从创建的模型中提取轮廓,并将其转换为世界坐标。
图像搜索和模型匹配:循环读取一系列图像,使用find_planar_calib_deformable_model函数在这些图像中搜索并匹配模型。
结果展示:对于每个匹配结果,显示模型在图像中的位置,并展示匹配得分和所用时间。
模型极性更新:使用图像和最后一个匹配的姿态来更新模型的极性信息,这有助于提高模型匹配的准确性。
改变模型原点:改变模型的参考点,这可能会影响模型匹配的准确性。
再次搜索和匹配:在更新了模型极性或改变参考点后,再次进行图像搜索和模型匹配。
清理和结束:最后,清理创建的可变形模型并结束程序。
代码中使用了许多Halcon的函数,如read_image、dev_display、create_planar_calib_deformable_model_xld、find_planar_calib_deformable_model等,这些函数是实现图像处理和机器视觉任务的关键。代码的结构清晰,按照逻辑顺序执行任务,适用于自动化生产线上对特定零件的识别和定位。
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。