MATLAB Answers

PLY/ RGB to 2D Grayscale Image

6 views (last 30 days)
Wookie
Wookie on 7 Dec 2020
Answered: Image Analyst on 29 Dec 2020
I have a PLY from a stereo vision sensor (1024 x 1280). The PLY gives:
- Location (X,Y,Z) [1310442 x 3]
- Color (R, G, B) [1310442 x 3] and
- XLimits, YLimits, ZLimits.
I am trying to convert the Color into a grayscale image/matrix. My attempt has been the following:
Image = ptCloud.Color(:,:,1);
R = double(Image(:,1));
G = double(Image(:,2));
B = double(Image(:,3));
I = 0.2989 * R + 0.5870 * G + 0.1140 * B; % For grayscale
%
% Since the matrix size does not equal 1024 x 1280, I add zeros to the end of I to compensate
%
Image_2d = reshape (I, 1024, 1280)
The image on the left is by plotting the PLY with MatLab's pcshow feature. This is what I want to obtain as an image/matrix but instead by following the above code... I get the image on the right which does not look correct!

Answers (2)

Rohit Pappu
Rohit Pappu on 29 Dec 2020
Based on the above code,
  • Image stores the Color Matrix of just the Red channel
  • R, G, B refer to the values of first, second and third column of Image matrix respectively
A plausible workaround could be
Image = ptCloud.Color; %% Store the entire m-by-n-by-3 color matrix in Image
%% Extract the Red , Green and Blue Channels
R = double(Image(:,:,1));
G = double(Image(:,:,2));
B = double(Image(:,:,3));
%% the rest of the code is same
  1 Comment
Image Analyst
Image Analyst on 29 Dec 2020
Or
[R, G, B] = imsplit(Image);
Or
grayImage = rgb2gray(Image);

Sign in to comment.


Image Analyst
Image Analyst on 29 Dec 2020
What does this show?
whos Image
Assuming Image is an RGb image and you want grayscale, you can do
grayImage = rgb2gray(Image);
I'd recommend agains using Image as the name of your image since image is the name of a built-in function.
From the pseudocolored image on the right, it looks like you're using imagesc() which applies a colormap by default that is usually not wanted or helpful. I never use that. I use imshow(). So just use imshow() with no colormap.
imshow(grayImage);
If you need more help, attach ptCloud in a .mat file:
save('answers.mat', 'ptCloud');

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!