Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variance and Mean of Image

Tags:

matlab

I am calculating mean and variance of my original and stego image to compare them I am using grayscale BMP image for comaprison

image=imread("image name")
M = mean(image(:))
V = var((image(:)))

Is this is correct way fo calculating mean/var in MATLAB? My Variance is getting more than mean..

Any help appreciated..

like image 684
user1268559 Avatar asked Mar 30 '12 06:03

user1268559


People also ask

How do you find the variance of an image?

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

How do you find the mean of an image?

Mean value is the sum of pixel values divided by the total number of pixel values. Pixel Values Each of the pixels that represents an image stored inside a computer has a pixel value which describes how bright that pixel is, and/or what color it should be.

What is meant by mean and variance?

Mean and variance is a measure of central dispersion. Mean is the average of given set of numbers. The average of the squared difference from the mean is the variance. Central dispersion tells us how the data that we are taking for observation are scattered and distributed.

How do you find the mean and STD of an image?

mean: simply divide the sum of pixel values by the total count - number of pixels in the dataset computed as len(df) * image_size * image_size. standard deviation: use the following equation: total_std = sqrt(psum_sq / count - total_mean ** 2)


2 Answers

These are indeed the correct way to calculate the mean and variance over all the pixels of your image.

It is not impossible that your variance is larger than the mean as both are defined in the following way:

mean     = sum(x)/length(x)
variance = sum((x - mean(x)).^2)/(length(x) - 1);

For example, if you generate noise from a standard normal distribution with randn(N,1), you will get N samples, and if you calculate the mean and variance, you will get approximately 0 and 1. So there as well, your variance may well be larger than the mean.

Both have a totally different meaning: the mean gives you an idea where your pixels are (i.e. are they white, black, 50% gray, ...). The mean will give you an idea of what pixel color to choose to summarize the color of the complete image. The variance gives you an idea how the pixel values are spread: e.g. if your mean pixel value is 50% gray, are most of the other pixels also 50% gray (small variance) or do you have 50 black pixels and 50 white pixels (large variance)? So you could also view it as a way to get an idea how well the mean summarizes the image (i.e. with zero variance, most of the information is captured by the mean).

edit: For the RMS value (Root Mean Square) of a signal, just do what the definition says. In most cases you want to remove the DC component (i.e. the mean) before calculating the RMS value.

edit 2: What I forgot to mention was: it also makes little sense to compare the numerical value of the variance with the mean from a physical point of view. The mean has the same dimension as your data (in case of pixels, think of intensity), while the variance has the dimension of your data squared (so intensity^2). The standard deviation (std in MATLAB), which is the square root of the variance on the other hand has the same dimension as the data, so there you could make some comparisons (it is another question whether you should do such comparison).

like image 84
Egon Avatar answered Sep 29 '22 12:09

Egon


If you are workign with RGB image (H x W x 3), you have to calculate mean and variance separately for each channel. In this case the mean pixel will also be 3-values vector.

for ch = 1:3
   M(ch) = mean(reshape(img(:,:,ch),[],1));
   V(ch) = var(reshape(img(:,:,ch),[],1));
end

MATLAB has function image. Avoid using it as a variable.

like image 28
yuk Avatar answered Sep 29 '22 12:09

yuk