Main Content

Create Custom Preprocessing Workflow with Lidar Viewer

The Lidar Viewer app is a tool for visualization, analysis, and preprocessing of lidar data. You can use this app to prepare your lidar data for advanced workflows such as labeling, segmentation, and calibration. The Lidar Viewer app offers several built-in preprocessing algorithms for use with lidar data, but it can also help you create custom preprocessing algorithms and integrate them into your preprocessing workflow.

Using the Lidar Viewer app, you can:

  • Create custom preprocessing algorithms and use them interactively within the Lidar Viewer app.

  • Combine custom preprocessing algorithms with other built-in and custom algorithms to create reusable preprocessing workflows.

This example shows how to:

  • Read the point cloud data and import it into the Lidar Viewer app.

  • Create and use a custom preprocessing algorithm to radially crop the point cloud.

  • Combine the custom radial cropping algorithm with a built-in algorithm to convert the unorganized point cloud to an organized point cloud.

  • Export the combined custom preprocessing workflow for reuse.

Read Point Cloud Data

Read the point cloud data into the workspace using the pcread function. This example uses the Ouster point cloud data.

% Read Ouster Point Cloud Data
fileName = fullfile(matlabroot,"examples","deeplearning_shared","data");
fileName = fullfile(fileName,"ousterLidarDrivingData.pcd");
ptCloud = pcread(fileName);

To determine whether the point cloud data is organized or unorganized, display the size of the Location property of the pointCloud object ptCloud. If the point cloud coordinates are in the form, M-by-N-by-3, the point cloud is an organized point cloud. If the point cloud coordinates are in the form, M-by-3, the point cloud is an unorganized point cloud.

disp(size(ptCloud.Location))
         64        1024           3

For more information about the organized and unorganized point clouds, see What are Organized and Unorganized Point Clouds?

Load Point Cloud Data into Lidar Viewer

Open the Lidar Viewer app from the MATLAB® command prompt, by entering this command.

lidarViewer

Alternatively, you can select the app from the Image Processing and Computer Vision section of the Apps tab.

On the app toolstrip, select Import > From Workspace. In the Import from Workspace dialog box, select ptCloud and click OK.

Import Workspace dialog box

The app loads the point cloud data and displays it in the Point Cloud Display pane.

Point cloud data displayed in Point Cloud Display pane

Create Custom Preprocessing Algorithm

Create Custom Algorithm

On the app toolstrip, select Edit Point Cloud to open the Edit tab. You can create custom spatial and temporal preprocessing algorithms in class-based and function-based formats and import them into the app. To create a class-based spatial preprocessing algorithm, first click Add Algorithm and select New > Class Template from the Spatial Algorithms section.

Add Algorithm list, Spatial Algorithms section, New Class Template selected

MATLAB opens a new script containing the code template and instructions for creating a class-based definition for your custom algorithm. Using this code, you can also define user interface elements for tuning the algorithm parameters in the Algorithm Parameters pane.

Write Custom Preprocessing Class Definition

Create a +lidar/+lidarViewer package directory within a folder that is already on the MATLAB path to save the custom preprocessing class definition.

These are the steps to write a custom preprocessing class definition inherited from lidar.internal.lidarViewer.edits.EditAlgorithm.

  1. Define descriptive properties for the algorithm, such as a unique EditName, Icon, and Description.

  2. Define the properties that manage algorithm execution.

    (Optional) Define a method that overwrites the initialization method of the superclass.

  3. Define methods to capture parameters and set up the user-interface panel.

    1. Define a method to package parameters in a structure.

    2. Create the user-interface panel and configure it to capture parameters.

    3. Define a callback function for the user-interface panel.

  4. Define the method that processes the point cloud.

For example, to radially crop the point cloud i.e. discard points outside a spherical region of certain radius, write the custom preprocessing class definition as follows.

%   Copyright 2021 The MathWorks, Inc.

% Name the class as radialCrop by replacing the default value
classdef radialCrop < lidar.internal.lidarViewer.edits.EditAlgorithm

    %-------------------------------------------------------------------
    % Step 1: Define the properties that describe the alogorithm. These
    % properties include a unique EditName, Icon and Description  
    % of the edit algorithm. The name of the algorithm and the icon
    % will be displayed in the edit algorithm gallery.
    properties (Constant)
       % Name the algorithm
       EditName = 'Radial Crop';
       % Set an icon
       Icon = fullfile(matlabroot, 'toolbox', 'lidar', 'lidar', ...
                '+lidar', '+internal', '+lidarViewer', '+view', ...
                '+icons', 'customClassEdit_24.png');
       % Give a description
       Description = "Discard points outside a certain radius";
    end
    
    %------------------------------------------------------------------
    % Step 2: Define properties to use in the algorithm. 
    % These properties are user-defined.
    properties
        % User interface elements defined as properties of the class
        elementsUI
    end
    
%   %------------------------------------------------------------------
%   % Optional Step: Define this method to set up the class properties.
%   methods
%   % a) This is an optional method.
%   %    The super class initializes the PointCloud property that 
%   %    stores the point cloud object present in the current frame 
%   %    when this edit operation is called. Overwrite this method if 
%   %    additional steps are required.
%   %
%       % Use this function as an initialization step.
%       %
%       function setUpEditOperation(this, ptCld)
%           this.PointCloud = ptCld;
%       %---------------------------------------------------------------
%       % Place your code here
%       %---------------------------------------------------------------
%       end
%   end

    %-------------------------------------------------------------------
    % Step 3: Define methods to capture the algorithm parameters and to 
    % configure the panel.
    methods
        % a) This function packages the algorithm parameters into a
        %    structure, params. This structure is passed to the
        %    applyEdits() function defined below. This function call
        %    process the input point cloud. The structure must be 
        %    compatible with the one used in applyEdits() function.
        function params = getCurrentParams(this)
            % Create empty structure
            params = struct(); 
            % Add user-defined parameter Radius as struct field
            params.Radius = this.elementsUI.Value; 
        end
        
        % b) This function creates the configuration panel with the
        %    UI components required to tune the algorithm.
        function setUpAlgorithmConfigurePanel(this, panel)
            % Label for numeric edit field
            uilabel(panel,Position=[25 150 100 22],Text="Radius"); 
            % Create numeric edit field with callback 
            this.elementsUI = uieditfield(panel,"numeric", ...
              ValueChangedFcn= ...
              @(elementsUI,event) configurationPanelCallback(this));
            % Set position of edit field 
            this.elementsUI.Position = [75 150 100 22]; 
            % Set initial value of parameter
            this.elementsUI.Value = 0;
            % Set limits for parameter input
            this.elementsUI.Limits = [0 inf];
            % Set decimal display format of edit field
            this.elementsUI.ValueDisplayFormat = "%.2f";
        end

        % Define callback function for configuration panel. 
        % This function is not given in the boilerplate code.
        function configurationPanelCallback(this)
            % To see the real-time output on changing the parameters
            evt = this.createEventData(); 
            notify(this,"PointCloudChanging",evt);
            % Get current parameters on event in user-interface
            getCurrentParams(this);
        end
    end
    
    %-------------------------------------------------------------------
    % Step 4: Define method to process the input point cloud object
    % using the parameters passed.
    methods (Static)
        % a) This function calls the algorithm to process the point
        %    cloud using the parameters defined in params. Note that
        %    the param structure is created using the getCurrentParams()
        %    function defined above.
        function ptCldOut = applyEdits(ptCldIn, params)  
            % Find points in spherical region 
            [croppedLocations, ~] = ...
                findNeighborsInRadius(ptCldIn,[0 0 0],params.Radius);
            % Crop point cloud
            ptCldOut = select(ptCldIn,croppedLocations); 
        end
    end

end
Save the algorithm class file to the package directory.

Import and Use Custom Preprocessing Algorithm

To import the algorithm into Lidar Viewer, on the app toolstrip, select Add Algorithm > From File > Import Class from the Spatial Algorithms section. Then, in the dialog box, select the algorithm class file and click Open, adding the custom algorithm to the Spatial Algorithms section of the toolstrip. If you do not see the custom algorithm on the toolstrip, select Add Algorithm > Refresh List.

Spatial algorithm toolstrip containing custom algorithm

Select your algorithm and tune the parameters in the user-interface.

Tune parameters of imported custom algorithm

The Lidar Viewer app dynamically updates the point cloud as you tune the parameters, enabling you to see the results in real-time.

Display of radially cropped point cloud

After tuning the parameters, select OK. The History pane records the preprocessing operation.

History panel showing Radial Crop operation details

To finish editing using this algorithm, select Cancel.

Your preprocessed point cloud is ready for export. However, if you wish to create a custom preprocessing workflow with multiple preprocessing steps, you can combine multiple preprocessing algorithms.

Combine Multiple Preprocessing Algorithms

The custom radial cropping operation generates an unorganized point cloud. To convert the unorganized point cloud to an organized point cloud, use the built-in Unorganize to Organize algorithm. To configure the algorithm for the Ouster sensor used to capture the point cloud data, in the Algorithm Parameters pane, specify Sensor Name as OS1Gen1-64.

Select built-in Unorganize to Organize algorithm from Spatial Algorithms section.

Algorithm parameters for Ouster device OS1Gen1-64

After tuning the parameters, select OK. The History pane records the preprocessing operation.

History panel showing Unorganize to Organize operation details

To finish editing using this algorithm, select Cancel.

Export Custom Preprocessing Workflow to MATLAB Function

Export this combination of preprocessing operations, with their parameters, as a function for future reuse. In the History pane, select Export To Function. The app creates a function script containing your custom preprocessing workflow function. Save this function with an appropriate name. The function accepts a pointCloud object as input and outputs a processed pointCloud object. To finalize your edits to the point cloud and return to the Lidar Viewer tab, on the app toolstrip, select Accept.

To use the exported preprocessing function, from the Edit tab of the Lidar Viewer toolstrip, select Add Algorithm > From File > Import Function in the Spatial Algorithms section, and select the exported function. You can then apply the imported function to the point cloud directly from the Algorithm section of the toolstrip.

Add Algorithm list, Spatial algorithms section, Import Function From File selected

Export Point Cloud Data from Lidar Viewer

You can export point clouds as PCD or PLY files. After processing the point cloud, on the app toolstrip, select Export Point Cloud.

Export Point Cloud dialog box

In the Export Point Cloud dialog box, select the preprocessed point cloud. Then, in the Provide path to the destination folder box, specify a destination file path or browse to the destination folder. To export the point cloud to the specified destination, select OK.

See Also

Apps

Functions

Objects

Related Topics