MATLAB: R g b components of an image

r g b components

i=imread('car.jpg'); r=i(:,:,1); imshow(r); my result after this is a black and white image. instead it should display red component of the image.. please suggest something to get exact result.

Best Answer

That should work. The only reason it wouldn't is if i is a floating point array, in which case you'd have to use imshow(i, []) to see it. By the way, use a variable name other than i (the imaginary variable) for your image name, such as rgbImage or something descriptive like that. If you have a floating point image and values exceed 1 they will appear as white and if they are less than 0 they will appear as black. Not sure if you meant a black and white image like you said, or if you really meant a gray scale (monochrome) image that has 256 gray levels, not just 2 levels, those being pure black and pure white.
Of course you know that each color channel by itself is a gray scale image , don't you? In other words if I extract each color channel:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
each one of those arrays is just a simple monochrome gray scale array, and will appear as gray scale if you use imshow(). If you want to see it appear in the color of the color channel that it represents then you'd have to either use cat() to make an RGB image out of it:
z = zeros(size(redChannel));
redAppearingImage = cat(3, redChannel, z, z);
imshow(redAppearingImage);
or use colormap() to apply a color to it (which will use less memory).
imshow(redChannel);
myColorMap = [[0:255]', zeros(256,1), zeros(256,1)];
colormap(myColorMap);
colorbar;
Finally you might have fun running my RGB histogram demo attached below.