问题
I have a 3D matrix im
which represents an RGB image. I can do
imshow(im)
to display the image.
I want to display only one of the RGB channels at a time: I want to display the red channel and I want it to appear red.
I've tried
imshow(im(:,:,1))
but it displays the grayscale image (which is not what I want).
How do I display the red channel and make it appear red?
回答1:
I have three proposals for you.
1.
Use the imagesc
function and choose a red color palette.
2.
Clear the other color channels: im(:,:,2:3) = 0; imshow(im);
3. Use the ind2rgb
function with a color map you build accordingly.
回答2:
Try this:
% display one channel only
clear all;
im=imread('images/DSC1228L_512.jpg');
im_red = im;
im_green = im;
im_blue = im;
% Red channel only
im_red(:,:,2) = 0;
im_red(:,:,3) = 0;
figure, imshow(im_red);
% Green channel only
im_green(:,:,1) = 0;
im_green(:,:,3) = 0;
figure, imshow(im_green);
% Blue channel only
im_blue(:,:,1) = 0;
im_blue(:,:,2) = 0;
figure, imshow(im_blue);
回答3:
Try this
I = imread('exemple.jpg');
%Red component
R = I(:,:,1);
image(R), colormap([[0:1/255:1]', zeros(256,1), zeros(256,1)]), colorbar;
%Green Component
G = I(:,:,2);
figure;
image(G), colormap([zeros(256,1),[0:1/255:1]', zeros(256,1)]), colorbar;
%Blue component
B = I(:,:,3);
figure;
image(B), colormap([zeros(256,1), zeros(256,1), [0:1/255:1]']), colorbar;
回答4:
You mean you want to extract red color only? using im(:,:,1) only seperate the red channel from the 3D image and convert it to a 2D image. Try this simple code:
im=imread('example.jpg');
im_red=im(:,:,1);
im_gray=rgb2gray(im);
im_diff=imsubtract(im_red,im_gray);
imshow(im_diff);
回答5:
For a better view, you could calculate and display the pure color. The formula Rp = Rc / (Rc + Gc + Bc). And a code example for the color red:
imagesc(im(:,:,1) ./ (im(:,:,1) + im(:,:,2) + im(:,:,3)))
This will make the color display more clearly, since the other colors have been filtered out.
I will try to illustrate it with an example:
Original image:

Red channel of image (im(:,:,1)
):

Pure red:

来源:https://stackoverflow.com/questions/3547029/how-do-i-display-the-red-channel-of-an-image-in-matlab