def optic_disk_detect_3(img): ''' Method that seems to work well with DIARETDB1 database. ''' hsi = rgb_to_hsi(img) intensity = hsi[:, :, 2].copy() #plt.axis('off'); show_image(intensity) #i_sp = add_salt_and_pepper(intensity, 0.005) i_med = mh.median_filter(intensity) # use Wiener filter instead? i_clahe = skimage.exposure.equalize_adapthist(i_med) #plt.axis('off'); show_image(i_clahe) seeds = (i_clahe > 0.85) seeds = skimage.morphology.remove_small_objects(seeds, min_size=300, connectivity=2) #plt.axis('off'); show_image(seeds) optic_disk_map = region_growing_1(i_clahe, radius=3, tol1=0.1, tol2=0.2, tol3=0.2, seeds=seeds) optic_disk_map += 1 #plt.axis('off'); show_image(optic_disk_map) _cl_areas = cl_areas(optic_disk_map) print _cl_areas optic_disk_map = leave_segments_by_mask(optic_disk_map, (8000 < _cl_areas) & (_cl_areas < 30000)) #optic_disk_map = skimage.morphology.remove_small_objects(optic_disk_map, min_size=500, connectivity=2) optic_disk_map = mh.close_holes(mh.close(optic_disk_map, Bc=np.ones((10, 10)))) #optic_disk_map = skimage.morphology.remove_small_objects(optic_disk_map, min_size=5000, connectivity=2) if np.all(optic_disk_map == 0): print 'Disk not found' return optic_disk_map
def optic_disk_detect_2(img): hsi = rgb_to_hsi(img) intensity = hsi[:, :, 2].copy() i_sp = add_salt_and_pepper(intensity, 0.005) i_med = mh.median_filter(i_sp) i_clahe = skimage.exposure.equalize_adapthist(i_med) optic_disk_map = (i_clahe > 0.6) & (hsi[:, :, 1] < 0.3) #show_image(optic_disk_map) optic_disk_map = skimage.morphology.remove_small_objects(optic_disk_map, min_size=500, connectivity=2) optic_disk_map = mh.close_holes(mh.close(optic_disk_map, Bc=np.ones((30, 30)))) optic_disk_map = skimage.morphology.remove_small_objects(optic_disk_map, min_size=10000, connectivity=2) if np.all(optic_disk_map == 0): print 'Disk not found' return optic_disk_map
def exudates_get_features_3(folder, elements_folder, fundus_mask, num_imgs=10, stride_width=6): thr_high = 189.0 / 255.0 y = [] pxl_df = pd.DataFrame(columns=['hue', 'intensity', 'mean intensity', 'std intensity', 'dist to optic disk']) for img_fn in image_names_in_folder(folder)[:num_imgs]: print 'Image filename:', img_fn img = load_image(img_fn) short_fn = os.path.split(img_fn)[-1] hard_exud = load_image(os.path.join(elements_folder, short_fn)) hsi = rgb_to_hsi(img) intensity = hsi[:, :, 2].copy() #i_sp = add_salt_and_pepper(intensity, 0.005) i_med = mh.median_filter(intensity) # use Wiener filter instead? i_clahe = skimage.exposure.equalize_adapthist(i_med) optic_disk_map = optic_disk_detect_3(img) mask, optic_disk_center = optic_disk_mask(optic_disk_map, return_center=True) print 'Disk center: ', optic_disk_center fundus_wo_disk_mask = fundus_mask & (~mask) pxl_mask = np.zeros_like(i_clahe, dtype=np.bool) pxl_mask[::stride_width, ::stride_width] = True pxl_mask[~fundus_wo_disk_mask] = False bbox = bounding_box(hard_exud >= thr_high, bbox_radius_ratio=2) pxl_mask &= bbox cur_pxl_df = pd.DataFrame(columns=['hue', 'intensity', 'mean intensity', 'std intensity', 'dist to optic disk']) cur_pxl_df['hue'] = hsi[:, :, 0][pxl_mask].ravel() cur_pxl_df['intensity'] = i_clahe[pxl_mask].ravel() cur_pxl_df['mean intensity'] = pf.pxls_mean_intensity(i_clahe, stride_width, neighbd_rad=7, mask=pxl_mask).ravel() cur_pxl_df['std intensity'] = pf.pxls_std_intensity(i_clahe, stride_width, neighbd_rad=3, mask=pxl_mask).ravel() cur_pxl_df['dist to optic disk'] = pf.pxls_dist_to_pnt(i_clahe.shape, optic_disk_center, stride_width, mask=pxl_mask).ravel() print 'Accounted pixels: ', len(cur_pxl_df) y.append(hard_exud[pxl_mask]) # we can use fraction of white points in a neighborhood instead, # but it becomes regression task then pxl_df = pd.concat((pxl_df, cur_pxl_df), axis=0, ignore_index=True) y = np.hstack(y) y = y >= thr_high return pxl_df, y
def hemorrhages_detect_3(img, fundus_mask, clf, stride_width=6): #thr_high = 189.0 / 255.0 #img = imh.load_image(img_fn) #short_fn = os.path.split(img_fn)[-1] #hard_exud = imh.load_image(os.path.join(hard_exud_folder, short_fn)) hsi = rgb_to_hsi(img) intensity = hsi[:, :, 2].copy() #i_sp = add_salt_and_pepper(intensity, 0.005) i_med = mh.median_filter(intensity) # use Wiener filter instead? i_clahe = skimage.exposure.equalize_adapthist(i_med) optic_disk_map = optic_disk_detect_3(img) mask, optic_disk_center = optic_disk_mask(optic_disk_map, return_center=True) #print 'Disk center: ', optic_disk_center fundus_wo_disk_mask = fundus_mask & (~mask) pxl_mask = np.zeros_like(i_clahe, dtype=np.bool) pxl_mask[::stride_width, ::stride_width] = True pxl_mask[~fundus_wo_disk_mask] = False cur_pxl_df = pd.DataFrame(columns=['hue', 'saturation', 'intensity', 'mean intensity', 'std intensity']) cur_pxl_df['hue'] = hsi[:, :, 0][::stride_width, ::stride_width].ravel() cur_pxl_df['saturation'] = hsi[:, :, 1][::stride_width, ::stride_width].ravel() cur_pxl_df['intensity'] = i_clahe[::stride_width, ::stride_width].ravel() cur_pxl_df['mean intensity'] = pf.pxls_mean_intensity(i_clahe, stride_width, neighbd_rad=3).ravel() cur_pxl_df['std intensity'] = pf.pxls_std_intensity(i_clahe, stride_width, neighbd_rad=7).ravel() print 'Accounted pixels: ', len(cur_pxl_df) scaler = sklearn.preprocessing.StandardScaler() cur_pxl_df_scaled = scaler.fit_transform(cur_pxl_df) pred = clf.predict(cur_pxl_df_scaled) ''' pred_map = np.zeros_like(pxl_mask, dtype=np.uint8) k = 0 for i in xrange(pred_map.shape[0]): for j in xrange(pred_map.shape[1]): if pxl_mask[i, j]: pred_map[i, j] = 255 * pred[k] if pred[k]: cv2.circle(pred_map, (i, j), 8, (255, 255, 255)) k += 1 ''' pred_map = pred.reshape(pxl_mask[::stride_width, ::stride_width].shape) show_image(pred_map) pred_map &= pxl_mask[::stride_width, ::stride_width] pred_map_full = np.zeros_like(pxl_mask, dtype=np.uint8) pred_map_full[::stride_width, ::stride_width] = pred_map pred_map_full_enl = np.zeros_like(pred_map_full, dtype=np.uint8) for i in xrange(pred_map_full.shape[0]): for j in xrange(pred_map_full.shape[1]): if pred_map_full[i, j]: #cv2.circle(pred_map_full_enl, (i, j), 3, (255, 255, 255)) sz = stride_width pred_map_full_enl[i - sz + 1:i + sz, j - sz + 1:j + sz] = 1 return pred_map_full_enl