user1268559
user1268559

Reputation: 223

Variance and Mean of Image

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

Upvotes: 6

Views: 44624

Answers (2)

Egon
Egon

Reputation: 4787

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

Upvotes: 12

yuk
yuk

Reputation: 19870

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.

Upvotes: 5

Related Questions