MATLAB Answers

shefali
0

r g b components of an image

Asked by shefali
on 21 Oct 2013
Latest activity Answered by mithlesh arya on 21 Aug 2017
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.

  0 Comments

Sign in to comment.

3 Answers

Answer by sixwwwwww
on 21 Oct 2013

Dear Shefali, one way to just show three separate color images is as follows:
img = imread('filename');
figure, imshow(img)
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);
a = zeros(size(img, 1), size(img, 2));
just_red = cat(3, red, a, a);
figure, imshow(just_red)
just_green = cat(3, a, green, a);
figure, imshow(just_green)
just_blue = cat(3, a, a, blue);
figure, imshow(just_blue)
I hope it helps. Good luck!

  0 Comments

Sign in to comment.


Answer by Image Analyst
on 22 Oct 2013

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.

  0 Comments

Sign in to comment.


Answer by mithlesh arya on 21 Aug 2017

In my image, I have lots of red cells..which are not useful for me...I want to remove these cells...how to remove these cells.

  0 Comments

Sign in to comment.