Simplicity
Simplicity

Reputation: 48916

Matlab - 8x8 window and finding mean

Say I have a matrix of an image, and I want to do the following:

How can I do that in matlab, provided that I'm kind of new to coding in matlab.

Thanks.

Upvotes: 2

Views: 1305

Answers (3)

bla
bla

Reputation: 26069

You can also use nlfilter :

fun = @(x) mean(x(:)); 
ans= nlfilter(img,[8 8],fun);

But as @s.bandara suggested, the conv2 is much faster for just calculating the mean... Note that the matrix size will change when using the conv2 with valid.

enter image description here

nlfilter Elapsed time is 0.433989 seconds.

conv2 Elapsed time is 0.000803 seconds.

So it is pretty obvious that for the task of finding the mean, conv2 is much much faster.

Upvotes: 4

s.bandara
s.bandara

Reputation: 5664

You could use conv2 with a ones(8) filter, as in I2 = conv2(I, 1.0 / 64.0 * ones(8), 'valid');. We divide by 64.0 because the "filter" isn't normalized.

Upvotes: 4

Mark
Mark

Reputation: 8431

try to extract first the sub-matrices of your image like in here: MATLAB Submatrix

then use the mean(A) function for each submatrices

Upvotes: 0

Related Questions