Examples of how to do downsample a matrix by averaging elements n*n with numpy in python:
Create a matrix
Let's first create a simple matrix:
Note: see the previous note how to upsample an array by repeating elements using numpy in python
import numpy as npa = np.array([[0,1], [2,3]])a = np.kron(a, np.ones((3,3)))print(a)[[0. 0. 0. 1. 1. 1.][0. 0. 0. 1. 1. 1.][0. 0. 0. 1. 1. 1.][2. 2. 2. 3. 3. 3.][2. 2. 2. 3. 3. 3.][2. 2. 2. 3. 3. 3.]]print(a.shape)
returns
(6, 6)
Downsampling the matrix a by avergaging 2*2 elements
Example of how to downsample by avergaging 2 by 2 elements of matrix a:
n = 2b = a.shape[0]//na_downsampled = a.reshape(-1, n, b, n).sum((-1, -3)) / nprint(a_downsampled)
returns
[[0. 1. 2.][2. 3. 4.][4. 5. 6.]]
Using a 2d convolution
Another example using scipy.signal.convolve2d
from scipy.signal import convolve2dkernel = np.ones((n, n))convolved = convolve2d(a, kernel, mode='valid')a_downsampled = convolved[::n, ::n] / nprint(a_downsampled)
returns:
[[0. 1. 2.][2. 3. 4.][4. 5. 6.]]
References
| Links | Site |
|---|---|
| numpy.ufunc.reduce | numpy.org |
| Strided convolution of 2D in numpy | stackoverflow |
| How to reduce the dimensions of a numpy array by using the sum over n elements? | stackoverflow |
| Decrease array size by averaging adjacent values with numpy | stackoverflow |
| Array reduction operations | jarrodmillman.com |
