HALCON的基础运用案例:- 例1- 3D点云的分割

前言:

在这个例子里面展示了用HALCON的操作函数segment_object_model_3d,来把一个输入的2.5D的3D图像进行分割。这里因为图像是一组圆柱体,有运用了一个物体的判别操作函数:dev_display_fitting_results。然后,自动给出了region的划分。


程序说明:

步骤1:读取2.5D的数据:

read_image (XYZ, '3d_machine_vision/segmentation/3d_primitives_xyz_01.tif')

数据就是几个圆柱体:

步骤2,转化为3D数据

access_channel (XYZ, X, 1)
access_channel (XYZ, Y, 2)
access_channel (XYZ, Z, 3)

【案】这时候,应该是拿了X,Y,Z三个坐标视图数据:【Franlin案,2.5D也许就是理解为通过三视图来转化得到的3D图像?感觉又不是,因为X,Y,Z的轮廓几乎是一样的角度】我们看给出的变量图如下:

X,Y,Z几乎为一样的角度,【案】也许是双目视图。

如果看X,Y的3D视图:都是一个平面

 当然Z是三维图形:

步骤3:准备分割:

xyz_to_object_model_3d (X, Y, Z, ObjectModel3DID)
prepare_object_model_3d (ObjectModel3DID, 'segmentation', 'false', 'max_area_holes', 100)

这里先通过xyz_to_object_model_3d【具体,参考我的其他博客说明】,把刚才原始的2.5D的图像,搞成了一个3D点云模型ObjectModel3DID:然后在prepare_object_model_3d【依据设定准备处理的内存等】里面设定好分割的参数准备和设定。

步骤4:开始分割:

ParSegmentation := ['max_orientation_diff','max_curvature_diff','output_xyz_mapping','min_area']
ValSegmentation := [0.13,0.11,'true',150]
ParFitting := ['primitive_type','fitting_algorithm']
ValFitting := ['all','least_squares_huber']
* Segmentation and fitting is done in one step,
* because the parameter 'fitting' is set to 'true' by default
segment_object_model_3d (ObjectModel3DID, [ParSegmentation,ParFitting], [ValSegmentation,ValFitting], ObjectModel3DOutID)

 输入,输出的模型数据比较如下:

 每一栏都是一个分割后的3D实体:

然后,Primitive Tpye应该为基本的3D模型,【大概,因为不同的角度,有不同的识别类型】

步骤5,显示分割的模型:

步骤6,运用fitting:


案例源码:

* ***********************************************************************
* This example program shows how to use the operator
* segment_object_model_3d in HALCON. First, the 2.5D
* input image is segmented. Additionally, with the same
* operator, a fitting is performed. The result of the
* 3D segmentation is converted to a region and is
* displayed. Finally, the values of the fitted radii
* for the cylinders and spheres are visualized.
* ***********************************************************************
dev_update_off ()
dev_close_window ()
* Input: 2.5D image
read_image (XYZ, '3d_machine_vision/segmentation/3d_primitives_xyz_01.tif')

dev_open_window_fit_image (XYZ, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* Access to (x-, y-, z-)coordinates
access_channel (XYZ, X, 1)
access_channel (XYZ, Y, 2)
access_channel (XYZ, Z, 3)
* 
Message := 'Generate a 3D object model from an'
Message[1] := 'XYZ image and segment primitives'
Message[2] := '(spheres, cylinders, planes) in it:'
dev_display (Z)
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Prepare the segmentation
xyz_to_object_model_3d (X, Y, Z, ObjectModel3DID)
prepare_object_model_3d (ObjectModel3DID, 'segmentation', 'false', 'max_area_holes', 100)
ParSegmentation := ['max_orientation_diff','max_curvature_diff','output_xyz_mapping','min_area']
ValSegmentation := [0.13,0.11,'true',150]
ParFitting := ['primitive_type','fitting_algorithm']
ValFitting := ['all','least_squares_huber']
* Segmentation and fitting is done in one step,
* because the parameter 'fitting' is set to 'true' by default
segment_object_model_3d (ObjectModel3DID, [ParSegmentation,ParFitting], [ValSegmentation,ValFitting], ObjectModel3DOutID)
* Show the result of the segmentation
dev_set_colored (12)
for Index := 0 to |ObjectModel3DOutID| - 1 by 1
    object_model_3d_to_xyz (XTmp, YTmp, ZTmp, ObjectModel3DOutID[Index], 'from_xyz_map', [], [])
    get_domain (ZTmp, DomainTmp)
    if (Index == 0)
        copy_obj (DomainTmp, Domain, 1, 1)
    else
        concat_obj (Domain, DomainTmp, Domain)
    endif
endfor
dev_display (Domain)
disp_message (WindowHandle, '3D Segmentation', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Segmented objects: ' + |ObjectModel3DOutID|, 'window', 40, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* Show the result of the fitting
dev_clear_window ()
dev_display_fitting_results (RegionCylinder, RegionSphere, RegionPlane, RegionNone, ObjectModel3DOutID, WindowHandle, [])
* 
* Example code, if further inspections should be made:
* 
* Store only the data of the primitive to save memory
for Index := 0 to |ObjectModel3DOutID| - 1 by 1
    * Copy only the data of the primitive
    copy_object_model_3d (ObjectModel3DOutID[Index], 'primitives_all', CopiedObjectModel3DID)
    * Further inspections
    * .....
    * .....
endfor
dev_update_on ()

猜你喜欢

转载自blog.csdn.net/yellow_hill/article/details/133672667