I couldn’t find any example with an affine matrix for downsizing / resizing a 3D array so I thought I’d share this code snippet.
import dask_image.ndinterp as ndinterp
import dask.array as da
import matplotlib.pyplot as plt
# Dummy 3D-array of annotation labels
ims = da.zeros((1000, 600, 600))
ims[:, 200:500, 200:400] = 1
ims[:, 300:350, 300:400] = 2
sz, sy, sx = ims.shape
# Scaling factors
fz = 20 # downscale by 20 in the z-direction
fy = 2 # downscale by half in the y-direction
fx = 2 # downscale by half in the x-direction
# Translation offsets to prevent image motif from moving to top-left corner
tz = -sz//2
ty = -sy//2
tx = -sx//2
affine_matrix = da.array([
[fz, 0, 0, tz],
[0, fy, 0, ty],
[0, 0, fx, tx],
[0, 0, 0, 1]])
ims_ds = ndinterp.affine_transform(ims, affine_matrix, mode=‘nearest’)
# Inspect result
fig, axs = plt.subplots(1, 2)
axs[0].imshow(ims[100, :, :])
axs[1].imshow(ims_ds[100, :, :])