在 numpy 中对大型 3D 图像进行下采样

2024-01-11

我需要通过任意非整数因子对由一系列 2d tiff 切片组成的大型 3D 图像 (30GB +) 进行下采样。 scipy.ndimage.zoom 对于适合 RAM 的输入图像效果很好。

我正在考虑读取堆栈的部分内容并使用 scipy.ndimage_map_coordintes 来获取插值像素坐标。另一个想法是使用 numpy.memmap 创建一个内存映射数组并对其执行 scipy.ndimage.zoom 。

在我继续之前,有人有更好的方法吗?


所以我通过查看 ImageJ 源代码来弄清楚该怎么做。我将其发布在这里,以防对其他人有所帮助:

import SimpleITK as sitk
import cv2
import numpy as np

def downsample_large_volume(img_path_list, input_voxel_size, output_voxel_size):

    scale = input_voxel_size / output_voxel_size
    resampled_zs = []

    #Resample z slices
    for img_path in img_path_list:
        z_slice_arr = cv2.imread(img_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
        z_slice_resized = cv2.resize(z_slice_arr, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_AREA)
        resampled_zs.append(z_slice_resized) # Or save to disk to save RAM and use np.memmap for xz scaling


    temp_arr = np.dstack(resampled_zs)  # We seem to be in yxz space now
    final_scaled_slices = []

    # Resample xz plane at each y
    for y in range(temp_arr.shape[0]):
        xz_pane = temp_arr[y, :, :]
        scaled_xz = cv2.resize(xz_pane, (0, 0), fx=scale, fy=1, interpolation=cv2.INTER_AREA)
        final_scaled_slices.append(scaled_xz)

    final_array = np.dstack(final_scaled_slices)


    img = sitk.GetImageFromArray(np.swapaxes(np.swapaxes(final_array, 0, 1), 1, 2))
    sitk.WriteImage(img, 'scaled_by_pixel.nrrd')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 numpy 中对大型 3D 图像进行下采样 的相关文章

随机推荐