Why does the red plane contain the whole image?
Show older comments
I am extracting the R,G,B plane from an image. And then I'm displaying them. I have observed a phenomenon that almost all the picture is in Red plane. I am new to image processing. My Code:
picture = imread('/home/parnab/pen.jpg');
Rplane = picture(:,:,1);
Gplane = picture(:,:,2);
Bplane = picture(:,:,3);
[r,c,z] = size(picture);
RedImage(1:r,1:c,1) = Rplane;
RedImage(1:r,1:c,2) = zeros(r,c);
RedImage(1:r,1:c,3) = zeros(r,c);
GreenImage(1:r,1:c,1) = zeros(r,c);
GreenImage(1:r,1:c,2) = Gplane;
GreenImage(1:r,1:c,3) = zeros(r,c);
BlueImage(1:r,1:c,1) = zeros(r,c);
BlueImage(1:r,1:c,2) = zeros(r,c);
BlueImage(1:r,1:c,3) = Bplane;
subplot(2,3,1),imshow(RedImage),
subplot(2,3,2),imshow(GreenImage),
subplot(2,3,3),imshow(BlueImage),
subplot(2,3,5),imshow(RedImage + GreenImage + BlueImage);
Output:

This is happening for all the images I have tested so far. Please help.
Original Image

link: https://drive.google.com/file/d/0BwdBHI1607sZQUZKMlB5VE0wc2M/view?usp=sharing
2 Comments
Guillaume
on 28 Oct 2016
Please attach your original image for us to try your code.
Note that a much simpler way of generate the three colour plane images would be:
RedImage = picture;
GreenImage = picture;
BlueImage = picture;
RedImage(:,:, [2 3]) = 0; %set green and blue plane to 0
GreenImage(:, :, [1 3]) = 0; %set red and blue plane to 0
BlueImage(:, :, [1 2]) = 0; %set red and green plane to 0
PARNAB SANYAL
on 28 Oct 2016
Accepted Answer
More Answers (2)
Image Analyst
on 29 Oct 2016
Guillaume's answer is a good one - a very detailed explanation. If you like his answer, go ahead and officially "Accept" it. I'm just going to add an alternate way of doing it, and that is to create a black plane and build your RGB image by using cat() with the color plane you extracted along with the black plane. Also note I used class(Rplane) in zeros() instead of 'uint8' so this code will work with uint16 images also.
% Read in original image.
picture = imread('peppers.png');
% Extract individual color planes into grayscale (monochrome) images.
Rplane = picture(:,:,1);
Gplane = picture(:,:,2);
Bplane = picture(:,:,3);
blackPlane = zeros(size(Rplane), class(Rplane)); % Create a new plane of all zeros that's the same size and class as the original color planes.
% Put individual color planes into a new RGB Color images
% in the appropriate plane so they show up as the color
% that they were extracted from.
RedImage = cat(3, Rplane, blackPlane, blackPlane);
GreenImage = cat(3, blackPlane, Gplane, blackPlane);
BlueImage = cat(3, blackPlane, blackPlane, Bplane);
% Display them.
fontSize = 20;
subplot(2,2,2),imshow(RedImage), title('Red Plane as red plane in RGB image', 'FontSize', fontSize);
subplot(2,2,3),imshow(GreenImage), title('Green Plane as red plane in RGB image', 'FontSize', fontSize);
subplot(2,2,4),imshow(BlueImage), title('Blue Plane as red plane in RGB image', 'FontSize', fontSize);
subplot(2,2,1),imshow(picture), title('Original RGB image', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
1 Comment
PARNAB SANYAL
on 29 Oct 2016
Image Analyst
on 29 Oct 2016
A third way is to leave the Rplane, etc. alone and don't create an RGB image and simply use the colormap to make the images appear in the right color.
% Initialization / clean-up code.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 20;
% Read in original image.
picture = imread('peppers.png');
% Extract individual color planes into grayscale (monochrome) images.
Rplane = picture(:,:,1);
Gplane = picture(:,:,2);
Bplane = picture(:,:,3);
% Make up colormaps.
maxGL = double(intmax(class(Rplane)))
ramp = (0 : maxGL)' / maxGL;
z = zeros(maxGL+1, 1);
redColorMap = [ramp, z, z]
greenColorMap = [z, ramp, z]
blueColorMap = [z, z, ramp]
% Display them.
fontSize = 20;
h2 = subplot(2,2,2),imshow(Rplane), title('Red Plane as indexed image via colormap', 'FontSize', fontSize);
colormap(h2, redColorMap), colorbar;
h3 = subplot(2,2,3),imshow(Gplane), title('Green Plane as indexed image via colormap', 'FontSize', fontSize);
colormap(h3, greenColorMap), colorbar;
h4 = subplot(2,2,4),imshow(Bplane), title('Blue Plane as indexed image via colormap', 'FontSize', fontSize);
colormap(h4, blueColorMap), colorbar;
subplot(2,2,1),imshow(picture), title('Original RGB image', 'FontSize', fontSize);
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

Categories
Find more on Red in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!