BlockMean

Version 1.2.0.0 (9.8 KB) by Jan
Mean of rectangular submatrices, fast C-Mex (no running mean)
2.5K Downloads
Updated 5 Nov 2010

View License

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)
Input:
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.
Output:
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.

Cite As

Jan (2024). BlockMean (https://www.mathworks.com/matlabcentral/fileexchange/24812-blockmean), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: Conservative regridding

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.2.0.0

No need to enable 80bit extended precision under Linux. This avoids troubles during the compilation.

1.1.0.0

64 bit addressing supported, rectangular blocks: V ~= W, isEqualTol included in the test function

1.0.0.0