Issue in separating single channel from color image

45 views (last 30 days)
I separate red channel from color image in Matlab, then apply some transformations, then view the image using "imshow" function. The issue is that image is shown as (almost) grayscale image instead of (expected) red(ish) image. The same is happening with green and blue channel, resulting in grayscale images.
here is color image used.
here is transformed (red channel) image.

Answers (2)

Stephen23
Stephen23 on 19 Dec 2018
Edited: Stephen23 on 19 Dec 2018
"The issue is that image is shown as (almost) grayscale image instead of (expected) red(ish) image"
It is possible that your expectation is incorrect. If I gave you this:
M = [1,0;0.5,1]
can you tell me if it represents four pixels of red data, green data, or blue data? Unless I give you extra information, you can't. Maybe it doesn't even represent any plane of RGB data, but is simply a binary image. Or maybe a grayscale image... or is not an image at all... there is no way to tell just from the numeric data alone. With MATLAB (or any other language) it is exactly the same: those numbers do not magically somehow remember that they are "red".
You have to tell MATLAB what those colors are either by defining an MxNx3 array (i.e. a standard RGB image) and displaying that, or by displaying that image as an indexed image and using an appropriate colormap.
>> M = [1,0;0.5,1]
M =
1.0 0.0
0.5 1.0
>> Z = zeros(size(M));
>> I = cat(3,M,Z,Z);
>> image(I)
I am sure that you can figure out the green and blue planes.

Image Analyst
Image Analyst on 19 Dec 2018
Edited: Image Analyst on 19 Dec 2018
Look at this code:
rgbImage = imread('lena.jpg');
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Now each of the separate channels is single-valued. So if you display any one of them, they're a monochrome image and will normally display as gray scale. There is no color anymore in them since they just have a single gray level, instead of 3 values, an R value, a G value, and B value.
If you want, you can take any of those gray scale images and make them appear red with a colormap, or you can turn each into an RGB color image by putting that channel into the proper channel, and making the other channels all zero. See demo code below:
% Loads an RGB image and shows each color channel in monochrome, indexed, and RGB in its respective color.
clc; % Clear the command window.
clearvars;
close all
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
subplot(3, 4, 1);
rgbImage = imread('peppers.png');
imshow(rgbImage);
impixelinfo; % Let user mouse around and see pixel values and location.
fontSize = 12;
title('Original Image', 'FontSize', fontSize);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
%==================================================================================================
% RED CHANNEL : Show as gray scale, indexed, and RGB.
% Display red channel as monochrome.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 2);
imshow(redChannel);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel as Gray Scale', 'FontSize', fontSize);
% Display red channel as indexed red.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 6);
imshow(redChannel, 'Colormap', cmap);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel in Indexed Red', 'FontSize', fontSize);
colorbar;
% Display red channel as RGB by making the red channel the redChannel and the other channels all zero.
blackColorChannel = zeros(size(redChannel));
redRGB = cat(3, redChannel, blackColorChannel, blackColorChannel);
subplot(3, 4, 10);
imshow(redRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Red Channel as RGB', 'FontSize', fontSize);
%==================================================================================================
% GREEN CHANNEL : Show as gray scale, indexed, and RGB.
% Display green channel as monochrome.
cmap = [(0:255)', zeros(256, 1), zeros(256, 1)] / 256;
subplot(3, 4, 3);
imshow(greenChannel);
title('Green Channel Image as Gray Scale', 'FontSize', fontSize);
% Display green channel as green.
cmap = [zeros(256, 1), (0:255)', zeros(256, 1)] / 256;
subplot(3, 4, 7);
impixelinfo; % Let user mouse around and see pixel values and location.
imshow(greenChannel, 'Colormap', cmap);
title('Green Channel Image in Indexed Green', 'FontSize', fontSize);
colorbar;
% Display green channel as RGB by making the green channel the greenChannel and the other channels all zero.
greenRGB = cat(3, blackColorChannel, greenChannel, blackColorChannel);
subplot(3, 4, 11);
imshow(greenRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Green Channel as RGB', 'FontSize', fontSize);
%==================================================================================================
% BLUE CHANNEL : Show as gray scale, indexed, and RGB.
% Display blue channel as monochrome.
subplot(3, 4, 4);
imshow(blueChannel);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Blue Channel Image as Gray Scale', 'FontSize', fontSize);
% Display blue channel as blue.
cmap = [zeros(256, 1), zeros(256, 1), (0:255)'] / 256;
subplot(3, 4, 8);
imshow(blueChannel, 'Colormap', cmap);
title('Blue Channel Image in Indexed Blue', 'FontSize', fontSize);
colorbar;
% Display blue channel as RGB by making the blue channel the blueChannel and the other channels all zero.
blueRGB = cat(3, blackColorChannel, blackColorChannel, blueChannel);
subplot(3, 4, 12);
imshow(blueRGB);
impixelinfo; % Let user mouse around and see pixel values and location.
title('Blue Channel as RGB', 'FontSize', fontSize);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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')
0001 Screenshot.png
  2 Comments
tanveer haq
tanveer haq on 20 Dec 2018
The .zip file is attached. It includes a simple 3 steps procedure to combine the results. when you run it you will recive a gray scale image instead of an RGB image. This will higlight my problem more simply. Image Analyst and Stephen Cobeldick.
Stephen23
Stephen23 on 20 Dec 2018
Edited: Stephen23 on 20 Dec 2018
"This will higlight my problem more simply."
What is the problem? You created an RGB image with very similar R, G, and B values, so naturally these will be appear as gray or very unsaturated colors. If you expect more saturated colors, then you either made a mistake when you "apply some transformations", or your expectations do not reflect that actual transformations that you are applying.
It is easy to check the differences between R, G, and B values:
>> I = double(Enc_Img);
>> RG = abs(I(:,:,1)-I(:,:,2));
>> RB = abs(I(:,:,1)-I(:,:,3));
>> GB = abs(I(:,:,2)-I(:,:,3));
>> M = [RG(:),RB(:),GB(:)];
>> hist(M)
>> legend('R-G','R-B','G-B')
It is clear that most of the differences between R, G, and B values are quite small:
>> mean(M)
ans =
20.347 20.287 20.311
>> median(M)
ans =
17 17 17
>> max(M)
ans =
51 51 51
So far it is not clear what exactly you think the "problem" is: MATLAB is correctly displaying your RGB image (with very low saturation colors) as an RGB image (with very low saturation colors). What exactly is the "problem" ?

Sign in to comment.

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!