numpy的ndarray数组如何reshape成固定大小
在做肺结节检测的时候,遇到dicom文件reshape之后尺寸大小不一。因为大下不一,numpy.reshape又无法重塑成指定大小的。最后还是在一个大牛的代码中找到了解决方法。
VL = np.load(r'D:\pycharm\TEAMWORK\Preprocess_3D\imageOR.npy')
# 我的imageOR中,每一个文件除了3维的ndarray之外,还保存了标签lab,所以下面写成isometric_volume[0],所以如果你只有数组信息,直接将后面的[0]去掉即可
votal = []
for isometric_volume in VL:
print(isometric_volume[0],'第一个')
mean = np.mean(isometric_volume[0]).astype(np.float32)
std = np.std(isometric_volume[0]).astype(np.float32)
volume_resized = scipy.ndimage.interpolation.zoom(isometric_volume[0],
np.divide(64, isometric_volume[0].shape),
mode='nearest')
volume_resized = (volume_resized.astype(np.float32) - mean) / (std + 1e-7)
z0, z1 = volume_resized.shape[0]//2, volume_resized.shape[0]
y0, y1 = 0, volume_resized.shape[1]//2
volume_resized = volume_resized[z0:z1, y0:y1, :]
votal.append([volume_resized , isometric_volume[1]])
print(volume_resized,'volume_resized')
print(volume_resized.shape)
print(votal , 'votal')
np.save('newimage.npy',votal)