Display Separated Color Channels of RGB Image
This example creates a simple RGB image and then separates the color channels. The example displays each color channel as a grayscale intensity image and as a color image.
Create an RGB image with uninterrupted areas of red, green, and blue. Display the image.
imSize = 200;
RGB = reshape(ones(imSize,1)*reshape(jet(imSize),1,imSize*3),[imSize,imSize,3]);
imshow(RGB)
title('Original RGB Image')
Separate the three color channels.
[R,G,B] = imsplit(RGB);
Display a grayscale representation of each color channel. Notice that each separated color plane in the figure contains an area of white. The white corresponds to the highest values (purest shades) of each separate color. For example, in the red channel image, the white represents the highest concentration of pure red values. As red becomes mixed with green or blue, gray pixels appear. The black region in the image shows pixel values that contain no red values, in other words, when R == 0
.
figure subplot(1,3,1) imshow(R) title('Red Channel') subplot(1,3,2) imshow(G) title('Green Channel') subplot(1,3,3) imshow(B) title('Blue Channel')
Display a color representation of each color channel. In these images, the desired color channel maintains its original intensity values and pixel values in the other two color channels are set to 0.
Create an all-black channel.
allBlack = zeros(size(RGB,1,2),class(RGB)); justR = cat(3,R,allBlack,allBlack); justG = cat(3,allBlack,G,allBlack); justB = cat(3,allBlack,allBlack,B);
Display all the channels in a montage.
figure montage({justR,justG,justB},'Size',[1 3], ... "BackgroundColor",'w',"BorderSize",10); title('Color Representation of the Red, Green, and Blue Color Channels');