我需要通过任意非整数因子对由一系列2d tiff切片组成的大型3D图像(30GB +)进行下采样。 scipy.ndimage.zoom适用于适合RAM的输入图像。
我正在考虑读取部分堆栈并使用scipy.ndimage_map_coordintes来获取插值像素坐标。另一个想法是使用numpy.memmap创建一个内存映射数组,并对此执行scipy.ndimage.zoom。
在我继续这个之前,有没有人有更好的方法?
答案 0 :(得分:2)
所以我通过查看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')