matlabcamera-calibrationpoint-cloudsdisparity-mapping

'Color' must correspond to the number of input points when using pointCloud in MATLAB


I want to create a 3D Map from a stereo camera, to test this I use the two given MATLAB examples:

  1. https://de.mathworks.com/help/vision/ref/estimatecameraparameters.html ("Stereo Camera Calibration")
  2. https://de.mathworks.com/help/vision/examples/depth-estimation-from-stereo-video.html

I combined these two scripts into the following one:

% load left and right images
leftImages = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
    'calibration','stereo','left'));
rightImages = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
    'calibration','stereo','right'));

% calculate image points
[imagePoints,boardSize] = ...
  detectCheckerboardPoints(leftImages.Files,rightImages.Files);

% calculate world points
squareSize = 108;
worldPoints = generateCheckerboardPoints(boardSize,squareSize);


% calculate camera paramters
I = readimage(leftImages,1); 
imageSize = [size(I,1),size(I,2)];
stereoParams = estimateCameraParameters(imagePoints,worldPoints, ...
                                  'ImageSize',imageSize);


% get left and right image
frameLeftGray = imread(leftImages.Files{1});
frameRightGray = imread(rightImages.Files{1});
[frameLeftRect, frameRightRect] = ...
    rectifyStereoImages(frameLeftGray, frameRightGray, stereoParams);


% get disparity map    
disparityMap = disparity(frameLeftRect, frameRightRect);
figure;
imshow(disparityMap, [0, 128]);
title('Disparity Map');
colormap jet
colorbar

% create 3D Bar 
points3D = reconstructScene(disparityMap, stereoParams);

% Convert to meters and create a pointCloud object
points3D = points3D ./ 1000;

% This will fail
ptCloud = pointCloud(points3D, 'Color', frameLeftRect);

% Create a streaming point cloud viewer
player3D = pcplayer([-3, 3], [-3, 3], [0, 8], 'VerticalAxis', 'y', ...
    'VerticalAxisDir', 'down');

% Visualize the point cloud
view(player3D, ptCloud);

However, upoon executing I will be greeted with the following error message:

Error using pointCloud/set.Color (line 545) 'Color' must correspond to the number of input points.

Error in pointCloud (line 151) this.Color = C;

Error in DepthEstimation (line 45) ptCloud = pointCloud(points3D, 'Color', frameLeftRect);

When trying the examples 1) and 2) seperatly they work just fine. I think it has something to do with the image sizes itself. However resizing it, would result in wrong camera parameters.

So is there any other way to fix the error with the "Color" parameter present?

Thank you in advance


Solution

  • You are using a grey scale image as input, thus it does not match up with the RGB points. Create an RGB image from the grey scale image and then use this.

    rgb = cat(3,frameRightRect,frameRightRect,frameRightRect);
    ptCloud = pointCloud(points3D, 'Color', rgb);