Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to calculate variance of image histogram?

1,400 views
Skip to first unread message

Mohd Farhan

unread,
Oct 28, 2011, 2:55:30 AM10/28/11
to
HELLO;
I AM NEW IN MATLAB IMAGE PROCESSING. I AM STUCK IN PROGRAMMING OF WITH-IN CLASS VARIANCE OF HISTOGRAM!!

THE HISTOGRAM OF AN IMAGE IS GIVEN AS:
I = imread('camera.tif');
histogram = imhist(I);
bar(histogram,'BarWidth',1)
figure;
J = histeq(I);
imshow(J);
figure;
histogramEq = imhist(J);
bar(histogramEq,'BarWidth',1)

NOW HOW TO CALCULATE THE "WITH-IN VARIANCE" OR "VARIANCE" OF GIVEN HISTOGRAMS?
ITS VARIANCE IS GIVEN AS:
square of (s) = summation of {square of (graylevel - mean) x no. of pixels in its perticular greylevel} / (total pixels in an image).

refer: http://itl.nist.gov/div898/handbook/eda/section3/eda356.htm

PLEASE HELP ME IN THIS ISSUE. THANKS IN ADVANCE.

Mohd Farhan

unread,
Oct 29, 2011, 10:04:14 AM10/29/11
to
"Mohd Farhan " <farha...@gmail.com> wrote in message <j8djl2$704$1...@newscl01ah.mathworks.com>...
Please anyone help me..

Mohd Farhan

unread,
Oct 29, 2011, 10:05:20 AM10/29/11
to
"Mohd Farhan " <farha...@gmail.com> wrote in message <j8djl2$704$1...@newscl01ah.mathworks.com>...
Please anyone help me..

ImageAnalyst

unread,
Oct 29, 2011, 11:57:57 AM10/29/11
to
That's just the regular histogram. And I think you want the variance
of the image, not the variance of the histogram. So just do this
varianceOfImage = var(imageArray(:));

Mohd Farhan

unread,
Oct 30, 2011, 6:54:08 AM10/30/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <43d64d1a-dcc1-4121...@g21g2000yqc.googlegroups.com>...
> That's just the regular histogram. And I think you want the variance
> of the image, not the variance of the histogram. So just do this
> varianceOfImage = var(imageArray(:));
THANKS FOR YOUR REPLY.
var(imageArray(:)) GIVING GOOD RESULT WHEN IMAGE IS CONSIDERED AS WHOLE i.e. WHEN (THRESHOLD) T=0, BUT NOT FOR OTHER THRESHOLD VALUES.
LET SUPPOSE I HAD THIS 6x6 MATRIX:
refer: http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html
R =
0 0 1 4 4 5
0 1 3 4 3 4
1 3 4 2 1 3
4 4 3 1 0 0
5 4 2 1 0 0
5 5 4 3 1 0

NOW HOW TO CALCULATE THE VARIANCE OF BACKGROUND & FOREGROUND IMAGE?
For Different Threshold, Background & Foreground variance is coming out to be:
Threshold Background variance Foreground variance
T=0 0 3.1196
T=1 0 1.9639
T=2 0.2489 0.7755
T=3 0.4637 0.5152
T=4 1.4102 0.2130
T=5 2.5303 0

HOW TO CALCULATE THESE VARIANCES?

ImageAnalyst

unread,
Oct 30, 2011, 9:44:30 AM10/30/11
to
The web page you gave talked about the variance of the image and
showed a way to get it via the histogram. This, as I hope you know,
is entirely different than the variance of the histogram. Now, to get
the foreground and background you can do this:
foregroundMask = imageArray >= thresholdValue;
foregroundPixelsValues = imageArray(foregroundMask);
backgroundPixelsValues = imageArray(~foregroundMask);
then pass each of those into imhist() to get the histograms, and use
the formulas on your web page.

Mohd Farhan

unread,
Oct 31, 2011, 4:42:10 AM10/31/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <a384766e-4c26-4a7b...@ek5g2000vbb.googlegroups.com>...
THANKS FOR YOUR REPLY.

I DID WHAT YOU QUOTED. BUT ONE PROBLEM ARRIVES:
The O/P result is:
backgroundPixelsValues =
0
0
1
0
1
1
2
2
1
1
1
0
0
1
0
0
0

foregroundPixelsValues =
4
5
5
3
4
4
5
3
4
3
4
4
4
3
4
3
5
4
3

Histogram {hist} of this backgroundPixelsValues gives range from 1-3 & also same for foregroundPixelsValues!!
I used this program:
A = backgroundPixelsValues(:);
H= hist(A,2);
bar(H,'BarWidth',1)

& SAME FOR foregroundPixelsValues.

NOW WHEN I DID HISTOGRAM EQUALIZATION:
histeq(backgroundPixelsValues); it gives wrong result as compared to theoretical calculation!
===================================
As theoretical histogram-equalization calculation gives:
backgroundPixelsValues:
for 0 --> 0.9412 quantized to 1.
for 1 --> 1.7648 quantized to 2.
for 2 --> quantized to 2.
===================================
but the above MATLAB program gives:
for 0 --> 0.4762 quantized to 1.
for 1 --> quantized to 1.
for 2 --> quantized to 1. !!

& same problem for foregroundPixelsValues.

Mohd Farhan

unread,
Nov 1, 2011, 5:34:28 AM11/1/11
to
my above problem is almost solved. thanks for ur replies.

Mohd Farhan

unread,
Nov 1, 2011, 8:01:11 AM11/1/11
to
WHAT WE HAVE TO DO IF WE WANT TO DIVIDE HISTOGRAM IN MORE THAN 2 PARTS? LIKE 4,8 etc.

I TOOK THIS MATRIX:
R = [6 0 1 7 4 5; 0 7 3 4 3 4; 6 6 4 2 1 3; 4 4 3 1 6 0; 5 4 7 1 0 0; 5 7 4 3 1 0];

& APPLIED THIS FOR DIVIDING INTO THREE PARTS FROM 0-2, 3-4 & 5-7:
foregroundMask = R >= 5;
midgroundMask = 5 > R >= 2;
backgroundMask = 2 > R >= 0;
foregroundPixelsValues = R(foregroundMask);
midgroundPixelsValues = R(midgroundMask);
backgroundPixelsValues = R(backgroundMask);

BUT IT IS NOT WORKING.. PLEASE HELP ME..

ImageAnalyst

unread,
Nov 1, 2011, 5:01:42 PM11/1/11
to
On Nov 1, 8:01 am, "Mohd Farhan " <farhan7...@gmail.com> wrote:
> foregroundMask = R >= 5;
> midgroundMask = 5 > R >= 2;
> backgroundMask = 2 > R >= 0;
> foregroundPixelsValues = R(foregroundMask);
> midgroundPixelsValues = R(midgroundMask);
> backgroundPixelsValues = R(backgroundMask);
>
> BUT IT IS NOT WORKING.. PLEASE HELP ME..

--------------------------------------------------------------
You can't do stuff like 5 > R >= 2.
You need to do it this way instead.

foregroundMask = R >= 5
midgroundMask = (5 > R) & (R >= 2)
backgroundMask = R < 2

Mohd Farhan

unread,
Nov 2, 2011, 2:23:14 AM11/2/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <ac859295-c385-4cd6...@o19g2000vbk.googlegroups.com>...
REALLY REALLY THANKS.. IT SOLVED MY PROBLEM..

Mohd Farhan

unread,
Nov 3, 2011, 3:16:13 AM11/3/11
to
refer: http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html

in above web-page, there is the calculation of within-class variance when Threshold is considered.
At T=0, ?2W = 3.1196
At T=1, ?2W = 1.5268
At T=2, ?2W = 0.5561
At T=3, ?2W = 0.4909
At T=4, ?2W = 0.9779
At T=5, ?2W = 2.2491

HOW CAN I DO THE PROCESS FOR AN 256X256 GREY IMAGE ? i.e. I WANT TO PRINT:: {ALL THE THRESHOLD VALUES OF AN IMAGE, WITH ALL CALCULATED WITHIN-CLASS VARIANCE} SO THAT I CAN COMPARE THE RSULT.

my program are showing errors.. may be i am doing something wrong somewhere!

Mohd Farhan

unread,
Nov 3, 2011, 9:23:11 AM11/3/11
to
"Mohd Farhan " <farha...@gmail.com> wrote in message <j8tf3t$ddl$1...@newscl01ah.mathworks.com>...
Please help me. How can i calculate & print all the with-in class variances? so that i can see & compare it.

Mohd Farhan

unread,
Nov 3, 2011, 10:55:27 AM11/3/11
to
let a image of 256x256.
its complete histogram had 65536 pixels.
Now i divide its histogram into two parts. how to calculate the total pixels of two sub-histograms?

pixelCount only showing total pixels in an image! but i want to know the pixel count of sub-histograms. so that weight can be calculated.

ImageAnalyst

unread,
Nov 3, 2011, 7:42:03 PM11/3/11
to
----------------------------------------------------------
So you divided the histogram somehow into two sub-histograms? OK,
then to get the pixel count in each subhistogram you simply sum up the
counts in each subhistogram array:
pixelsInSubHistogram = sum(subHistPixelCounts(:))

Mohd Farhan

unread,
Nov 4, 2011, 9:00:20 AM11/4/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <a932bd34-c6b7-4e0d...@9g2000yqv.googlegroups.com>...
thanks i works.. i was doing one error before.

can you help me calculating minimum & maximum [& other if possible] within-class histogram variance of an 256x256 image.
here: http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html : 6 values are calculated but as image size increases its no. increases.
So please help me how can i calculate with-in class histogram variance of larger images.

please.

ImageAnalyst

unread,
Nov 4, 2011, 3:58:41 PM11/4/11
to
Well the page tells you so just follow the example:

% Calculate the histogram:
[pixelCounts GLs] = imhist(pixelValues);

% Calculate the mean:
meanGL = sum(pixelCounts .* GLs) / sum(pixelCounts);

% Subtract the mean from the gray levels:
m0 = pixelCounts - meanGL;

% Square that difference:
diffSquared = m0 .^ 2

% Multiply by the count in that histogram bin:
numerator = diffSquare .* pixelCounts;
denominator = sum(pixelCounts);

% Finally:
histVariance = numerator / denominator;

Of course you could make that more compact. I just expanded it out
and make it real explicit so that you'd be able to follow along
easily. In fact you could just do
histVariance = var(pixelValues(:));
and get the same answer, but you wanted the way to do it from a
histogram.

Mohd Farhan

unread,
Nov 5, 2011, 9:09:29 AM11/5/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <1c31528d-b211-4be9...@u28g2000yqb.googlegroups.com>...
This formula is for variance only.
But i want to calculate with-in class histogram variance.

With-in class histogram variance is calculated by dividing histogram in two grounds: foreground & background for every grey-level. i.e. for grey level image, we have 256 with-in class variances.

1st when threshold is took at 0 grey level (i.e. T=0) --> so only ground is generated.
2nd when threshold is took at 1 grey level (i.e. T=1) --> so two grounds are generated, foreground is the from (2 to 256) & back ground is from (0 to 1).
.
.
.
.
150th when threshold is took at 149th grey level (i.e. T=149) --> so two grounds are generated, foreground is the from (149 to 256) & back ground is from (0 to 148).
.
.
.
till 255th..

THIS IS WHAT I MEAN.
I WANT TO CALCULATE, ALL THE WITH-IN CLASS VARIANCES. SO THAT I CAN SEE & COMPARE WHICH THRESHOLD LEVEL GIVES MINIMUM & MAXIMUM WITH-IN CLASS VARIANCE.

i think now it is clear.

ImageAnalyst

unread,
Nov 5, 2011, 11:14:24 AM11/5/11
to
On Nov 5, 9:09 am, "Mohd Farhan " <farhan7...@gmail.com> wrote:
> THIS IS WHAT I MEAN.
> I WANT TO CALCULATE, ALL THE WITH-IN CLASS VARIANCES. SO THAT I CAN SEE & COMPARE WHICH THRESHOLD LEVEL GIVES MINIMUM & MAXIMUM WITH-IN CLASS VARIANCE.
>
> i think now it is clear.
------------------------------------------------------------------
I know. It was clear before already. That's why I gave you the
code. pixelValues is either the foreground or background pixels
depending on which class you're calculating the variance for. I think
it's clear with me (what you want), I'm not so sure about you. If you
don't understand why my code won't work, then write back and say why.

Mohd Farhan

unread,
Nov 5, 2011, 11:30:29 AM11/5/11
to
ImageAnalyst <imagea...@mailinator.com> wrote in message <349469fe-5090-4eae...@o19g2000vbk.googlegroups.com>...
Your formula works.. no doubt.. but that is for variance. not for WITHIN CLASS VARIANCE.

WITHIN CLASS VARIANCE have 256 different values for one gray-image,
but variance has only 1 value for one gray-image.

I am looking for that 256 values.
WITHIN CLASS VARIANCE = (weight of foreground histogram * variance of foreground histogram) + (weight of background histogram * variance of background histogram).

& these are 256 different values for grey-scale image.

refer: http://www.labbookpages.co.uk/software/imgProc/otsuThreshold.html

Mohd Farhan

unread,
Nov 5, 2011, 11:39:27 AM11/5/11
to
actually what i am in need is that, a loop type of thing which automatically sets a threshold value (from 0 to 255) [& therefore divides histogram in two grounds], & give us output value as WITH-IN CLASS VARIANCE. So that i can compare the result of minimum & maximum values.

Mohd Farhan

unread,
Nov 6, 2011, 1:07:13 AM11/6/11
to
please help me.
0 new messages