Could someone please explain what the code means?

1 view (last 30 days)
Duncan on 22 May 2014
Commented: Duncan on 22 May 2014
function displacements2D
%________________________________________Read images & Shadding correction
%k=input('Enter the image file name: ','s'); % input image; color image
r = rgb(:,:,1); g = rgb(:,:,2); b = rgb(:,:,3);
I would especially like to know about the 3rd last line. What's happening there? Thanks, any help would be appreciated

Answers (2)

Image Analyst
Image Analyst on 22 May 2014
rgb2 is the color image. Then they do an overly complicated way of making a copy of it in rgb. Then they extract out each color channel into separate 2D images r, g, and b. Then they get a gray scale version of the color image which is made up of a weighted combination of the red, green, and blue channel images.

Walter Roberson
Walter Roberson on 22 May 2014
The only thing that
does for you other than to copy rgb2 to rgb, is to throw an error in the case that the image read was greyscale instead of RGB. grayscale images would only have one plane instead of 3.
JPEG files cannot store pseudocolor images (which would also be 2D instead of 3D).
JPEG files also cannot store CMYK images (which would have 4 planes instead of 3) -- only TIFF files can store those.
If you wanted to check whether it was 3D or not then that should be done explicitly with ndims

Sign in to comment.


Find more on Printing and Saving in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by