calculation of mean , standard deviation and average for each pixel in an image M*N?
Show older comments
if i have an image M*N
if i want to making each pixel in the center of window with 31*31 size , and then compute the average and standard deviation and the mean based on pixel 's nighborhoods ? how this can be done?
i dont mean computing the mean , standard deviation , and average for the whole image?
i wait any help
Answers (2)
Teja Muppirala
on 23 Jan 2012
1 vote
If you have the Image Processing Toolbox, the functions
IMFILTER (can be used to do mean filtering on a neighborhood),
STDFILT (standard deviation on a neighborhood),
or the more general NLFILTER (arbitrary function on a neighborhood),
are there to do these types of operations quite easily.
David Young
on 22 Jan 2012
To get the means, you can do
means = conv2(Image, ones(31)/(31*31), 'valid');
Note that means(1,1) is the mean for the window centred on Image(16,16), and so on.
I'm not sure how the average is different from the mean.
To get the SDs, just extend the technique above in the obvious way, using the standard formula for the SD.
10 Comments
mmm ssss
on 22 Jan 2012
mmm ssss
on 22 Jan 2012
David Young
on 22 Jan 2012
I use that code because it does what you want. It does not compute the same thing as mean2(Image), as you will see if you try it.
mmm ssss
on 22 Jan 2012
Image Analyst
on 22 Jan 2012
He told you. There are no "pointers" just indexing. To find the means around (10,25) just index into the means array David created for you
meanValueAt1025 = means(10,25);
mean2() does the mean of the whole image whereas conv2 will give you an image with each pixel being the mean within a 31x31 window centered around the pixel. Those are different things.
mmm ssss
on 22 Jan 2012
Walter Roberson
on 22 Jan 2012
Create a loop to calculate the threshold if you want to, but you can simplify it as a matrix operation,
Thesh = meanArray + k * stdArray;
David's code is an example of making a single call that calculates everything at once instead of having to code loops to do it pixel by pixel. You can code it pixel by pixel if you feel more confident about that.
David Young
on 22 Jan 2012
If something is not clear, it can help to think about a small example. Suppose your image is 5x5, and the window size is 3x3 (instead of 31x31). Now look at this:
Image = magic(5)
means = conv2(Image, ones(3)/(3*3), 'valid')
Look at the numbers printed out and you can see that means(1,1) is the mean of the top-left 3x3 corner of Image, means(2,1) is the mean of the block one pixel below that, means(1,2) is the mean of the block one pixel to the right, and so on. So it's computed the mean of each 3x3 window. Isn't that what you want, scaled up to 31x31?
David Young
on 22 Jan 2012
It's true that I haven't told you how to compute the neighbourhood SDs, but once you understand how the neighbourhood means are computed I am sure you'll be able to see how to get the SDs - you just need to apply the same technique to the pointwise square of the image and look up the formula for the SD.
David Young
on 25 Jan 2012
... or, as Teja Muppirala points out, there's also stdfilt.
Categories
Find more on Image Transforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!