Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to calculate variance of image histogram?

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 28 Oct, 2011 06:55:30

Message: 1 of 22

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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 29 Oct, 2011 14:04:14

Message: 2 of 22

"Mohd Farhan " <farhan7787@gmail.com> wrote in message <j8djl2$704$1@newscl01ah.mathworks.com>...
> 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.

Please anyone help me..

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 29 Oct, 2011 14:05:20

Message: 3 of 22

"Mohd Farhan " <farhan7787@gmail.com> wrote in message <j8djl2$704$1@newscl01ah.mathworks.com>...
> 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.

Please anyone help me..

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 29 Oct, 2011 15:57:57

Message: 4 of 22

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(:));

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 30 Oct, 2011 10:54:08

Message: 5 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <43d64d1a-dcc1-4121-8767-8960f88c885d@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?

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 30 Oct, 2011 13:44:30

Message: 6 of 22

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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 31 Oct, 2011 08:42:10

Message: 7 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a384766e-4c26-4a7b-9961-fdb73343b90a@ek5g2000vbb.googlegroups.com>...
> 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.
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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 1 Nov, 2011 09:34:28

Message: 8 of 22

my above problem is almost solved. thanks for ur replies.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 1 Nov, 2011 12:01:11

Message: 9 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a384766e-4c26-4a7b-9961-fdb73343b90a@ek5g2000vbb.googlegroups.com>...
> 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.

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..

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 1 Nov, 2011 21:01:42

Message: 10 of 22

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

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 2 Nov, 2011 06:23:14

Message: 11 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <ac859295-c385-4cd6-a47d-6463e7b9bed5@o19g2000vbk.googlegroups.com>...
> 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

REALLY REALLY THANKS.. IT SOLVED MY PROBLEM..

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 3 Nov, 2011 07:16:13

Message: 12 of 22

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!

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 3 Nov, 2011 13:23:11

Message: 13 of 22

"Mohd Farhan " <farhan7787@gmail.com> wrote in message <j8tf3t$ddl$1@newscl01ah.mathworks.com>...
> 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!

Please help me. How can i calculate & print all the with-in class variances? so that i can see & compare it.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 3 Nov, 2011 14:55:27

Message: 14 of 22

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.

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 3 Nov, 2011 23:42:03

Message: 15 of 22

On Nov 3, 10:55 am, "Mohd Farhan " <farhan7...@gmail.com> wrote:
> 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.

----------------------------------------------------------
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(:))

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 4 Nov, 2011 13:00:20

Message: 16 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <a932bd34-c6b7-4e0d-bb59-07509063d0bd@9g2000yqv.googlegroups.com>...
> On Nov 3, 10:55 am, "Mohd Farhan " <farhan7...@gmail.com> wrote:
> > 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.
>
> ----------------------------------------------------------
> 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(:))
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.

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 4 Nov, 2011 19:58:41

Message: 17 of 22

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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 5 Nov, 2011 13:09:29

Message: 18 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <1c31528d-b211-4be9-bfbd-ce6b8e1f5fc4@u28g2000yqb.googlegroups.com>...
> 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.
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.

Subject: how to calculate variance of image histogram?

From: ImageAnalyst

Date: 5 Nov, 2011 15:14:24

Message: 19 of 22

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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 5 Nov, 2011 15:30:29

Message: 20 of 22

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <349469fe-5090-4eae-8137-8696ec1101bd@o19g2000vbk.googlegroups.com>...
> 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.

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

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 5 Nov, 2011 15:39:27

Message: 21 of 22

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.

Subject: how to calculate variance of image histogram?

From: Mohd Farhan

Date: 6 Nov, 2011 05:07:13

Message: 22 of 22

please help me.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us