Trouble using imhist in image
21 views (last 30 days)
Show older comments
Margarida Moura
on 24 Nov 2018
Commented: Image Analyst
on 24 Nov 2018
Hello! I want to see the histogram of my image, but I don't know why, it doesn't show.
Here's my code:
I=imread('D7.jpg');
imshow(I);
imhist(I);
The error is :" Error using imhist
Expected input number 1, I or X, to be two-dimensional.
Error in imhist>parse_inputs (line 278)
validateattributes(a,
{'double','uint8','int8','logical','uint16','int16','single','uint32',
'int32'}, ...
Error in imhist (line 60)
[a, n, isScaled, top, map] = parse_inputs(varargin{:});
Error in Process (line 3)
imhist(x);".
My image is 1024x995x3 uint8.
Thank you very much
0 Comments
Accepted Answer
Image Analyst
on 24 Nov 2018
Edited: Image Analyst
on 24 Nov 2018
You're passing in a color image and imhist() is not set up for that. If you want 3 histograms, extract each color channel independently,
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
like in the attached demo, and histogram each color channel one at a time. Or if you don't care which color channel, then just lump them all together with (:)
imhist(rgbImage(:));
2 Comments
Image Analyst
on 24 Nov 2018
It does work. In fact here is the code:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'index.jpg';
folder = pwd;
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
%=======================================================================================
% Read in demo image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 4, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original Color Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0.05 1 0.95]);
% 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')
drawnow;
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display the color channel images.
subplot(2, 4, 2);
imshow(redChannel);
caption = sprintf('Red Channel Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis on;
subplot(2, 4, 3);
imshow(greenChannel);
caption = sprintf('Green Channel Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis on;
subplot(2, 4, 4);
imshow(blueChannel);
caption = sprintf('Blue Channel Image');
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
impixelinfo;
axis on;
drawnow;
% See if all three channels are identical.
% If so, it's a gray scale image in a color image format.
subplot(2, 4, 5:8);
if isequal(redChannel, greenChannel) && isequal(redChannel, blueChannel)
% It's gray scale.
imhist(redChannel); % Just take any one of them
else
redCounts = imhist(redChannel);
greenCounts = imhist(greenChannel);
blueCounts = imhist(blueChannel);
gls = 0 : 255;
plot(gls, redCounts, 'r-', 'LineWidth', 2);
hold on;
plot(gls, greenCounts, 'g-', 'LineWidth', 2);
plot(gls, blueCounts, 'b-', 'LineWidth', 2);
xlabel('Gray Levels', 'FontSize', 16);
ylabel('Counts', 'FontSize', 16);
title('All 3 color histograms', 'FontSize', 16);
end
grid on;

The reason that the color histograms are different, is because you unwisely chose to save the image in JPEG format. I believe the image should really be grayscale. Don't use JPG format when doing image analysis.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!