主要内容

激光雷达相机标定和融合

R2026b

此示例说明如何估计激光雷达传感器与相机之间的刚性变换,以及如何使用标定结果融合来自激光雷达传感器的点云数据和来自相机的图像数据。

概述

激光雷达传感器和相机通常配合使用,因为它们提供互补的信息。激光雷达传感器捕获三维空间数据,而相机捕获的图像包含更多关于外观和纹理的信息。通过融合来自这两种传感器的数据,您可以增强目标检测和分类工作流。激光雷达相机标定会估计一个变换矩阵,该矩阵描述激光雷达传感器相对于相机的相对旋转和平移。此变换可用于融合激光雷达和相机数据。

此图展示了激光雷达相机标定的工作流。该过程首先使用标定板(例如棋盘格、ChArUco 板或 AprilGrid),并捕获多个帧,在这些帧中标定板在点云和图像中均可见。接下来,您在点云和图像中检测标定板,并在激光雷达坐标系和相机坐标系中估计标定板的角点。图的左侧展示了激光雷达坐标系中包含检测到的标定板的点云,图的右侧展示了相机坐标系中检测到的标定板。标定会输出一个刚性变换,用来将激光雷达坐标系中的标定板角点对齐到相机坐标系中的标定板角点,并以 rigidtform3d (Image Processing Toolbox) 对象表示该变换。有关激光雷达相机标定的详细信息,请参阅什么是激光雷达-相机标定?

此示例使用一对同步的 PNG 图像和 PCD 点云,点云由 Ouster OS1 激光雷达传感器采集。此示例假设您已估计相机的内参。有关计算相机内参的详细信息,请参阅Evaluating the Accuracy of Single Camera Calibration (Computer Vision Toolbox)。

加载标定数据

将图像和点云数据加载到工作区。

imageDataPath = fullfile(toolboxdir("pointcloud"),"pcdata","calibration","images");
imds = imageDatastore(imageDataPath);
imageFileNames = imds.Files;

ptCloudFilePath = fullfile(toolboxdir("pointcloud"),"pcdata","calibration","pointClouds");
pcds = fileDatastore(ptCloudFilePath,ReadFcn=@pcread);
ptCloudFileNames = pcds.Files;

将相机内参加载到工作区。

cameraIntrinsicFile = fullfile(imageDataPath,"intrinsics.mat");
intrinsics = load(cameraIntrinsicFile).intrinsics;

检测图像中的标定板角点

禁用关于使用对称标定板的警告。虽然不推荐使用对称标定板,但均匀的边距以及有限的标定板旋转(在采集的图像中小于 45 度)可确保对称性不会影响结果。

currentState = warning;
warning("off","vision:calibrate:boardShouldBeAsymmetric")

检测棋盘格点。

[imagePoints,patternDims] = detectCheckerboardPoints(imageFileNames,PartialDetections=false);

还原原始警告状态。

warning(currentState)

根据下图指定棋盘格参数。

以毫米为单位指定方格大小。

squareSize = 100;

以毫米为单位指定边距 [P1 P2 P3 P4]。

padding = [100 100 100 100];

估计相机坐标系中的标定板角点。

[cornersCamera,boardSize] = estimateBoardCornersCamera("checkerboard",imagePoints,...
    intrinsics,patternDims,squareSize,Padding=padding);

可视化相机坐标系中所有检测到的标定板。

figure
cam = plotCamera(Size=0.25,Opacity=0.8);
hold on

numImages = numel(imageFileNames);
plotColors = hsv(numImages);
cornerIdx = [1 2 3 4 1];
for i = 1:numImages
    plot3(cornersCamera(cornerIdx,1,i),cornersCamera(cornerIdx,2,i),cornersCamera(cornerIdx,3,i),Color=plotColors(i,:),LineWidth=2)
end
set(gca,CameraUpVector=[-1 0 0]);

Figure contains an axes object. The axes object contains 20 objects of type line, text, patch.

检测点云中的标定板角点

预处理点云:首先选择感兴趣区域以加快检测,然后去除地面点。

roi = [-5 5 -Inf Inf -Inf Inf];

numPtClouds = numel(ptCloudFileNames);
ptCloudArr = repmat(pointCloud(zeros(0,3)),numPtClouds,1);

for i = 1:numPtClouds
    ptCloud = pcread(ptCloudFileNames{i});

    idx = findPointsInROI(ptCloud,roi);
    ptCloud = select(ptCloud,idx);

    [~,ptCloud] = segmentGroundSMRF(ptCloud,MaxWindowRadius=8,ElevationThreshold=0.2,ElevationScale=1.25);

    ptCloudArr(i) = ptCloud;
end

指定聚类阈值,用于对每个点云进行聚类,以便检测标定板。

clusterThreshold = 0.05;

指定平面拟合距离,用于为每个点云簇拟合平面,以便检测标定板。

planeFitDistance = 0.05;

估计激光雷达坐标系中的标定板角点。

[cornersLidar,ptCloudBoard] = estimateBoardCornersLidar(ptCloudArr,clusterThreshold,boardSize,...
    PlaneFitDistance=planeFitDistance);

可视化激光雷达坐标系中所有检测到的标定板。

figure
allPtClouds = pccat(ptCloudArr);
pcshow(allPtClouds)
hold on

for i = 1:numPtClouds
    pcshow(ptCloudBoard(i).Location,"white",ViewPlane="XZ")
    plot3(cornersLidar(cornerIdx,1,i),cornersLidar(cornerIdx,2,i),cornersLidar(cornerIdx,3,i),Color=plotColors(i,:),LineWidth=2)
end

Figure contains an axes object. The axes object contains 21 objects of type scatter, line.

求从激光雷达传感器到相机的变换

[lidarCameraTform,errors] = estimateLidarCameraTransform(ptCloudBoard,...
    cornersCamera,intrinsics,Lidar3DCorners=cornersLidar);

使用标定结果对齐激光雷达和相机坐标系后,可视化标定板。

figure
allPtCloudTransformed = pctransform(allPtClouds,lidarCameraTform);
pcshow(allPtCloudTransformed)
hold on
plotCamera(Size=0.25,Opacity=0.8)

for i = 1:numPtClouds
    ptCloudBoardTransformed = pctransform(ptCloudBoard(i),lidarCameraTform);
    pcshow(ptCloudBoardTransformed.Location,"white",ViewPlane="YX")
    cornersLidarTransformed = transformPointsForward(lidarCameraTform,cornersLidar(:,:,i));
    plot3(cornersLidarTransformed(cornerIdx,1),cornersLidarTransformed(cornerIdx,2),cornersLidarTransformed(cornerIdx,3),Color=plotColors(i,:),LineWidth=2)
    plot3(cornersCamera(cornerIdx,1,i),cornersCamera(cornerIdx,2,i),cornersCamera(cornerIdx,3,i),Color=plotColors(i,:),LineWidth=2)
end
title("Alignment after calibration")

Figure contains an axes object. The axes object with title Alignment after calibration contains 41 objects of type line, text, patch, scatter.

或者,您也可以按如下方式在每个图像中可视化标定板的对齐情况。

读取第一个图像并对其去畸变。

idxDataPair = 1;
I = imread(imageFileNames{idxDataPair});
[J,newIntrinsics] = undistortImage(I,intrinsics);

将来自相机和激光雷达的标定板角点投影到图像上。

projectedCornersCamera = projectLidarPointsOnImage(cornersCamera(:,:,idxDataPair),newIntrinsics,rigidtform3d);
projectedCornersLidar = projectLidarPointsOnImage(cornersLidar(:,:,idxDataPair),newIntrinsics,lidarCameraTform);

在图像上可视化投影后的标定板。将来自相机的标定板绘制为红色,将来自激光雷达传感器的标定板绘制为黄色。

figure
imshow(J)
hold on
showShape("polygon",projectedCornersCamera,Color="red")
showShape("polygon",projectedCornersLidar,Color="yellow")

Figure contains an axes object. The hidden axes object contains an object of type image.

可视化并评估标定误差

您可以使用以下误差度量来估计标定精度。

  • 平移误差是指点云中标定板角点的质心坐标与对应图像中标定板角点的质心坐标之间的差值,单位为米。

  • 旋转误差是指点云中标定板所定义的法线角与对应图像中标定板所定义的法线角之间的差值,单位为度。

  • 重投影误差是指点云中标定板角点的质心坐标投影(变换)到图像后,与对应图像中质心坐标之间的差值,单位为像素。

绘制每个数据对组的平移误差。

figure
bar(errors.TranslationError)
title("Translation Error")
xlabel("Data Pair")
ylabel("Error (meters)")

Figure contains an axes object. The axes object with title Translation Error, xlabel Data Pair, ylabel Error (meters) contains an object of type bar.

绘制每个数据对组的旋转误差。

figure
bar(errors.RotationError)
title("Rotation Error")
xlabel("Data Pair")
ylabel("Error (degrees)")

Figure contains an axes object. The axes object with title Rotation Error, xlabel Data Pair, ylabel Error (degrees) contains an object of type bar.

绘制每个数据对组的重投影误差。

figure
bar(errors.ReprojectionError)
title("Reprojection Error")
xlabel("Data Pair")
ylabel("Error (pixels)")

Figure contains an axes object. The axes object with title Reprojection Error, xlabel Data Pair, ylabel Error (pixels) contains an object of type bar.

根据这些误差度量,您可以选择从标定中排除部分数据对组,以提高结果的精度。例如,您可以按如下方式排除具有最大重投影误差的数据对组。

确定哪个数据对组具有最大的重投影误差。

[~,idxToExclude] = max(errors.ReprojectionError);

删除要排除的数据对组的值。

cornersCamera(:,:,idxToExclude) = NaN;
cornersLidar(:,:,idxToExclude) = NaN;

重复标定过程。

[lidarCameraTform,errors] = estimateLidarCameraTransform(ptCloudBoard,...
    cornersCamera,intrinsics,Lidar3DCorners=cornersLidar);

绘制排除所选数据对组后的重投影误差。

figure
bar(errors.ReprojectionError)
title("Reprojection Error")
xlabel("Data Pair")
ylabel("Error (pixels)")

Figure contains an axes object. The axes object with title Reprojection Error, xlabel Data Pair, ylabel Error (pixels) contains an object of type bar.

融合激光雷达和相机数据

在细化激光雷达相机标定结果之后,您可以使用传感器之间的变换来向点云添加颜色信息。此示例的这一节使用 MultiSensorDrivingData_Seg7_Seq30 数据,该数据包含使用前面几节中用于标定的相同传感器设置所采集的激光雷达和相机数据。请注意,该数据集大小约为 1.8 GB。

使用 helperDownloadDataset 支持函数将数据下载为结构体。

if ~exist('recordedData','var')
    recordedData = helperDownloadDataset();
end

激光雷达相机标定会产生一个将激光雷达坐标系对齐到相机坐标系的变换。要向点云添加颜色信息,您需要使用此变换的逆变换,也就是将相机坐标系对齐到激光雷达坐标系的变换。

cameraLidarTform = invert(lidarCameraTform);

现在,对于每个可用的数据对组,重复以下步骤:

1) 获取点云帧。

numFrame = 1;
ptCloud = recordedData.Lidar.PointClouds{numFrame};
figure
pcshow(ptCloud)

Figure contains an axes object. The axes object contains an object of type scatter.

2) 使用时间戳查找与该点云对应的图像。

cameraTimestamps = recordedData.Camera.Timestamps;
ptCloudTimestamp = recordedData.Lidar.Timestamps(numFrame);
[~,idxImage] = min(abs(cameraTimestamps - ptCloudTimestamp));

3) 获取对应的图像帧并对其去畸变。

I = recordedData.Camera.Images{idxImage};
[J,newIntrinsics] = undistortImage(I,intrinsics);
figure
imshow(J)

Figure contains an axes object. The hidden axes object contains an object of type image.

4) 使用 imlocalbrighten (Image Processing Toolbox) 函数增强图像的亮度。

J = imlocalbrighten(J);
figure
imshow(J)

Figure contains an axes object. The hidden axes object contains an object of type image.

5) 使用 fuseCameraToLidar 函数向点云添加颜色信息。

ptCloudColor = fuseCameraToLidar(J,ptCloud,newIntrinsics,cameraLidarTform);
figure
pcshow(ptCloudColor,MarkerSize=20)
ylim([10 110])

Figure contains an axes object. The axes object contains an object of type scatter.

现在对其余帧重复步骤 1-5。为了可视化结果,您可以使用 pcplayer,并在右下角添加一个坐标区,以便同时可视化图像。

设置 pcplayer 用于点云可视化。

% Close existing plots and clear player from previous runs.
close all
if exist("player","var")
    clear player
end
figure(WindowState="maximized")
axPtCloud = axes;
player = pcplayer([-60 60],[0 150],[-5 30],Parent=axPtCloud,MarkerSize=75,AxesVisibility="off");

修改相机位置和视图,以改善可视化效果。

axPtCloud.CameraPosition = [12 -105 5];
axPtCloud.CameraTarget = [-2 88 2];
axPtCloud.CameraUpVector = [0 0 1];
axPtCloud.CameraViewAngle = 5;

创建一个内嵌坐标区,用于可视化图像。

axImage = axes('Position',[0.79 0 0.25 0.25]);
box on;

对所有点云重复步骤 1-5。

numPtClouds = numel(recordedData.Lidar.PointClouds);

for i = 1:numPtClouds
    % Get point cloud and its corresponding timestamp
    ptCloud = recordedData.Lidar.PointClouds{i};
    ptCloudTimestamp = recordedData.Lidar.Timestamps(i);
    
    % Find the image that corresponds to the point cloud based on
    % timestamps
    [~,index] = min(abs(cameraTimestamps - ptCloudTimestamp));
    I = recordedData.Camera.Images{index};

    % Undistort the image
    J = undistortImage(I,intrinsics);
    
    % Enhance the color of the image
    J = imlocalbrighten(J);
    
    % Display the image
    imshow(J,Parent=axImage)
    
    % Add color information to the point cloud
    ptCloudColor = fuseCameraToLidar(J,ptCloud,newIntrinsics,cameraLidarTform);
    
    % Display the point cloud
    view(player,ptCloudColor);
end

Figure contains 2 axes objects. Hidden axes object 1 with xlabel X, ylabel Y contains an object of type scatter. Hidden axes object 2 contains an object of type image.

close(axPtCloud.Parent)

参考资料

[1] Zhou, Lipu, Zimo Li, and Michael Kaess.“Automatic Extrinsic Calibration of a Camera and a 3D LiDAR Using Line and Plane Correspondences.”In 2018 IEEE/RSJ International Conference on Intelligent Robots and Systems (IROS), 5562–69.Madrid:IEEE, 2018. https://doi.org/10.1109/IROS.2018.8593660.

[2] Arun, K. S., T. S. Huang, and S. D. Blostein.“Least-Squares Fitting of Two 3-D Point Sets.”IEEE Transactions on Pattern Analysis and Machine Intelligence PAMI-9, no. 5 (September 1987):698–700. https://doi.org/10.1109/TPAMI.1987.4767965.

另请参阅

App

主题