参考链接:
dicom数据处理示例https://www.kaggle.com/akh64bit/full-preprocessing-tutorial
1.crop中间区域(结合网络输入的尺寸设置)
def get_square_crop(img, base_size=128, crop_size=128):
try:
res = img
height, width = res.shape
except:
pass
else:
if height < base_size:
diff = base_size - height
extend_top = diff / 2
extend_bottom = diff - extend_top
res = cv2.copyMakeBorder(res, extend_top, extend_bottom, 0, 0, borderType=cv2.BORDER_CONSTANT, value=0)
height = base_size
print("增加边框")
if width < base_size:
diff = base_size - width
extend_top = diff / 2
extend_bottom = diff - extend_top
res = cv2.copyMakeBorder(res, 0, 0, int(extend_top), int(extend_bottom), borderType=cv2.BORDER_CONSTANT,
value=0)
width = base_size
print("增加边框")
crop_y_start = (height - crop_size) / 2
crop_x_start = (width - crop_size) / 2
res = res[int(crop_y_start):int(crop_y_start + crop_size), int(crop_x_start):int(crop_x_start + crop_size)]
return res
2.shape一致改变resample(非常见操作)
def resample_shape(image, PixelSpacing, new_shape=np.array([180,180])):
# Determine current pixel spacing
shape = np.multiply(np.array(image.shape), np.array(PixelSpacing))
new_spacing=np.divide(shape,new_shape)
resize_factor =np.divide(PixelSpacing ,new_spacing)
new_shape = np.round(new_shape)
real_resize_factor = new_shape / image.shape
image = scipy.ndimage.interpolation.zoom(image, real_resize_factor)
return image, new_spacing
3.pixcelspacing一致(统一分辨率)
宗旨:每个数据最后在shape一致的时候,应该pixcel的真实物理尺寸相差不大
这里也需要统计分析所有spacing的分布情况,才能决定统一的spacing的大小。
A scan may have a pixel spacing of [2.5, 0.5, 0.5], which means that the distance between slices is 2.5 millimeters. For a different scan this may be [1.5, 0.725, 0.725], this can be problematic for automatic analysis (e.g. using ConvNets)!
A common method of dealing with this is resampling the full dataset to a certain isotropic resolution. If we choose to resample everything to 1mm1mm1mm pixels we can use 3D convnets without worrying about learning zoom/slice thickness invariance.
Whilst this may seem like a very simple step, it has quite some edge cases due to rounding. Also, it takes quite a while.
Below code worked well for us (and deals with the edge cases)
def resample_sapcing(image,spacing, new_spacing=[1.27,1.27]):
# Determine current pixel spacing
spacing = np.array(list(spacing))
resize_factor = spacing / new_spacing
new_real_shape = image.shape * resize_factor
new_shape = np.round(new_real_shape)
real_resize_factor = new_shape / image.shape
new_spacing = spacing / real_resize_factor
image = scipy.ndimage.interpolation.zoom(image, real_resize_factor)
return image, new_spacing
Please note that when you apply this, to save the new spacing! Due to rounding this may be slightly off from the desired spacing (above script picks the best possible spacing with rounding).
Let’s resample our patient’s pixels to an isomorphic resolution of 1 by 1 by 1 mm.