Code covered by the BSD License

### Highlights fromColor Image Histogram

2.9

2.9 | 31 ratings Rate this file 139 Downloads (last 30 days) File Size: 1.23 KB File ID: #4875

# Color Image Histogram

05 May 2004 (Updated 08 Nov 2010)

Display a histogram showing the color balance of an image.

File Information
Description

RGBHIST displays a histogram of the overall color balance of an image.

Use the 'RGB' mode to get a quick sense of which colors are present in particular highlight, shadow, and midtone regions. The 'LAB' mode is more useful for determining the actual color balance of an image, but it is slower.

This function supports UINT8, UINT16, INT8, INT16, and DOUBLE images without colormaps.

Required Products Image Processing Toolbox
MATLAB release MATLAB 6.5 (R13)
Tags for This File
Everyone's Tags
Tags I've Applied
08 Apr 2013

hey, can anybody tel me that what '1/3' signifies in
'stem(1:256 + 1/3, gHist,'g'); '
statement

18 Jan 2013

thank u sir..it worked!

07 Dec 2012

solution to problem set(h, 'marker', 'none')

function varargout = rgbhist(I)
%RGBHIST Histogram of RGB values.

if (size(I, 3) ~= 3)
error('rgbhist:numberOfSamples', 'Input image must be RGB.')
end

nBins = 256;

rHist = imhist(I(:,:,1), nBins);
gHist = imhist(I(:,:,2), nBins);
bHist = imhist(I(:,:,3), nBins);

hFig = figure;
hold on
h(1) = stem(1:256, rHist,'r');
h(2) = stem(1:256 + 1/3, gHist,'g');
h(3) = stem(1:256 + 2/3, bHist),'b';

% I like more without "set" but also function whit "set"
%set(h, 'marker', 'none')
%set(h(1), 'color', [1 0 0])
%set(h(2), 'color', [0 1 0])
%set(h(3), 'color', [0 0 1])

01 Apr 2012

Nice work Jess !

30 Oct 2011

It works well with the modification of Jeff in 29 Apr 2011. Thank you!

29 Apr 2011

My modification on Jeff's initial code (including comment by Tomasz). Now input image & histogram derived are display in same figure window.

function rgbhist(I)
%RGBHIST Histogram of RGB values.

if (size(I, 3) ~= 3)
error('rgbhist:numberOfSamples', 'Input image must be RGB.')
end

nBins = 256;

rHist = imhist(I(:,:,1), nBins);
gHist = imhist(I(:,:,2), nBins);
bHist = imhist(I(:,:,3), nBins);

%hFig = figure;

figure
subplot(1,2,1);imshow(I)
subplot(1,2,2);

h(1) = stem(1:256, rHist); hold on
h(2) = stem(1:256 + 1/3, gHist);
h(3) = stem(1:256 + 2/3, bHist);
hold off

set(h, 'marker', 'none')
set(h(1), 'color', [1 0 0])
set(h(2), 'color', [0 1 0])
set(h(3), 'color', [0 0 1])
axis square

06 Dec 2010

Very nice

01 Sep 2010

stem(1:256, rHist,'r');hold on
stem(1:256 + 1/3, gHist,'g');
stem(1:256 + 2/3, bHist,'b');
hold off

24 Jul 2010

Excellent

04 Aug 2009

Respected Sir, ho to solve tis problem?
it gives the following error
??? Invalid handle object.

Error in ==> rgbhist at 19
set(h, 'marker', 'none'
thanks

04 Aug 2009

Respected Sir,
I am having error with your code.following is the error
??? Invalid handle object.

Error in ==> rgbhist at 19
set(h, 'marker', 'none')

Sir please tell how can i make histogram of color imag in matlab.
thanks

06 Oct 2008

Hello
In the above program how to get an image,is there any special syntax has to be used to get an image.
I used the syntax
But it's giving me error.
Is there any special syntax to get the RGB image.

22 Jun 2008

no comment

21 Mar 2008

good

02 Mar 2008
18 Feb 2008
19 Dec 2007

Sir,

when u convert rgb2hsi we get an hsi component image. Sir, can i treat the h matrix as an image

20 Nov 2007

please sir can you explain the steps of the program to me? is it an intersection of the histograms of all the colors in the image or what exactly?
i hope you can respond me or any one who understand this program can explain it to me
i will be very thankful

17 Nov 2007

hello

24 May 2007
31 Mar 2007
30 Mar 2007

Not working

02 Feb 2007
29 Nov 2006

Doesn't work.

25 Nov 2006

??? Invalid handle object.

Error in ==> rgbhist at 19

09 Nov 2006

it gives the following error
??? Invalid handle object.

Error in ==> rgbhist at 19
set(h, 'marker', 'none'

08 Nov 2006

Good one

19 Oct 2006

doesn't work

18 Sep 2006

??? Invalid handle object.

Error in ==> rgbhist at 19
set(h, 'marker', 'none')

31 Aug 2006

good

14 Aug 2006
20 Jul 2006

This doesn't work in R14/2006a (and I doubt it did in his version either), or at least it needs fixing. It produces a stem plot, not a plot like his screenshot shows. And the stem plot is only for the B channel because he forgot to issue a HOLD command after the line h(1) = stem(1:256, rHist); so the green blows away the red plot, and the blue blows away the green plot so that only the blue stem plot is there after all is said and done. Finally, there is nothing in this macro about different color spaces, RGB or LAB, no radio buttons to select between them, in fact, no user interface at all.

12 Jul 2006
21 Jun 2006
24 May 2006

log

12 Mar 2006

Does not work. Error: In an assignment A(I) = B, the number of elements in B and
I must be the same.

Must be something version-dependent

11 Mar 2006
29 Nov 2005
17 Oct 2005

Error: In an assignment A(I) = B, the number of elements in B and
I must be the same.

07 Jun 2005

??? In an assignment A(I) = B, the number of elements in B and
I must be the same.

27 Dec 2004

It does seem to give the following error" In an assignment A(I) = B, the number of elements in B and I must be the same" when trying to find the histogram of a 256x256 colour image

09 Dec 2004
09 Dec 2004
24 Jul 2004

asome

07 Jul 2004