From: shilpa khinvasara on
can u pls tell let me know the code to display the histogram of HSV image.the image as u know is divided into 18,3,3, bins
i want o display the histogram of entire image and not the planes differently

thanks
From: Christopher on
"shilpa khinvasara" <ship(a)gamil.com> wrote in message <i0k1rj$9d2$1(a)fred.mathworks.com>...
> can u pls tell let me know the code to display the histogram of HSV image.the image as u know is divided into 18,3,3, bins
> i want o display the histogram of entire image and not the planes differently
>
> thanks

I don't believe there is any single function provided in either the standard library or the Image Processing Toolbox that will do this for you. Therefore, you will have to create your own function to do it.

Do you already have the histogram or do you need to create it? Is the image 3-dimensional? (i.e. NxMx3 ?)

Chris
From: ImageAnalyst on
On Jul 2, 2:44 am, "shilpa khinvasara" <s...(a)gamil.com> wrote:
> can u pls tell let me know the code to display the histogram of HSV image..the image as u know is divided into 18,3,3, bins
> i want o display the histogram of entire image and not the planes differently
>
> thanks

----------------------------------------------
I have demo code for this - I'll post it here later today.
-ImageAnalyst
From: ImageAnalyst on
OK, here it is. I haven't posted this on the File Exchange (yet) so
you'll have to fix up some lines that the newsreader will split into
two lines.

function rgb2hsv_Demo()

% 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
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;

% Read in standard MATLAB color demo images.
imagesFolder = 'C:\Program Files\MATLAB\R2010a\toolbox\images
\imdemos';
if ~exist(imagesFolder, 'dir')
message = sprintf('Please browse to your image folder');
button = questdlg(message, 'Specify Folder', 'OK', 'Cancel', 'OK');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'Cancel')
return;
else
imagesFolder = uigetdir();
if imagesFolder == 0
return;
end
end
end

% Read the directory to get a list of images.
filePattern = [imagesFolder, '\*.jpg'];
jpegFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.tif'];
tifFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.png'];
pngFiles = dir(filePattern);
filePattern = [imagesFolder, '\*.bmp'];
bmpFiles = dir(filePattern);
imageFiles = [jpegFiles; tifFiles; pngFiles; bmpFiles];

% Bail out if there aren't any images in that folder.
numberOfImagesProcessed = 0;
numberOfImagesToProcess = length(imageFiles);
if numberOfImagesToProcess <= 0
message = sprintf('I did not find any JPG, TIF, PNG, or BMP images
in the folder\n%s\nClick OK to Exit.', imagesFolder);
uiwait(msgbox(message));
return;
end

% Create a figure for our images.
figure;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.

% Preallocate arrays to hold the means of all the images.
hImage_Mean = zeros(numberOfImagesToProcess, 1);
sImage_Mean = zeros(numberOfImagesToProcess, 1);
vImage_Mean = zeros(numberOfImagesToProcess, 1);
% Loop though all images, converting to hsv
% and then getting the means of the h, s, and v channels.
for k = 1 : numberOfImagesToProcess
% Read in this one file.
baseFileName = imageFiles(k).name;
fullFileName = fullfile(imagesFolder, baseFileName);
rgbImage = imread(fullFileName);
[rows columns numberOfColorBands] = size(rgbImage);
if numberOfColorBands <= 1
% Skip monochrome or indexed images.
continue;
end
subplot(3, 3, 1);
imshow(rgbImage, []);
caption = sprintf('Original Color Image\n%s', baseFileName);
% If there are underlines in the name, title() converts the next
character to a subscript.
% To avoid this, replace underlines by spaces.
caption = strrep(caption, '_', ' ');
title(caption, 'FontSize', fontSize);

% Convert to floating point so it does the calculations correctly.
% Also needs to be normalized to 0-1.
rgbFloating = double(rgbImage) / 255.0;

% Compute hsv image
hsvImage = rgb2hsv(rgbFloating);
% H image:
hImage = hsvImage(:,:,1);
subplot(3, 3, 4);
imshow(hImage, []); % Display the image.
% Compute mean
hImage_Mean(k) = mean(hImage(:));
caption = sprintf('Hue Image. Mean = %6.2f', hImage_Mean(k));
title(caption, 'FontSize', fontSize);
% Compute and display the histogram for the H image.
histogramDouble(hImage, 7, 'Histogram of Hue Image');

% S image:
sImage = hsvImage(:,:,2);
subplot(3, 3, 5);
imshow(sImage, []); % Display the image.
% Compute mean
sImage_Mean(k) = mean(sImage(:));
caption = sprintf('Saturation Image. Mean = %6.2f', sImage_Mean(k));
title(caption, 'FontSize', fontSize);
% Compute and display the histogram for the S image.
histogramDouble(sImage, 8, 'Histogram of Saturation Image');

% V image:
vImage = hsvImage(:,:,3);
subplot(3, 3, 6);
imshow(vImage, []); % Display the image.
numberOfImagesProcessed = numberOfImagesProcessed + 1;
% Compute mean
vImage_Mean(k) = mean(vImage(:));
caption = sprintf('Value Image. Mean = %6.2f', vImage_Mean(k));
title(caption, 'FontSize', fontSize);
% Compute and display the histogram for the V image.
histogramDouble(vImage, 9, 'Histogram of Value Image');

% Prompt user to continue.
promptMessage = sprintf('Currently displaying image #%d of %d:\n%s\n
\nDo you want to\nContinue processing, or\nCancel processing?',...
numberOfImagesProcessed, numberOfImagesToProcess, baseFileName);
button = questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel',
'Continue');
if strcmp(button, 'Cancel')
break;
end

end
% Crop off any unassigned values:
hImage_Mean = hImage_Mean(1:numberOfImagesProcessed);
sImage_Mean = sImage_Mean(1:numberOfImagesProcessed);
vImage_Mean = vImage_Mean(1:numberOfImagesProcessed);

% Print to command window
fprintf(1, ' Filename, H Mean, S Mean, V Mean\n');
for k = 1 : length(hImage_Mean)
baseFileName = imageFiles(k).name;
fprintf(1, '%24s %6.2f, %6.2f, %6.2f\n', ...
baseFileName, hImage_Mean(k), sImage_Mean(k), vImage_Mean(k));
end

if numberOfImagesProcessed == 1
caption = sprintf('Done with demo!\n\nProcessed 1 image.\nCheck out
the command window for the results');
else
caption = sprintf('Done with demo!\n\nProcessed %d images.\nCheck out
the command window for the results', numberOfImagesProcessed);
end
msgbox(caption);



function histogramDouble(dblImage, subplotNumber, caption)
% So now we have a double image that is our "starting image."
% However can't use imhist on this. We need to scale to 0-1.
minValue = min(min(dblImage));
maxValue = max(max(dblImage));
range = maxValue - minValue;
dblImage = (dblImage - minValue) / range;
% Check to verify that range is now 0-1.
% minValueNorm = min(min(dblImage));
% maxValueNorm = max(max(dblImage));

% Let's get its histogram into 256 bins.
[pixelCount grayLevels] = imhist(dblImage, 256);

% Let's suppress the zero bin because it's always so high.
pixelCount(1) = 0;

% But now grayLevelsD goes from 0 to 1.
% We want it to go from the original range, so we need to scale.
originalDoubleGrayLevels = range * grayLevels + minValue;

subplot(3, 3, subplotNumber);
plot(originalDoubleGrayLevels, pixelCount);
title(caption, 'FontSize', 16);
% Scale x axis manually.
xlim([originalDoubleGrayLevels(1) originalDoubleGrayLevels(end)]);
return;

From: shilpa khinvasara on
sir,
but this code will display hue,saturation and value histograms seprately
can't i have a single histogram


shilpa






ImageAnalyst <imageanalyst(a)mailinator.com> wrote in message <cf3bb4bf-55a1-4a03-804f-dc498a220a06(a)e30g2000vbl.googlegroups.com>...
> OK, here it is. I haven't posted this on the File Exchange (yet) so
> you'll have to fix up some lines that the newsreader will split into
> two lines.
>
> function rgb2hsv_Demo()
>
> % 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
> clc; % Clear command window.
> clear; % Delete all variables.
> close all; % Close all figure windows except those created by imtool.
> imtool close all; % Close all figure windows created by imtool.
> workspace; % Make sure the workspace panel is showing.
> fontSize = 20;
>
> % Read in standard MATLAB color demo images.
> imagesFolder = 'C:\Program Files\MATLAB\R2010a\toolbox\images
> \imdemos';
> if ~exist(imagesFolder, 'dir')
> message = sprintf('Please browse to your image folder');
> button = questdlg(message, 'Specify Folder', 'OK', 'Cancel', 'OK');
> drawnow; % Refresh screen to get rid of dialog box remnants.
> if strcmpi(button, 'Cancel')
> return;
> else
> imagesFolder = uigetdir();
> if imagesFolder == 0
> return;
> end
> end
> end
>
> % Read the directory to get a list of images.
> filePattern = [imagesFolder, '\*.jpg'];
> jpegFiles = dir(filePattern);
> filePattern = [imagesFolder, '\*.tif'];
> tifFiles = dir(filePattern);
> filePattern = [imagesFolder, '\*.png'];
> pngFiles = dir(filePattern);
> filePattern = [imagesFolder, '\*.bmp'];
> bmpFiles = dir(filePattern);
> imageFiles = [jpegFiles; tifFiles; pngFiles; bmpFiles];
>
> % Bail out if there aren't any images in that folder.
> numberOfImagesProcessed = 0;
> numberOfImagesToProcess = length(imageFiles);
> if numberOfImagesToProcess <= 0
> message = sprintf('I did not find any JPG, TIF, PNG, or BMP images
> in the folder\n%s\nClick OK to Exit.', imagesFolder);
> uiwait(msgbox(message));
> return;
> end
>
> % Create a figure for our images.
> figure;
> set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
>
> % Preallocate arrays to hold the means of all the images.
> hImage_Mean = zeros(numberOfImagesToProcess, 1);
> sImage_Mean = zeros(numberOfImagesToProcess, 1);
> vImage_Mean = zeros(numberOfImagesToProcess, 1);
> % Loop though all images, converting to hsv
> % and then getting the means of the h, s, and v channels.
> for k = 1 : numberOfImagesToProcess
> % Read in this one file.
> baseFileName = imageFiles(k).name;
> fullFileName = fullfile(imagesFolder, baseFileName);
> rgbImage = imread(fullFileName);
> [rows columns numberOfColorBands] = size(rgbImage);
> if numberOfColorBands <= 1
> % Skip monochrome or indexed images.
> continue;
> end
> subplot(3, 3, 1);
> imshow(rgbImage, []);
> caption = sprintf('Original Color Image\n%s', baseFileName);
> % If there are underlines in the name, title() converts the next
> character to a subscript.
> % To avoid this, replace underlines by spaces.
> caption = strrep(caption, '_', ' ');
> title(caption, 'FontSize', fontSize);
>
> % Convert to floating point so it does the calculations correctly.
> % Also needs to be normalized to 0-1.
> rgbFloating = double(rgbImage) / 255.0;
>
> % Compute hsv image
> hsvImage = rgb2hsv(rgbFloating);
> % H image:
> hImage = hsvImage(:,:,1);
> subplot(3, 3, 4);
> imshow(hImage, []); % Display the image.
> % Compute mean
> hImage_Mean(k) = mean(hImage(:));
> caption = sprintf('Hue Image. Mean = %6.2f', hImage_Mean(k));
> title(caption, 'FontSize', fontSize);
> % Compute and display the histogram for the H image.
> histogramDouble(hImage, 7, 'Histogram of Hue Image');
>
> % S image:
> sImage = hsvImage(:,:,2);
> subplot(3, 3, 5);
> imshow(sImage, []); % Display the image.
> % Compute mean
> sImage_Mean(k) = mean(sImage(:));
> caption = sprintf('Saturation Image. Mean = %6.2f', sImage_Mean(k));
> title(caption, 'FontSize', fontSize);
> % Compute and display the histogram for the S image.
> histogramDouble(sImage, 8, 'Histogram of Saturation Image');
>
> % V image:
> vImage = hsvImage(:,:,3);
> subplot(3, 3, 6);
> imshow(vImage, []); % Display the image.
> numberOfImagesProcessed = numberOfImagesProcessed + 1;
> % Compute mean
> vImage_Mean(k) = mean(vImage(:));
> caption = sprintf('Value Image. Mean = %6.2f', vImage_Mean(k));
> title(caption, 'FontSize', fontSize);
> % Compute and display the histogram for the V image.
> histogramDouble(vImage, 9, 'Histogram of Value Image');
>
> % Prompt user to continue.
> promptMessage = sprintf('Currently displaying image #%d of %d:\n%s\n
> \nDo you want to\nContinue processing, or\nCancel processing?',...
> numberOfImagesProcessed, numberOfImagesToProcess, baseFileName);
> button = questdlg(promptMessage, 'Continue?', 'Continue', 'Cancel',
> 'Continue');
> if strcmp(button, 'Cancel')
> break;
> end
>
> end
> % Crop off any unassigned values:
> hImage_Mean = hImage_Mean(1:numberOfImagesProcessed);
> sImage_Mean = sImage_Mean(1:numberOfImagesProcessed);
> vImage_Mean = vImage_Mean(1:numberOfImagesProcessed);
>
> % Print to command window
> fprintf(1, ' Filename, H Mean, S Mean, V Mean\n');
> for k = 1 : length(hImage_Mean)
> baseFileName = imageFiles(k).name;
> fprintf(1, '%24s %6.2f, %6.2f, %6.2f\n', ...
> baseFileName, hImage_Mean(k), sImage_Mean(k), vImage_Mean(k));
> end
>
> if numberOfImagesProcessed == 1
> caption = sprintf('Done with demo!\n\nProcessed 1 image.\nCheck out
> the command window for the results');
> else
> caption = sprintf('Done with demo!\n\nProcessed %d images.\nCheck out
> the command window for the results', numberOfImagesProcessed);
> end
> msgbox(caption);
>
>
>
> function histogramDouble(dblImage, subplotNumber, caption)
> % So now we have a double image that is our "starting image."
> % However can't use imhist on this. We need to scale to 0-1.
> minValue = min(min(dblImage));
> maxValue = max(max(dblImage));
> range = maxValue - minValue;
> dblImage = (dblImage - minValue) / range;
> % Check to verify that range is now 0-1.
> % minValueNorm = min(min(dblImage));
> % maxValueNorm = max(max(dblImage));
>
> % Let's get its histogram into 256 bins.
> [pixelCount grayLevels] = imhist(dblImage, 256);
>
> % Let's suppress the zero bin because it's always so high.
> pixelCount(1) = 0;
>
> % But now grayLevelsD goes from 0 to 1.
> % We want it to go from the original range, so we need to scale.
> originalDoubleGrayLevels = range * grayLevels + minValue;
>
> subplot(3, 3, subplotNumber);
> plot(originalDoubleGrayLevels, pixelCount);
> title(caption, 'FontSize', 16);
> % Scale x axis manually.
> xlim([originalDoubleGrayLevels(1) originalDoubleGrayLevels(end)]);
> return;