File Exchange

## Color Image Histogram

version 1.1 (1.23 KB) by

Display a histogram showing the color balance of an image.

2.93548
32 Ratings

Updated

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.

Hua

### Hua (view profile)

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

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

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

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

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 ?

tamanna

### tamanna (view profile)

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

nsit

### nsit (view profile)

thank u sir..it worked!

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

Nice work Jess !

Dongni Zhang

### Dongni Zhang (view profile)

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

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

Ronny Meza

Very nice

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

Excellent

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

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

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.

mustafa omar

no comment

papu champu

good

esraa ahmed

keshamoni Divya

santosh saraf

Sir,

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

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

kubilay i?ci

hello

Joao Silva

Divya Seelan

Bruce Banner

Not working

KAsper HAnsen

Doesn't work.

dolly jain

??? Invalid handle object.

Error in ==> rgbhist at 19

ayman nassar

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

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

Syed Murtaza Hussain

Good one

angry sam

doesn't work

Kevin Ostheimer

??? Invalid handle object.

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

muthu krishnan

good

gabrielle tuijthof

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.

mohammed Alnafea

Jong-Cheon park

log

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

Lei sheng

Arjan Gijsenij

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

Lahmar karim

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

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

reza saffari

reza saffari

feng Yang

asome

valla dafrel