def slice_by_label(): ''' :return: ''' files = glob.glob("../datasets/myo_data/train25_myops_gd_convert/*.nii.gz") output_lab_dir = "../datasets/myo_data/train25_myops_gd_crop/" output_img_dir = "../datasets/myo_data/train25_myops_crop/" mkdir_if_not_exist(output_lab_dir) for i in files: lab = sitk.ReadImage(i) #先转化成统一space,保证crop的大小一致 lab = sitkResample3DV2(lab, sitk.sitkNearestNeighbor, [1, 1, 1]) bbox = get_bounding_boxV2(sitk.GetArrayFromImage(lab), padding=10) ##extend bbox crop_lab = crop_by_bbox(lab, bbox) crop_lab = sitkResize3DV2(crop_lab, [256, 256, crop_lab.GetSize()[-1]], sitk.sitkNearestNeighbor) sitk_write_image(crop_lab[:, :, crop_lab.GetSize()[-1] // 2], dir=output_lab_dir, name=os.path.basename(i)) img_file = glob.glob( "../datasets/myo_data/train25_convert/*%s*.nii.gz" % (os.path.basename(i).split("_")[2])) for j in img_file: img = sitk.ReadImage(j) img = sitkResample3DV2(img, sitk.sitkLinear, [1, 1, 1]) crop_img = crop_by_bbox(img, bbox) crop_img = sitkResize3DV2( crop_img, [256, 256, crop_img.GetSize()[-1]], sitk.sitkLinear) sitk_write_image(crop_img[:, :, crop_lab.GetSize()[-1] // 2], dir=output_img_dir, name=os.path.basename(j))
def __sitkresize(self, img, lab, size): img = sitkResize3DV2(img, size, sitk.sitkLinear) # img=sitkResize3D(img,size) lab = sitkResize3DV2(lab, size, sitk.sitkNearestNeighbor) # lab=sitkResize3D(lab,size) return img, lab
def resize_img_lab(imgs,labs,output_img,output_lab,zscore): for p_img,p_lab in zip(imgs,labs): img_obj=sitk.ReadImage(p_img) lab_obj=sitk.ReadImage(p_lab) resize_lab = sitkResize3DV2(lab_obj, [96, 96, 96], sitk.sitkNearestNeighbor) sitk_write_image(resize_lab, dir=output_lab, name=os.path.basename(p_lab)) resize_img = sitkResize3DV2(img_obj, [96, 96, 96], sitk.sitkLinear) if zscore: resize_img=sitk.RescaleIntensity(resize_img) resize_img=sitk.Normalize(resize_img) sitk_write_image(resize_img, dir=output_img, name=os.path.basename(p_img))
def de_resize(refs, tgts, output_dir): for r, t in zip(refs, tgts): ref = sitk.ReadImage(r) lab = sitk.ReadImage(t) lab = sitkResize3DV2(lab, ref.GetSize(), sitk.sitkNearestNeighbor) sitk_write_image(lab, dir=output_dir, name=os.path.basename(t))
def preprocess_mannul_itk_img(self, structure=420): for i in range(self.img.num_data): print("process image %d" % (i + 1)) image = self.img.get_file_obj(i) print("original size: ", image.GetSize()) image = sitkResize3DV2(image, [96, 96, 96], sitk.sitkLinear) print("image size: ", image.GetSize()) # image=self.__reset_space(image) self.__itksave_single_img(image, i, tag='resize')
def de_resize(self, structure): self.structure = structure save_dir = self.target_labels.dir_name + "_deResize" for i in range(self.target_labels.num_data): print("process image %d" % (i + 1)) ref = self.references.get_file_obj(i) lab = self.target_labels.get_file_obj(i) lab = sitkResize3DV2(lab, ref.GetSize(), sitk.sitkNearestNeighbor) self.__itksave_label(lab, i, save_dir) return save_dir