def get_images_diaretdb(image_dir, preprocess=False): if preprocess: limit = 2 grid_size = 8 if not os.path.exists(os.path.join(image_dir, 'ddb1_fundusimages_CLAHE')): os.mkdir(os.path.join(image_dir, 'ddb1_fundusimages_CLAHE')) images_ori = glob.glob(os.path.join(image_dir, 'ddb1_fundusimages/*.png')) images_ori.sort() # mean brightness. meanbright = 0. for img_path in images_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'ddb1_fundusmask', 'fmask1.tif') gray = cv2.imread(img_path, 0) mask_img = cv2.imread(mask_path, 0) brightness = gray.sum() / (mask_img.shape[0] * mask_img.shape[1] - mask_img.sum() / 255.) meanbright += brightness meanbright /= len(images_ori) # preprocess for images. for img_path in images_ori: img_name = os.path.split(img_path)[-1] mask_path = os.path.join(image_dir, 'ddb1_fundusmask', 'fmask1.tif') clahe_img = clahe_gridsize(img_path, mask_path, denoise=True, verbose=False, brightnessbalance=meanbright, cliplimit=limit, gridsize=grid_size) cv2.imwrite(os.path.join(image_dir, 'ddb1_fundusimages_CLAHE', os.path.split(img_path)[-1]), clahe_img) image_paths = glob.glob(os.path.join(image_dir, 'ddb1_fundusimages_CLAHE/*.png')) else: image_paths = glob.glob(os.path.join(image_dir, 'ddb1_fundusimages/*.png')) image_paths.sort() return image_paths
def get_images(image_dir, preprocess='0', phase='train'): if phase == 'train' or phase == 'eval': setname = 'TrainingSet' elif phase == 'test': setname = 'TestingSet' limit = 2 grid_size = 8 if not os.path.exists(os.path.join(image_dir, 'Images_CLAHE' + preprocess)): os.mkdir(os.path.join(image_dir, 'Images_CLAHE' + preprocess)) if not os.path.exists(os.path.join(image_dir, 'Images_CLAHE' + preprocess, setname)): os.mkdir(os.path.join(image_dir, 'Images_CLAHE' + preprocess, setname)) # compute mean brightess meanbright = 0. images_number = 0 for tempsetname in ['TrainingSet', 'TestingSet']: imgs_ori = glob.glob(os.path.join(image_dir, 'OriginalImages/'+ tempsetname + '/*.jpg')) imgs_ori.sort() images_number += len(imgs_ori) # mean brightness. for img_path in imgs_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'Groundtruths', tempsetname, 'Mask', img_name + '_MASK.tif') gray = cv2.imread(img_path, 0) mask_img = cv2.imread(mask_path, 0) brightness = gray.sum() / (mask_img.shape[0] * mask_img.shape[1] - mask_img.sum() / 255.) meanbright += brightness meanbright /= images_number imgs_ori = glob.glob(os.path.join(image_dir, 'OriginalImages/' + setname + '/*.jpg')) preprocess_dict = {'0': [False, False, None], '1': [False, False, meanbright], '2': [False, True, None], '3': [False, True, meanbright], '4': [True, False, None], '5': [True, False, meanbright], '6': [True, True, None], '7': [True, True, meanbright]} for img_path in imgs_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'Groundtruths', setname, 'Mask', img_name + '_MASK.tif') clahe_img = clahe_gridsize(img_path, mask_path, denoise=preprocess_dict[preprocess][0], contrastenhancement=preprocess_dict[preprocess][1], brightnessbalance=preprocess_dict[preprocess][2], cliplimit=limit, gridsize=grid_size) cv2.imwrite(os.path.join(image_dir, 'Images_CLAHE' + preprocess, setname, os.path.split(img_path)[-1]), clahe_img) imgs = glob.glob(os.path.join(image_dir, 'Images_CLAHE' + preprocess, setname, '*.jpg')) imgs.sort() mask_paths = [] train_number = int(len(imgs) * train_ratio) eval_number = int(len(imgs) * eval_ratio) if phase == 'train': image_paths = imgs[:train_number] elif phase == 'eval': image_paths = imgs[train_number:] else: image_paths = imgs mask_path = os.path.join(image_dir, 'Groundtruths', setname) lesions = ['HardExudates', 'Haemorrhages', 'Microaneurysms', 'SoftExudates', 'Mask'] lesion_abbvs = ['EX', 'HE', 'MA', 'SE', 'MASK'] for image_path in image_paths: paths = [] name = os.path.split(image_path)[1].split('.')[0] for lesion, lesion_abbv in zip(lesions, lesion_abbvs): candidate_path = os.path.join(mask_path, lesion, name + '_' + lesion_abbv + '.tif') if os.path.exists(candidate_path): paths.append(candidate_path) else: paths.append(None) mask_paths.append(paths) return image_paths, mask_paths
def get_images(image_dir, preprocess=False, phase='train', healthy_included=True): if preprocess: limit = 2 grid_size = 8 if not os.path.exists( os.path.join(image_dir, 'ApparentRetinopathy_CLAHE')): os.mkdir(os.path.join(image_dir, 'ApparentRetinopathy_CLAHE')) os.mkdir(os.path.join(image_dir, 'NoApparentRetinopathy_CLAHE')) apparent_ori = glob.glob( os.path.join(image_dir, 'ApparentRetinopathy/*.jpg')) noapparent_ori = glob.glob( os.path.join(image_dir, 'NoApparentRetinopathy/*.jpg')) apparent_ori.sort() noapparent_ori.sort() # mean brightness. meanbright = 0. for img_path in apparent_ori + noapparent_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'GroundTruth', 'MASK', img_name + '_MASK.tif') gray = cv2.imread(img_path, 0) mask_img = cv2.imread(mask_path, 0) brightness = gray.sum() / ( mask_img.shape[0] * mask_img.shape[1] - mask_img.sum() / 255.) meanbright += brightness meanbright /= len(apparent_ori + noapparent_ori) # preprocess for apparent. for img_path in apparent_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'GroundTruth', 'MASK', img_name + '_MASK.tif') clahe_img = clahe_gridsize(img_path, mask_path, denoise=True, verbose=False, brightnessbalance=meanbright, cliplimit=limit, gridsize=grid_size) cv2.imwrite( os.path.join(image_dir, 'ApparentRetinopathy_CLAHE', os.path.split(img_path)[-1]), clahe_img) # preprocess for noapparent. for img_path in noapparent_ori: img_name = os.path.split(img_path)[-1].split('.')[0] mask_path = os.path.join(image_dir, 'GroundTruth', 'MASK', img_name + '_MASK.tif') clahe_img = clahe_gridsize(img_path, mask_path, denoise=True, verbose=False, brightnessbalance=meanbright, cliplimit=limit, gridsize=grid_size) cv2.imwrite( os.path.join(image_dir, 'NoApparentRetinopathy_CLAHE', os.path.split(img_path)[-1]), clahe_img) apparent = glob.glob( os.path.join(image_dir, 'ApparentRetinopathy_CLAHE/*.jpg')) noapparent = glob.glob( os.path.join(image_dir, 'NoApparentRetinopathy_CLAHE/*.jpg')) else: apparent = glob.glob( os.path.join(image_dir, 'ApparentRetinopathy/*.jpg')) noapparent = glob.glob( os.path.join(image_dir, 'NoApparentRetinopathy/*.jpg')) apparent.sort() noapparent.sort() image_paths = [] mask_paths = [] if healthy_included: imgset = [apparent, noapparent] else: imgset = [apparent] for each in imgset: train_number = int(len(each) * train_ratio) eval_number = int(len(each) * eval_ratio) if phase == 'train': image_paths.extend(each[:train_number]) elif phase == 'eval': image_paths.extend(each[train_number:train_number + eval_number]) else: image_paths.extend(each[train_number + eval_number:]) mask_path = os.path.join(image_dir, 'GroundTruth') lesions = ['EX', 'HE', 'MA', 'SE', 'MASK'] for image_path in image_paths: paths = [] name = os.path.split(image_path)[1].split('.')[0] for lesion in lesions: candidate_path = os.path.join(mask_path, lesion, name + '_' + lesion + '.tif') if os.path.exists(candidate_path): paths.append(candidate_path) else: paths.append(None) mask_paths.append(paths) return image_paths, mask_paths