Suppose I have a matrix:
sbt = RandomInteger[{0, 2^16 - 1}, {5202, 3465}];
It represents an image and I would like to do "pixel binning" on it, where I take non-overlapping n x n
square submatrices and replace them with the total of each submatrix. A typical n
is 8. When the image dimensions aren't divisible by n
it's okay to discard the last few rows and columns.
Importantly, each resulting pixel is a total, not an average or a bilinear resampling.
My naive function to do this is:
imageBin[imagedata_, n_] := With[
{dims = Reverse@Dimensions[imagedata]},
Table[
Total[Flatten[imagedata[[row ;; row + n - 1, col ;; col + n - 1]]]],
{row, 1, dims[[2]] - n + 1, n},
{col, 1, dims[[1]] - n + 1, n}
]
]
I have thousands of images to process this way, so I care about making the process fast.
Timing[Do[imageBin[sbt, 8];, {10}]]
gives about 3.4 seconds on my machine. Is there a way to make this faster?
n*RandomInteger...
for each bin. $\endgroup$