From: PANDIAN` NITHYANANDAM on
hi friends

i am new bee


can you same me how to plot historgram for a color image which is of 24bit depth i.e. R,G,B
From: ImageAnalyst on
On Jun 23, 3:05 am, "PANDIAN` NITHYANANDAM" <nithyanand...(a)ssn.edulin>
wrote:
> hi friends
>
> i am new bee
>
> can you same me how to plot historgram for a color image which is of 24bit depth i.e. R,G,B

-------------------------------------------------------------------------------------
IMPORTANT: Be sure to join any lines split into two by the news
reader.

%-------------------------------------------------------------------------------------------------------------
% Calculates and displays histograms of a color image.
% Each color channel (red, green, blue) is extracted and its histogram
calculated and displayed.
% Written by Image Analyst, Jan. 2010.

clc;
clear;
close all;
workspace;

% Change the current folder to the folder of this m-file.
% (The line of code below is from Brett Shoelson of The Mathworks.)
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

% Set up sizes for the captions and text we will show on the figure.
captionFontSize = 17; % Font size for headers/captions of images.
axisFontSize = 14; % Font size for axes of the histogram plots.

% Set the initial default directory to the one containing the
% standard demo images for the MATLAB Image Processing Toolbox.
folder = 'C:\Program Files\MATLAB\R2010a\toolbox\images\imdemos';
if ~exist(folder, 'dir')
folder = pwd;
end
filePattern = fullfile(folder, '*.*');

% Ask user to browse for the image file.
[baseFileName, folder] = uigetfile(filePattern, 'Specify an image
file');
% Construct the full file name.
fullImageFileName = fullfile(folder, baseFileName);


%------------------------------------
% Read the image into an array.
rgbImage = imread(fullImageFileName);
%------------------------------------


% Check to see if it is color or monochrome.
[rows columns numberOfColorChannels] = size(rgbImage);
if strcmpi(class(rgbImage), 'uint8')
% Flag for 256 gray levels.
eightBit = true;
else
eightBit = false;
end
% If it's monochrome, convert it to color.
if numberOfColorChannels == 1
rgbImage = cat(3, rgbImage, rgbImage, rgbImage);
end

% Display the original RGB image.
subplot(3,4,1);
imshow(rgbImage);
% Maximize figure.
set(gcf, 'Position', get(0, 'ScreenSize'));
% drawnow; % Make it display immediately.
if numberOfColorChannels > 1
title('Original Color Image', 'FontSize', captionFontSize);
else
title('Original Monochrome Image (converted to color)', 'FontSize',
captionFontSize);
end

% Extract out the individual color channels
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);

% Display the red channel image.
subplot(3,4,2);
imshow(redChannel);
title('Red Channel Image', 'FontSize', captionFontSize);
% Display the green channel image.
subplot(3,4,3);
imshow(greenChannel);
title('Green Channel Image', 'FontSize', captionFontSize);
% Display the blue channel image.
subplot(3,4,4);
imshow(blueChannel);
title('Blue Channel Image', 'FontSize', captionFontSize);

% Calculate the histogram of the red channel.
hR = subplot(3, 4, 6);
[countsR, grayLevelsR] = imhist(redChannel);
maxGLValueR = find(countsR > 0, 1, 'last');
maxCountR = max(countsR);
% Plot the histogram of the red channel.
bar(countsR, 'r');
grid on;
xlabel('Red Gray Levels', 'FontSize', axisFontSize);
ylabel('Red Pixel Count', 'FontSize', axisFontSize);
title('Histogram of Red Channel', 'FontSize', axisFontSize);

% Calculate the histogram of the green channel.
hG = subplot(3, 4, 7);
[countsG, grayLevelsG] = imhist(greenChannel);
maxGLValueG = find(countsG > 0, 1, 'last');
maxCountG = max(countsG);
% Plot the histogram of the green channel.
bar(countsG, 'g');
grid on;
xlabel('Green Gray Levels', 'FontSize', axisFontSize);
ylabel('Green Pixel Count', 'FontSize', axisFontSize);
title('Histogram of Green Channel', 'FontSize', axisFontSize);

% Calculate the histogram of the blue channel.
hB = subplot(3, 4, 8);
[countsB, grayLevelsB] = imhist(blueChannel);
maxGLValueB = find(countsB > 0, 1, 'last');
maxCountB = max(countsB);
% Plot the histogram of the blue channel.
bar(countsB, 'b');
grid on;
xlabel('Blue Gray Levels', 'FontSize', axisFontSize);
ylabel('Blue Pixel Count', 'FontSize', axisFontSize);
title('Histogram of Blue Channel', 'FontSize', axisFontSize);

% Set all axes to be the same height.
maxCount = max([maxCountR, maxCountG, maxCountB]);
% Set all axes to be the same width.
maxGL = max([maxGLValueR, maxGLValueG, maxGLValueB]);
if eightBit
maxGL = 255;
end
% If there's a big spike at the last bin, it can be hard to see
because
% of the box around the axes, so make the box a few bins wider.
% This will make it easier to see spikes in the last bin.
maxGL = maxGL + 8;
axis([hR hG hB], [0 maxGL 0 maxCount]);

% Plot all
subplot(3, 1, 3);
plot(grayLevelsR, countsR, 'r', 'LineWidth', 2);
grid on;
xlabel('Gray Level', 'FontSize', axisFontSize);
ylabel('Pixel Count', 'FontSize', axisFontSize);
hold on;
plot(grayLevelsG, countsG, 'g', 'LineWidth', 2);
plot(grayLevelsB, countsB, 'b', 'LineWidth', 2);
title('Histograms of All Channels', 'FontSize', captionFontSize);
maxGrayLevel = max([maxGLValueR, maxGLValueG, maxGLValueB]);
% Trim x-axis to just the max gray level on the bright end.
if eightBit
xlim([0 255]);
else
xlim([0 maxGrayLevel]);
end