Could someone please explain what the code means?

2 次查看(过去 30 天)
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

回答(2 个)

Image Analyst
Image Analyst 2014-5-22
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 2014-5-22
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 个评论
Walter Roberson
Walter Roberson 2014-5-22
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.

请先登录,再进行评论。

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by