IMAGE 3D into 2D

7 views (last 30 days)
Sergio
Sergio on 11 Nov 2011
I need to change a 3D image into 2D. However the image should stay as coloured, not gray. Because I need to generate the histogram; and imhist() only works on 2D coloured image.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Nov 2011
[indI, cmapI] = rgb2ind(I);
imhist(indI, cmapI)

More Answers (1)

Image Analyst
Image Analyst on 11 Nov 2011
Huh? What are you talking about? If you want the histograms of the 3 color channels, then say so. If you turn an rgb image into an indexed image, the gray scale image will look like junk, until you apply the colormap which will apply a pseudocolor look up table to make it look like the color image. But the underlying grayscale image is still junk and the histogram of it is also junk and totally meaningless. You want proof? Just run this code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
% set(gcf, 'Position', get(0,'Screensize'));
set(gcf, 'units','normalized','outerposition',[0 0 1 1])
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get RGB histograms
[pixelCountsR GLs] = imhist(redChannel);
[pixelCountsG GLs] = imhist(greenChannel);
[pixelCountsB GLs] = imhist(blueChannel);
subplot(2, 2, 2);
plot(GLs, pixelCountsR, 'r-');
hold on;
plot(GLs, pixelCountsG, 'g-');
plot(GLs, pixelCountsB, 'b-');
grid on;
title('RGB Histograms', 'FontSize', fontSize);
[indI, cmapI] = rgb2ind(rgbImage, 256);
subplot(2, 2, 4);
imhist(indI, cmapI)
title('Histogram of Indexed Image', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(indI, []);
title('Indexed Image', 'FontSize', fontSize);
message = sprintf('Look at the indexed image in grayscale\nIt is junk and its histogram is junk.\nNow Click OK to apply the colormap.');
uiwait(msgbox(message));
colormap(cmapI)
title('Indexed Image with colormap applied', 'FontSize', fontSize);
Now, take a step back and say what you want to achieve without speculating on a way to achieve that - I'll tell you how to do it.
  3 Comments
Image Analyst
Image Analyst on 11 Nov 2011
Right. I noticed that. It doesn't seem of much use to me. I doubt you'd ever use that for color segmentation and I don't know what you'd do with it. There is also a related thing called Color Frequency Image and there's a File Exchange submission on that: http://www.mathworks.com/matlabcentral/fileexchange/28164-color-frequency-image in case anyone's interested.
Sergio
Sergio on 11 Nov 2011
Thanks.... thats is that i need!
i will try!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!