How to show histogram of RGB image in Matlab?

E_learner picture E_learner · Feb 4, 2013 · Viewed 69.1k times · Source

I read an image in matlab using

input = imread ('sample.jpeg');

Then I do

imhist(input);

It gives this error:

??? Error using ==> iptcheckinput
Function IMHIST expected its first input, I or X, to be two-dimensional.

Error in ==> imhist>parse_inputs at 275
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...

Error in ==> imhist at 57
[a, n, isScaled, top, map] = parse_inputs(varargin{:});

After running size(input), I see my input image is of size 300x200x3. I know the third dimension is for color channel, but is there any way to show histogram of this? Thanks.

Answer

bla picture bla · Feb 4, 2013

imhist displays a histogram of a grayscale or binary images. Use rgb2gray on the image, or use imhist(input(:,:,1)) to see one of the channel at a time (red in this example).

Alternatively you can do this:

hist(reshape(input,[],3),1:max(input(:))); 
colormap([1 0 0; 0 1 0; 0 0 1]);

to show the 3 channels simultaneously...