How to find the variance of an image in a special windows? variance(in 3*3 windows)

I obtain mean of image like this
if true
function [ I2] = mean3dar3( m )
h = fspecial('average', [3 3]);
I2 = imfilter(m,h);
end
I want to obtain variance of image in a special windows like mean but I dont know how can I make filter of this... thanks
end

Answers (3)

You could use the built-in function stdfilt() and square it.
grayImage = imread('cameraman.tif');
subplot(1,3,1);
imshow(grayImage);
fontSize = 20;
title('Original Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0, 1, 1]);
stdImage = stdfilt(grayImage, true(3));
subplot(1,3, 2);
imshow(stdImage, []);
title('Standard Deviation Image', 'FontSize', fontSize);
varianceImage = stdImage .^2;
subplot(1,3, 3);
imshow(varianceImage, []);
title('Variance Image', 'FontSize', fontSize);
Or you could also do it with blockproc() though it's not so straightforward. Let me know if you want a demo of that method.

2 Comments

thanks.
yes I think about my problem ...I don t want to have overlap.I need blockproc()...can you help me??
Here are my blocproc demos. I show it used in a variety of ways.

Sign in to comment.

You can use the function conv2:
A = imread('foo.bmp');
small_window = ones(3)/9;
A_sq = conv2(A.^2,small_window,'same');
A_m = conv2(A,small_window,'same');
Var = A_sq - A_m.^2;
thanks IMage Analyst and Alessandro Masullo
Excuse me, Is there any way to do cross correlation in a special windows,too?like stdfilt command??
cross correlation(in 3*3 windows)...

6 Comments

Cross correlation give an image. So at each pixel you will have a 5-by-5 image that is the cross correlation of the window with some other 3 by 3 window that you specify. You can do this with blockproc() but you will end up with an image 5 times as big in each dimension and it will look blocky. Is that what you want? I can give you a demo if you want.
thanks Image Analyst
I should think about this you said...I will com back soon
Dear Image Analyst I want to use blockproc and do cross correlation between each window and its neighbor windows in my image...for example if my image is like this:
in article that I read the writer write: 2dcross correlation: cross correlation between each window and its neighbor windows in the image...(window is 3*3) thanks
Was that really in a paper, word for word, verbatim? So for each 3x3 window, there are eight neighboring 3x3 windows. So that would result in eight 5x5 windows, because it's like I said, if you cross correlate two arrays you will get an image that is larger - it is the sum of the two sizes in each direction/dimension. You can look that up and find it in numerous places, I'm sure.What do they do with these eight 5-by-5 windows? Here, to illustrate, try this code:
a=rand(3,3);
b=rand(3,3);
c=xcorr2(a, b);
whos c;
In the command window:
Name Size Bytes Class Attributes
c 5x5 200 double
So for each window location, you're going to have 8 of those because you can have 8 3x3 neighbor windows.
you mean that input block 3*3 and output block is 5*5??
If you correlate a 3x3 window with another 3x3 window, yes, you will get a 5x5 window. You won't get a single number. And since with each 3x3 colored block you show in your image, there are 8 other 3x3 colored blocks surrounding it, you will have eight 5x5 matrices if you do what you said to do.

Sign in to comment.

Asked:

on 26 Jan 2015

Commented:

on 2 Feb 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!