def detect_vessels_3(img, sigma=16, gabor_perc=95.0, one_channel_img=False): # Preprocessing if one_channel_img: intensity = img.copy() else: intensity = img.mean(axis=2) / 256.0 show_image(intensity); plt.axis('off'); plt.savefig('../presentation/pics/vessels/1.png') intensity = skimage.exposure.equalize_adapthist(intensity) intensity -= mh.median_filter(intensity, Bc=np.ones((25, 25))) show_image(intensity); plt.axis('off'); plt.savefig('../presentation/pics/vessels/2.png') intensity = normalize_image(intensity) intensity_inv = 1.0 - intensity show_image(intensity_inv); plt.axis('off'); plt.savefig('../presentation/pics/vessels/3.png') vessels = modified_gabor_convolve(intensity_inv, sigma) show_image(vessels); plt.axis('off'); plt.savefig('../presentation/pics/vessels/4.png') # Thresholding result gabor_thresholded = vessels > np.percentile(vessels.ravel(), gabor_perc) gabor_thresholded = skimage.morphology.remove_small_objects(gabor_thresholded, min_size=200) show_image(gabor_thresholded); plt.axis('off'); plt.savefig('../presentation/pics/vessels/5.png') return gabor_thresholded
def hemorrhages_detect(img): moat = moat_operator(img, sigma=5.0) show_image(moat); plt.axis('off'); plt.savefig('../presentation/pics/hemorrhages/1.png') m_clahe = skimage.exposure.equalize_adapthist(moat) m_med = mh.median_filter(m_clahe, Bc=np.ones((5, 5))) show_image(m_med); plt.axis('off'); plt.savefig('../presentation/pics/hemorrhages/2.png') threshold_global_otsu = skimage.filters.threshold_otsu(m_med) print 'Otsu threshold:', threshold_global_otsu segmented = m_med <= 0.9 * threshold_global_otsu show_image(segmented); plt.axis('off'); plt.savefig('../presentation/pics/hemorrhages/3.png') #vessels = od.detect_vessels_3(img) vessels = detect_vessels_3(m_med, one_channel_img=True, gabor_perc=88.0) vessels = skimage.morphology.remove_small_objects(vessels, min_size=150) segmented[mh.dilate(vessels, Bc=np.ones((10, 10)))] = False show_image(segmented); plt.axis('off'); plt.savefig('../presentation/pics/hemorrhages/4.png') segm_label = skimage.measure.label(segmented) segm_eccen = np.array(map(attrgetter('eccentricity'), skimage.measure.regionprops(segm_label + 1))) segm_area = cl_areas(segm_label) #segm_circ = cf.cl_circularity(segm_label) segm_filtered = leave_segments_by_mask(segm_label, (segm_eccen < 0.85) & (segm_area < 30000), replace_value=0) != 0 return segm_filtered
selected |= (edges_fzw == segm) segm_mean.append(img[(edges_fzw == segm) & (img < 0.95)].mean()) plt.figure(figsize=(15, 10)) plt.title("Mean color of segments") plt.plot(segm_mean) all_mean = img[selected & (img < 0.95)].mean() all_var = img[selected & (img < 0.95)].var() #print all_mean, all_var #all_mean = np.mean(segm_mean) #all_var = np.var(segm_mean) #print all_mean, all_var segments_to_leave_2 = [] for i, el in enumerate(segm_mean): #if abs(el - all_mean) < mean_color_threshold: if abs(el - all_mean) < 15 * all_var: segments_to_leave_2.append(segments_to_leave_1[i]) blanked = leave_segments(img, edges_fzw, segments_to_leave_2, img_orig) #print "error for train image: ", rmse(y_train[0], X_train[0]) #print "error for train image: ", rmse(y_train[0], blanked) #show_image(blanked) return blanked img_idx = 0 blanked = blank_by_edges_and_fzw(X_train[img_idx], X_train_orig[img_idx]) print "error for train image: ", rmse(y_train[img_idx], X_train[img_idx]) print "error for train image: ", rmse(y_train[img_idx], blanked) show_image(blanked)
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