Main Content

Acquire Depth Image from Basler ToF blaze-101 Camera and View as Point Cloud

This example shows how to acquire a range map from a GenICam™ compliant Basler ToF blaze-101 camera and visualize it as a point cloud.

Requirements

This example requires the following add-ons:

  • Image Acquisition Toolbox™

  • Image Acquisition Toolbox Support Package for GenICam™ Interface

Connect to Camera

Create a videoinput object using the gentl adaptor. The example selects the first device and Coord3D_C16 pixel format to acquire 2D depth images from Basler ToF blaze-101 camera. You can view all the available device using imaqhwinfo.

vid = videoinput("gentl", 1, "Coord3D_C16");

Configure the camera for manual triggering. You will use the snapshot in a loop technique described in the example Acquire Single Image in Loop Using getsnapshot.

triggerconfig(vid, "manual");

Configure Camera

Retrieve the videosource object.

src = getselectedsource(vid);

Configure the camera to stream using the Range component to access range maps and disable all other components.

src.ComponentSelector = "Intensity";
src.ComponentEnable = "False";
src.ComponentSelector = "Confidence";
src.ComponentEnable = "False";
src.ComponentSelector = "Range";
src.ComponentEnable = "True";

Configure the minimum depth of the camera.

src.DepthMin = 0;

Set Scan3dCoordinateSelector property to "CoordinateC", which represents Z axis, and retrieve the depth-to-distance conversion factor by reading the value of Scan3dCoordinateScale property. This is used while converting the range map into a point cloud.

src.Scan3dCoordinateSelector = "CoordinateC";
% Depth-to-distance(mm) conversion factor
gray2mm = src.Scan3dCoordinateScale;

Start Acquisition

Preallocate the height and width of the range map.

videoResolution = vid.VideoResolution;
rangeMap = zeros(videoResolution(2), videoResolution(1), 0);

Start the camera streaming.

start(vid)

Create a Scatter Plot to View the Range Map as a Point Cloud

To create a point cloud, you need the X, Y, and Z coordinates. You get the X and Y coordinates using the find function. The Z coordinate is calculated using the following formula:

ZCoordinate=PixelValue*DepthConversionFactor

Capture a single image.

img = getsnapshot(vid);
rangeMap(:, :, end+1) = img;

Get X and Y coordinates from the image.

[y, x] = find(img);

Store the depth (Z coordinate) values from the pixels.

z = zeros(numel(x), 1);
for i = 1:numel(x)
    % Acquire the pixel data from the image
    pixelValue = img(y(i), x(i));
    % Calculate z-coordinate value using the above formula
    z(i) = pixelValue * gray2mm;
end

Create a scatter3 object.

fig = figure(Name="Point Cloud Preview");
hscatter = scatter3(x, y, z, 1, z, "filled");
% Add colormap and colorbar
colormap(gca, "parula"); 
colorbar;
% Turn the axis rotation on.
rotate3d on;

Stream Point Cloud and Acquire Range Map

Stream and save range data until the scatter3 plot is closed.

while ishandle(hscatter)
    img = getsnapshot(vid);
    rangeMap(:, :, end+1) = img;
    [y, x] = find(img);
    z = zeros(numel(x), 1);
    for i = 1:numel(x)
        % Acquire the pixel data from the image
        pixelValue = img(y(i), x(i));
        % Calculate z-coordinate value using the above formula
        z(i) = pixelValue * gray2mm;
    end
    if ishandle(hscatter)
        % Update scatter3 plot
        set(hscatter, XData=x, YData=y, ZData=z, CData=z);
    end
end
clear hscatter;
clear fig;

Scatter_final-sized.gif

View Point Cloud While Acquiring Range Map

With Computer Vision Toolbox™, you can also convert the data into a pointCloud (Computer Vision Toolbox) object and use pcplayer (Computer Vision Toolbox) to visualize point cloud data.

Create a pcplayer object.

player = pcplayer([min(x) max(x)], [min(y) max(y)], [min(z) max(z)]);

Stream point cloud data until pcplayer is closed.

ptCloud = [];
while isOpen(player) 
    img = getsnapshot(vid);
    rangeMap(:, :, end+1) = img;
    [y, x] = find(img);
    z = zeros(numel(x), 1);
    for i = 1:numel(x)
        % Acquire the pixel data from the image
        pixelValue = img(y(i), x(i));
        % Calculate z-coordinate value using the above formula
        z(i) = pixelValue * gray2mm;
    end
    ptCloud = [ptCloud pointCloud([x,y,z], "Intensity", z)];
    view(player, ptCloud(end))
end

pointCloudPlayer-sized.gif

Clean Up

stop(vid);
delete(vid);

See Also

Topics