开发者

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

开发者 https://www.devze.com 2023-01-12 12:51 出处:网络
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 wa

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.

开发者_C百科

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?


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.


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


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;


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


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:

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

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

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

Pure red:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消