BLOCKMEAN - Fast mean of rectangular submatrices
The mean of V*W elements along the 1st and 2nd dimension is calculated. This is no running mean filter: The sizes of the 1st and 2nd dimension are reduced by the factors V and W.
I use this as cheap anti-aliasing of RGB images, therefore it is implemented for DOUBLE and UINT8 input.
Y = BlockMean(X, V, W)
X: UINT8 or DOUBLE array of any size.
V, W: 2 scalars as size of the window. Each element of the output is the
mean over V*W neighbouring elements of the input.
V and W are limited to 256 to limit memory usage.
Y: UINT8 or DOUBLE array, the 1st and 2nd dimensions are V and W times shorter:
[FIX(X / V) x FIX(Y / W) x (further dims...)]
If the size of the 1st or 2nd dimension is not a multiple of V and W, the
remaining elements at the end are skipped.
E.g. for 4x4 blocks of a 1024x768x3 double array, this MEX implementation is about 5 times faster than the corresponding Matlab method (Matlab 2009a, 1.5GHz Pentium-M, WinXP, MSVC 2008):
reshape(sum(sum(reshape(X, 4, 256, 4, 192, 3), 1), 3), 256, 192, 3).
Run the unit-test uTest_BlockMean to check validity and speed.
Tested with: LCC v2.4, v3.8, Open Watcom 1.8, BCC 5.5, MSVC 2008, Matlab 6.5, 7.7, 7.8, WinXP 32 bit
Compatibility assumed: Linux, MacOS, 64bit.
Pre-compiled Mex: http://www.n-simon.de/mex
I'd appreciate suggestions for improvements and bug reports sent through email - thanks.