Could someone please explain what the code means?

3 views (last 30 days)
Duncan
Duncan on 22 May 2014
Commented: Duncan on 22 May 2014
function displacements2D
%________________________________________Read images & Shadding correction
k='Pattern_ab.jpg';
%k=input('Enter the image file name: ','s'); % input image; color image
rgb2=imread(k);
tic;
rgb=rgb2(:,:,1:3);
r = rgb(:,:,1); g = rgb(:,:,2); b = rgb(:,:,3);
gray=rgb2gray(rgb);
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
rgb=rgb2(:,:,1:3);
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
  3 Comments
Walter Roberson
Walter Roberson on 22 May 2014
rgb2(:,:,1:3) means to extract all rows and all columns and planes 1, 2, and 3 of the matrix rgb2. The first position after the "(" is row numbers, the second is column numbers, the third is plane numbers; beyond that would get to higher and higher hyperplanes.
rgb(:,:,2) would be extracting all rows and all columns of the 2nd hyperplane.
In an RGB image, the first hyperplane stores the red information, the second stores the green information, and the third stores the blue information.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!