Obtaining Point Cloud Data from Depth Map in Custom Development with GenICam

When developing with the GenICam protocol, there may be questions about obtaining point cloud data from a depth map. For specific guidance, refer to the “obtain_depth_map” example in Halcon.

File Location

  • After installing the latest version of the Mech-Eye SDK, open the path “Mech-Eye SDK\API\samples\halcon” to find the “obtain_depth_map” example.
  • If you have Halcon software installed, you can open it directly. If Halcon is not installed, you can use the system’s built-in Notepad to open it.

Introduction

The code in the following sample demonstrates the process of connecting to the camera and obtaining depth values. In the ‘Coord3D_C16’ section, ‘C’ corresponds to the data in the Z direction of the point cloud. If you also need to obtain data in the XY direction of the point cloud, you should modify ‘Coord3D_C16’ to ‘Coord3D_ABC16’, and change ‘CoordinateC’ to ‘CoordinateA’ or ‘CoordinateB’. Correspondingly, modify ‘ScaleZ’, ‘OffsetZ’ to ‘ScaleX’, ‘OffsetX’, or ‘ScaleY’, ‘OffsetY’.

* Switch the "DeviceScanType" parameter to "Areascan3D" to obtain the 3D data.
set_framegrabber_param (AcqHandle, 'DeviceScanType', 'Areascan3D')
* Set the "ComponentSelector" parameter to "Range" to allow configuration of the "PixelFormat" parameter.
set_framegrabber_param (AcqHandle, 'ComponentSelector', 'Range')

* Switch "PixelFormat" parameter to "Coord3D_C16 pixel to get only depth map(2D image containing the Z values).
set_framegrabber_param (AcqHandle, 'PixelFormat', 'Coord3D_C16')

get_framegrabber_param (AcqHandle, 'Width', Width)
get_framegrabber_param (AcqHandle, 'Height', Height)

* Acquire the raw data from the camera and convert the data to a depth map.
grab_image(DepthRaw, AcqHandle)

set_framegrabber_param (AcqHandle, 'ChunkScan3dCoordinateSelector', 'CoordinateC')
get_framegrabber_param (AcqHandle, 'ChunkScan3dCoordinateScale', ScaleZ) 
get_framegrabber_param (AcqHandle, 'ChunkScan3dCoordinateOffset', OffsetZ)
convert_image_type(DepthRaw, DepthReal, 'real')
scale_image(DepthReal, DepthInMM, ScaleZ, OffsetZ)
* Convert the unit of depth values in the depth map from mm to m.
scale_image(DepthInMM, DepthInM, 0.001, 0)

Code Explanation

  1. set_framegrabber_param(AcqHandle, 'DeviceScanType', 'Areascan3D'): Sets the device’s scan type to Areascan3D to acquire three-dimensional data.
  2. set_framegrabber_param(AcqHandle, 'ComponentSelector', 'Range'): Sets the component selector to Range for configuring pixel format parameters.
  3. set_framegrabber_param(AcqHandle, 'PixelFormat', 'Coord3D_C16'): Configures the pixel format parameter to Coord3D_C16 to obtain a depth map containing only depth values (a two-dimensional image with Z values).
  4. get_framegrabber_param(AcqHandle, 'Width', Width): Retrieves the width of the image and stores it in the variable ‘Width’.
  5. get_framegrabber_param(AcqHandle, 'Height', Height): Retrieves the height of the image and stores it in the variable ‘Height’.
  6. grab_image(DepthRaw, AcqHandle): Retrieves raw data from the camera and stores it in the variable ‘DepthRaw’.
  7. set_framegrabber_param(AcqHandle, 'ChunkScan3dCoordinateSelector', 'CoordinateC'): Sets the coordinate selector to CoordinateC to obtain scaling and offset parameters for the Z-axis.
  8. get_framegrabber_param(AcqHandle, 'ChunkScan3dCoordinateScale', ScaleZ): Retrieves the scaling parameter for the Z-axis and stores it in the variable ‘ScaleZ’.
  9. get_framegrabber_param(AcqHandle, 'ChunkScan3dCoordinateOffset', OffsetZ): Retrieves the offset parameter for the Z-axis and stores it in the variable ‘OffsetZ’.
  10. convert_image_type(DepthRaw, DepthReal, 'real'): Converts the raw data ‘DepthRaw’ to the real number type and stores the result in the variable ‘DepthReal’.
  11. scale_image(DepthReal, DepthInMM, ScaleZ, OffsetZ): Scales the depth map ‘DepthReal’ of real number type using the scaling parameter ‘ScaleZ’ and offset parameter ‘OffsetZ’, resulting in a depth map ‘DepthInMM’ in millimeters.
  12. scale_image(DepthInMM, DepthInM, 0.001, 0): Converts the depth values in the depth map ‘DepthInMM’ from millimeters to meters and stores the result in the variable ‘DepthInM’.