MATLAB: Trouble using imhist in image

histogramimage processingimhist

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

Best Answer

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(:));