def preprocess_train(datapath, processedpath): os.mkdir(processedpath) os.mkdir(f'{processedpath}/image') os.mkdir(f'{processedpath}/label') idxs = range(len(os.listdir(f'{datapath}/image/'))) n = len(idxs) for i, idx in enumerate(idxs): sys.stdout.write(f'\rProcessing...{i+1}/{n}') sys.stdout.flush() img = imread(f'{datapath}/image/{idx}.tif') lung_mask = preprocess_helpers.get_lung_mask(img).astype('float') if str(idx) + '.tif' in os.listdir('data/special_train_masks'): lung_mask += imread(f'data/special_train_masks/{idx}.tif').astype( 'float') lung_mask = np.clip(lung_mask, 0, 1) lung_mask = preprocess_helpers.resize(lung_mask) if lung_mask.sum() == 0: sys.stdout.write( f'\rEmpty lung field returned for image {idx}. Skipping\n') continue img = preprocess_helpers.normalize(img) img = preprocess_helpers.resize(img) img = img * lung_mask pil_im = Image.fromarray(img) enhancer = ImageEnhance.Contrast(pil_im) enhanced_im = enhancer.enhance(2.0) np_im = np.array(enhanced_im) imsave(f'{processedpath}/image/{idx}.tif', np_im) mask = imread(f'{datapath}/label/{idx}.tif') mask = preprocess_helpers.resize(mask) imsave(f'{processedpath}/label/{idx}.tif', mask) print(f'\nComplete.')
def preprocess(datapath, processedpath): os.mkdir(processedpath) for i in range(8): os.mkdir(f'{processedpath}/image{i}') os.mkdir(f'{processedpath}/label{i}') idxs = os.listdir(f'{datapath}') n = len(idxs) for i, idx in enumerate(idxs): sys.stdout.write(f'\rProcessing...{i+1}/{n}') sys.stdout.flush() empty_found = False image, mask, spacing, thickness = pickle.load( open(f'data/extracted/{idx}', 'rb') ) processed_lungmasks = [] processed_image = [] processed_mask = [] for j in range(len(image)): if empty_found: continue img = image[j] processed_lungmasks.append(preprocess_helpers.get_lung_mask(img)) processed_image.append(img) processed_mask.append(mask[j]*100) lung_mask = max(processed_lungmasks, key=lambda x: x.sum()) image = resample( np.array(processed_image), (spacing, spacing), thickness ) mask = resample( np.array(processed_mask), (spacing, spacing), thickness ) mask = np.clip(mask, 0, 1) for k in range(8): im = preprocess_helpers.normalize(image[k]) im = preprocess_helpers.resize(im) im = im*preprocess_helpers.resize(lung_mask) pil_im = Image.fromarray(im) enhancer = ImageEnhance.Contrast(pil_im) enhanced_im = enhancer.enhance(2.0) np_im = np.array(enhanced_im) mk = preprocess_helpers.resize(mask[k]) imsave(f'{processedpath}/image{k}/{i}.tif', np_im) imsave(f'{processedpath}/label{k}/{i}.tif', mk.astype(np.int32)) print(f'\nComplete.')
def preprocess_img(img, special=False, manual_lung_mask=None): lung_mask = get_lung_mask(img).astype('float') if special: lung_mask += manual_lung_mask lung_mask = np.clip(lung_mask, 0, 1) lung_mask = resize(lung_mask) img = normalize(img) img = resize(img) img = img * lung_mask pil_im = Image.fromarray(img) enhancer = ImageEnhance.Contrast(pil_im) enhanced_im = enhancer.enhance(2.0) return np.array(enhanced_im)
def preprocess_img(img, special=0, custom_lung_mask=None): """ Preprocessing pipeline for individual images. Applies lung field segmentation, resizes, normalizes and enhances constrast :param img: input image :param special: whether or not the image has a difficult/custom lung mask :param custom_lung_mask: custom lung mask (if special != 0) """ lung_mask = get_lung_mask(img).astype('float') if special == 1: lung_mask += custom_lung_mask lung_mask = np.clip(lung_mask, 0, 1) if special == 2: lung_mask = custom_lung_mask lung_mask = resize(lung_mask) img = normalize(img) img = resize(img) img = img * lung_mask pil_im = Image.fromarray(img) enhancer = ImageEnhance.Contrast(pil_im) enhanced_im = enhancer.enhance(2.0) return np.array(enhanced_im)