How is making zero of a component in an RGB image different from taking individual array color panel?
5 views (last 30 days)
Show older comments
Sachin Motwani
on 20 Aug 2020
Commented: Sachin Motwani
on 22 Aug 2020
I tried to implement the following code
X = imread ('parrot.jpg');
%Method 1
figure;
sgtitle ('Color planes of R, G & B intensities in an RGB image');
subplot(2,2,1),imshow(X);
title('Original RGB Image')
subplot(2,2,2),imshow(X(:,:,1));
title('Red image')
subplot(2,2,3),imshow(X(:,:,2));
title ('Green Image')
subplot(2,2,4),imshow(X(:,:,3));
title ('Blue Image')
%Method 2
R,G,B = X;
figure;
subplot(2,2,1),imshow(X);
R(:,:,2:3) = 0; %Make 2nd and 3rd component zero
subplot(2,2,2),imshow(R);
G(:,:,[1 3]) = 0; %Make 1st and 3rd component zero
subplot(2,2,3),imshow(G);
B(:,:,1:2) = 0; %Make 1st and 2nd component zero
subplot(2,2,4),imshow(B);
Sum = R+G+B;
figure;
imshow(Sum); %Same as the original image
greySum = X(:,:,1)+X(:,:,2)+ X(:,:,3)
imshow(greySum); %not the same as the original image
I intended to extract the individual color panels from an RGB image.
Method 1 (based on my understanding of the concept): Take one panel (out of three) and display it.
Method 2 (got a code on MATLAB forum): Make the other two components zero, and then display the entire image.
Where does the difference lie? Please explain.
0 Comments
Accepted Answer
Walter Roberson
on 21 Aug 2020
When you interpret a 2D data array as a grayscale image, then you assume that the contributions of R, G, and B are all equal.
RGB = repmat(grayscale, 1, 1, 3);
equal red, green, blue contributions in RGB images show up as gray to humans.
When you take a colorplane out of an RGB image and put it into a different RGB array with the other two planes set to 0, like in method 2, then the value of the other two planes is 0, not "the same" as the plane that you kept. That will show up as monochrome planes of the major color, not as grayscale.
Sum = R+G+B;
When you have zeroed the other two color planes then when you add the 3D arrays, you just reproduce the original RGB image. There is the G and B have 0 in their red planes, so the red plane of Sum would effectively just be copied from the red plane of the original image.
greySum = X(:,:,1)+X(:,:,2)+ X(:,:,3)
the value associated with the red contribution gets added to the value associated with the green and blue contributions. The contributions interfere with each other.
Your imread() of the .jpg is going to return a uint8 array. When you add the three uint8 arrays together, the locations are going to "saturate" -- any sum that would have exceeded 255 is going to be set to 255. This results in a lot of bright areas on the resulting image.
If you were to use im2double() as you read in the image, then you would not get saturation of the double() data class. You would, however, get results in the range 0.0 to 3.0, and when you go to display, any result above 1.0 would be treated as 1.0, so you would get the same kind of bright saturation. However, you could do
greySum = (X(:,:,1)+X(:,:,2)+ X(:,:,3))/3;
in that case to get something in the range 0 to 1. This will not work for uint8 data.
Note that using
greySum = (X(:,:,1)+X(:,:,2)+ X(:,:,3))/3
will not be the same as rgb2gray() . It turns out that Green is responsible for more of our sensation of brightness, followed by red, and that blue does not give much contribution to brightness. When you add them weighted equally, you over-represent blue and red contribution to brightness.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!