How do I display the red channel of an image in Matlab?

41,877

Solution 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.

Solution 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);

Solution 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;

Solution 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);
Share:
41,877
snakile
Author by

snakile

Updated on March 09, 2020

Comments

  • snakile
    snakile about 4 years

    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?