Code covered by the BSD License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

### Highlights from Color Image Histogram

2.93548
2.9 | 32 ratings Rate this file 22 Downloads (last 30 days) File Size: 1.23 KB File ID: #4875 Version: 1.1

# Color Image Histogram

### Jeff Mather (view profile)

05 May 2004 (Updated )

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   Please login to tag files.
Comments and Ratings (50)
12 Apr 2016 Hua

### Hua (view profile)

Good solution from John Diaz, thanks Jeff Mather for your contribution too!

07 Jun 2015 fajar anggara

### fajar anggara (view profile)

please anyone can explain me with the following code
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';
my question is why do we must add 1/3 and 2/3 respectively in h(2),h(3)?.

thank you very much

Comment only
26 May 2014 Saleha Samad

### Saleha Samad (view profile)

i want to extract the value that is maximum for a color pixel of an object , how can i get that ???

Comment only
30 Jul 2013 Yong

### Yong (view profile)

I test that h(1) = stem(1:256, rHist); hold on
h(2) = stem(1:256 , gHist);
h(3) = stem(1:256 , bHist);
hold off can achieve the same result. so it need not to use h(2) = stem(1:256 + 1/3, gHist,'g');
h(3) = stem(1:256 + 2/3, bHist),'b';
and we can use this command result = (1:256)==(1:256 + 1/3) to test, and it is just the same

Comment only
14 Jun 2013 shashi

### shashi (view profile)

How Can I read the Color of an Image with Some Pixel Values ?

I want to change the color of that particular Pixel color to another Color ?

Comment only
08 Apr 2013 tamanna

### tamanna (view profile)

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

Comment only
18 Jan 2013 nsit

### nsit (view profile)

thank u sir..it worked!

Comment only
07 Dec 2012 John Diaz

### John Diaz (view profile)

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])

Comment only
01 Apr 2012 adam

### adam (view profile)

Nice work Jess !

Comment only
30 Oct 2011 Dongni Zhang

### Dongni Zhang (view profile)

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

Comment only
29 Apr 2011 Jess

### Jess (view profile)

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 Ronny Meza

### Ronny Meza (view profile)

Very nice

01 Sep 2010 Tomasz

### Tomasz (view profile)

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

Comment only
24 Jul 2010 Muhammad Nawaz

### Muhammad Nawaz (view profile)

Excellent

Comment only
04 Aug 2009 muhammad ashraf

### muhammad ashraf (view profile)

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

Error in ==> rgbhist at 19
set(h, 'marker', 'none'
please help me how can i make color imag histogram?
thanks

04 Aug 2009 muhammad ashraf

### muhammad ashraf (view profile)

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

Comment only
06 Oct 2008 Sakthi Kandan

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 mustafa omar

no comment

Comment only
21 Mar 2008 papu champu

good

02 Mar 2008 esraa ahmed
18 Feb 2008 keshamoni Divya
19 Dec 2007 santosh saraf

Sir,

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

20 Nov 2007 hanan yaseen

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

Comment only
17 Nov 2007 kubilay i?ci

hello

Comment only
24 May 2007 Joao Silva
31 Mar 2007 Divya Seelan
30 Mar 2007 Bruce Banner

Not working

02 Feb 2007 Mourad Oussalah
29 Nov 2006 KAsper HAnsen

Doesn't work.

25 Nov 2006 dolly jain

??? Invalid handle object.

Error in ==> rgbhist at 19

Comment only
09 Nov 2006 ayman nassar

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

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

08 Nov 2006 Syed Murtaza Hussain

Good one

19 Oct 2006 angry sam

doesn't work

18 Sep 2006 Kevin Ostheimer

??? Invalid handle object.

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

Comment only
31 Aug 2006 muthu krishnan

good

14 Aug 2006 gabrielle tuijthof
20 Jul 2006 Mark Hayworth

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 mohammed Alnafea
21 Jun 2006 Jong-Cheon park
24 May 2006 eman mehrabadi

log

Comment only
12 Mar 2006 Maria Gorlatova

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 Lei sheng
29 Nov 2005 hanif mohammadi
17 Oct 2005 Arjan Gijsenij

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

07 Jun 2005 Lahmar karim

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

27 Dec 2004 visesh Jeetoo

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 reza saffari
09 Dec 2004 reza saffari
24 Jul 2004 feng Yang

asome

07 Jul 2004 valla dafrel