def load_vol_and_seg( vol_file, load_seg=True, do_mask_vol=False, load_contours=False, keep_labels=None, ): if 'train' in vol_file: segs_dir = MRI_TRAIN_SEGS_DIR elif 'valid' in vol_file: segs_dir = MRI_VALIDATION_SEGS_DIR else: segs_dir = MRI_TEST_SEGS_DIR # load volume and corresponding segmentation from file vol_name = os.path.splitext(os.path.basename(vol_file))[0].split('_vol')[0] img_data = np.load(vol_file)['vol_data'][..., np.newaxis] if load_seg or do_mask_vol or load_contours: seg_vol_file = os.path.join(segs_dir, '{}_seg.npz'.format(vol_name)) if not os.path.isfile(seg_vol_file): print('Could not find corresponding seg file for {}!'.format( vol_file)) return None seg_data = np.load(seg_vol_file)['seg_data'] else: seg_data = None if do_mask_vol: # background should always be 0 and other segs should be integers img_data *= (seg_data > 0).astype(int)[..., np.newaxis] if keep_labels is not None: unique_seg_labels = np.unique(seg_data) for l in unique_seg_labels: if l not in keep_labels: seg_data[seg_data == l] = 0 contours_file = os.path.join( MRI_CONTOURS_DIR, '{}_{}labels.npz'.format(vol_name, len(keep_labels))) else: contours_file = os.path.join(MRI_CONTOURS_DIR, '{}_alllabels.npz'.format(vol_name)) contours = None if load_contours: if not os.path.isfile(contours_file): contours = pynd_segutils.seg2contour( seg_data, contour_type='both')[..., np.newaxis] contours[contours > 0] = 1 np.savez(contours_file, contours_data=contours) else: contours = np.load(contours_file)['contours_data'] return img_data, seg_data, contours
def _load_vol_and_seg(vol_file, load_seg=True, do_mask_vol=False, load_contours=False, keep_labels=None, ): if 'train' in vol_file: mode = 'train' elif 'valid' in vol_file: mode = 'validate' else: mode = 'test' # load volume and corresponding segmentation from file vol_name = os.path.splitext(os.path.basename(vol_file))[0] if vol_name == 'atlas': img_data, seg_data = _load_atlas_vol() else: if mri_vol_root in vol_file: vol_base_name = '_'.join(vol_name.split('_')[:-1]) else: vol_base_name = vol_name img_data = np.load(vol_file)['vol_data'][..., np.newaxis] if load_seg or do_mask_vol or load_contours: if mri_vol_root in vol_file: seg_vol_file = mri_vol_root + '/' + mode + '/asegs/' + vol_base_name + '_aseg.npz' elif buckner_vol_root in vol_file: seg_vol_file = os.path.join(buckner_vol_root, 'asegs', vol_base_name + '.npz') else: print('Could not find corresponding seg file for {}!'.format(vol_file)) sys.exit() if not os.path.isfile(seg_vol_file): print('Could not find corresponding seg file for {}!'.format(vol_file)) return None seg_data = np.load(seg_vol_file)['vol_data'] else: seg_data = None if do_mask_vol: # background should always be 0 and other segs should be integers img_data *= (seg_data > 0).astype(int)[..., np.newaxis] if keep_labels is not None: unique_seg_labels = np.unique(seg_data) for l in unique_seg_labels: if l not in keep_labels: seg_data[seg_data==l] = 0 contours_file = os.path.join(mri_contours_root, vol_name + '_{}labels.npz'.format(len(keep_labels))) else: contours_file = os.path.join(mri_contours_root, vol_name + '_alllabels.npz') contours = None if load_contours: if not os.path.isfile(contours_file): contours = segutils.seg2contour(seg_data, contour_type='both')[..., np.newaxis] contours[contours > 0] = 1 np.savez(contours_file, contours_data=contours) else: contours = np.load(contours_file)['contours_data'] return img_data, seg_data, contours
def __compute_contour(seg_data): contours = pynd_segutils.seg2contour(seg_data, exclude_zero=True, contour_type='both')[None] contours[contours > 0] = 1 return torch.Tensor(contours).cuda()
def load_vol_and_seg( vol_file, load_seg=True, do_mask_vol=False, load_contours=False, keep_labels=None, ): if 'train' in vol_file: segs_dir = MRI_TRAIN_SEGS_DIR elif 'valid' in vol_file: segs_dir = MRI_VALIDATION_SEGS_DIR else: segs_dir = MRI_TEST_SEGS_DIR # load volume and corresponding segmentation from file #数据文件命名有强制要求'_vol'[wangxu] vol_name = os.path.splitext(os.path.basename(vol_file))[0].split('_vol')[0] # 到这里数据已加载进来 [wangxu] img_data = np.load(vol_file)['vol_data'][ ..., np.newaxis] # 文件内键值'vol_data'也写死了 [wangxu] if load_seg or do_mask_vol or load_contours: # load_seg 和 load_contours都会加载分割标记,load_contours会将分割标记转换为轮廓 [wangxu] seg_vol_file = os.path.join(segs_dir, '{}_seg.npz'.format(vol_name)) if not os.path.isfile(seg_vol_file): print('Could not find corresponding seg file for {}!'.format( vol_file)) return None seg_data = np.load(seg_vol_file)[ 'seg_data'] # 分割标记i文件内的键值为'seg_data' [wangxu] else: seg_data = None if do_mask_vol: # 图像去背景 [wangxu] # background should always be 0 and other segs should be integers img_data *= (seg_data > 0).astype(int)[..., np.newaxis] if keep_labels is not None: unique_seg_labels = np.unique(seg_data) for l in unique_seg_labels: # 去掉未包含在预设标签中的像素,标签置为0 [wangxu] if l not in keep_labels: seg_data[seg_data == l] = 0 contours_file = os.path.join( MRI_CONTOURS_DIR, '{}_{}labels.npz'.format( vol_name, len(keep_labels))) # 定义所要生成contours文件的路径 [wangxu] else: contours_file = os.path.join(MRI_CONTOURS_DIR, '{}_alllabels.npz'.format(vol_name)) contours = None if load_contours: if not os.path.isfile(contours_file): contours = pynd_segutils.seg2contour( seg_data, contour_type='both')[..., np.newaxis] # 这一步很耗时 [wangxu] contours[contours > 0] = 1 # 轮廓进行了二值化 [wangxu] np.savez(contours_file, contours_data=contours) else: contours = np.load(contours_file)['contours_data'] return img_data, seg_data, contours