Why does the red plane contain the whole image?

12 views (last 30 days)
PARNAB  SANYAL
PARNAB SANYAL on 28 Oct 2016
Answered: Image Analyst on 29 Oct 2016
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
PARNAB  SANYAL
PARNAB SANYAL on 28 Oct 2016
Your solution is working as I wanted. but please help me with my code.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 28 Oct 2016
The problem is one of double vs uint8.
Your original image is of type uint8, so are Rplane, Gplane and Bplane.
Assuming that RedImage does not exist already, when you do:
RedImage(1:r,1:c,1) = Rplane;
you create a matrix RedImage of the same type as Rplane, so uint8. You then assign double arrays to the other two planes but since the matrix is uint8 the double values are converted to uint8, so RedImage is uint8 in the end.
For the other two images, assuming they don't exist anymore, you first create them with:
|GreenImage| = zeros(r, c);
|BlueImage| = zero(r, c);
so they're created of type double. When you copy Gplane or Bplane matlab convert the source type to the destination type, so you end up with double images in the range [0 255]. Because matlab assumes the range [0 1] for double image, when you display these any value above 1 is considered maximum intensity, hence when your blue image is shown all blue.
There are many ways you could have avoided this:
  • Always create the images by copying the uint8 colour plane first, as I've done in my comment
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
  • Tell zeros to create a uint8 matrix:
RedImage = zeros(1:r, 1:c, 3, 'uint8');
GreenImage = zeros(1:r, 1:c, 3, 'uint8');
BlueImage = zeros(1:r, 1:c, 3, 'uint8');
RedImage(:, :, 1) = Rplane;
GreenImage(:, :, 2) = Gplane;
BlueImage(:, :, 3) = Bplane;
  • Make sure you assign zeros of the correct class
GreenImage(1:r, 1:c, 1) = zeros(r, c, 'uint8);
%note that this could be simply written as:
GreenImage(1:r, 1:c, 1) = uint8(0);
  • Not worry about the class of the image (not ideal) and tell imshow to ignore the default display range and always use [0 255]:
subplot(2,3,1),imshow(RedImage, [0 255]),
subplot(2,3,2),imshow(GreenImage, [0 255]),
subplot(2,3,3),imshow(BlueImage, [0 255]),

More Answers (2)

Image Analyst
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')

Image Analyst
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')

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!