Matlab - 8x8 window and finding mean

随声附和 提交于 2020-01-05 03:27:19

问题


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

  • Slide an 8x8 window over the matrix
  • Calculate the mean for each pixel in the matrix

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

Thanks.


回答1:


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.




回答2:


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.

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.




回答3:


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

then use the mean(A) function for each submatrices



来源:https://stackoverflow.com/questions/14454338/matlab-8x8-window-and-finding-mean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!