def segment_veins(img, test=False): to_plot = [] """ Segmentamos venas """ img_inverted = skimage.util.invert(img) if test: to_plot.append(("inverted green channel", img_inverted)) img_inverted = exposure.equalize_adapthist(img_inverted, clip_limit=0.5) if test: to_plot.append(("high contrast", img_inverted)) mask_invert = th.mask_otsu(img_inverted) if test: to_plot.append(("Global Otsu", th.apply(img, mask_invert))) """ creamos mascara para eliminar fondo """ (ancho, alto) = img.shape rr, cc = ellipse(ancho / 2, alto / 2, (ancho / 2) - 5, (alto / 2) - 5) mask_background = np.zeros((ancho, alto)) > 0 mask_background[rr, cc] = 1 mask_invert = np.logical_and(mask_invert, mask_background) mask_invert = skimage.morphology.remove_small_objects(mask_invert, 200) if test: to_plot.append(("Mask without background", mask_invert)) if test: vi.plot_multy(to_plot, 1, 4, 'Veins segmentation') return mask_invert, mask_background
def clean(input_file, thresh_val = [250, 245, 240, 230, 225, 220], window_size = 5, kernel_size = 5): # Ensures that both quality and window_size parameters are integers window_size = int(window_size) kernel_size = int(kernel_size) #Checking arguments and raising expected exceptions check_arguments(input_file, window_size, kernel_size) # Loading the image image = input_file if isinstance(image, str): image = cv.imread(input_file) # Applying Grayscale, Gaussian and median blur and erode image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image = cv.GaussianBlur(image, (window_size, window_size), 0) image = cv.medianBlur(image, window_size) image = cv.erode(image, (kernel_size, kernel_size)) # Applying threshold list results = th.apply(image, thresh_val) return results
def closest_prop(img, mask): (lbls, num) = label(mask, return_num=True) max_mean = 0.0 max_mask = None if num > 1: for lb in range(num + 1): current_mean = 0 current = th.apply(img, lbls == lb) current_mean = np.sum(current) / np.count_nonzero(current) if max_mean < current_mean: max_mean = current_mean max_mask = current return max_mask > 0 else: return mask
def clean(input_file, thresh_val = [225, 220, 215, 210, 205, 200], window_size = 3): # Ensures that window_size parameter is integer window_size = int(window_size) # Checking arguments and raising expected exceptions check_arguments(window_size) # Loading the image image = iu.get_image(input_file) # Gray-scale and Gaussian Blur image = cv.cvtColor(image, cv.COLOR_BGR2GRAY) image = cv.GaussianBlur(image, (window_size, window_size), 0) # Applying threshold list results = th.apply(image, thresh_val) return results
def handmade_p_tile_method(img, op="disc", test=False): if op == "disc" or op == "cup": to_plot = [] img_red = img[:, :, 0] if test: to_plot.append(("Red Channel", img_red)) img_red = dilation(img_red, disk(3)) if test: to_plot.append(("Gaussing", img_red)) img_red = enhance_contrast(img_red, disk(10)) if test: to_plot.append(("Enhace_contrast", img_red)) mask = f.hand_made(img_red, 10, 2) if test: to_plot.append(("P-tile", th.apply(img_red, mask))) mask = binary_opening(mask, disk(7)) mask = skimage.morphology.remove_small_objects(mask, 1700) if test: to_plot.append( ("Opening and remove small objects", th.apply(img_red, mask))) props = msr.props(img_red, mask)[0] minr, minc, maxr, maxc = props.bbox img_cut = img[minr:maxr, minc:maxc] if test: to_plot.append(("Binary opening", th.apply(img_red, mask))) if test: vi.plot_multy(to_plot, 2, 3, 'P-tile') if op == "cup": minr, minc, maxr, maxc = props.bbox img_green = img[minr:maxr, minc:maxc, 1] to_plot = [] if test: to_plot.append(("green channel", img_green)) v_mask, _ = segment_veins(img_green, test) img_aux = closing(img_green, disk(6)) img_aux = dilation(img_aux, disk(6)) img_aux2 = dilation(img_green, disk(3)) if test: to_plot.append(("closed + dilated", img_aux)) to_plot.append(("dilated", img_aux2)) img_v_closed = th.apply(img_aux, v_mask) img_t_dilated = th.apply(img_aux2, v_mask == False) if test: to_plot.append(("veins part", img_v_closed)) to_plot.append(("target part", img_t_dilated)) img_green = img_v_closed + img_t_dilated if test: to_plot.append(("without veins", img_green)) img_green = dilation(img_green, disk(6)) img_green = enhance_contrast(img_green, disk(10)) if test: to_plot.append(("dilation + contrast", img_green)) mask = f.hand_made(img_green, 10, 2, smallest=0) if test: to_plot.append(("P-tile", th.apply(img_green, mask))) mask1 = mask mask = np.zeros(img[:, :, 1].shape) mask[minr:maxr, minc:maxc] = mask1 if test: vi.plot_multy(to_plot, 4, 4, 'Otsu Local chain') return (mask, img_cut, props)
def otsu_local_method(img, op="disc", test=False): if op == "disc" or op == "cup": to_plot = [] img_red = img[:, :, 0] img_red = skimage.util.img_as_ubyte(img_red) if test: to_plot.append(("red_chan", img_red)) img_red = skimage.filters.gaussian(img_red, 4.2) if test: to_plot.append(("gaussian f", img_red)) img_red = enhance_contrast(img_red, disk(3)) if test: to_plot.append(("enhace_contrast", img_red)) """ Local Otsu """ t_loc_otsu = otsu(img_red, disk(20)) mask = img_red >= t_loc_otsu if test: to_plot.append(("Otsu local", th.apply(img_red, mask))) mask = binary_opening(mask, disk(2)) mask = binary_closing(mask, disk(4)) if test: to_plot.append(("binary open-close", th.apply(img_red, mask))) """ Chusing brightest region """ mask = msr.closest_prop(img_red, mask) if test: to_plot.append(("brightest region", th.apply(img_red, mask))) maskfinal = binary_closing(mask, disk(20)) if test: to_plot.append(("binary closing", th.apply(img_red, maskfinal))) img_red = th.apply(img_red, maskfinal) props = msr.props(img_red, maskfinal)[0] minr, minc, maxr, maxc = props.bbox img_cut = img[minr:maxr, minc:maxc] if test: vi.plot_multy(to_plot, 3, 3, 'Otsu Local chain') if op == "cup": minr, minc, maxr, maxc = props.bbox img_green = img[minr:maxr, minc:maxc, 1] to_plot = [] if test: to_plot.append(("green channel", img_green)) v_mask, _ = segment_veins(img_green, test) img_aux = closing(img_green, disk(6)) img_aux = dilation(img_aux, disk(6)) img_aux2 = dilation(img_green, disk(3)) if test: to_plot.append(("closed + dilated", img_aux)) to_plot.append(("dilated", img_aux2)) img_v_closed = th.apply(img_aux, v_mask) img_t_dilated = th.apply(img_aux2, v_mask == False) if test: to_plot.append(("veins part", img_v_closed)) to_plot.append(("target part", img_t_dilated)) img_green = img_v_closed + img_t_dilated if test: to_plot.append(("img_green", img_green)) """ Local Otsu """ t_loc_otsu = otsu(img_green, disk(20)) mask = img_green >= t_loc_otsu mask = msr.closest_prop(img_green, mask) if test: to_plot.append(("Otsu local", th.apply(img_green, mask))) mask1 = mask mask = np.zeros(img[:, :, 1].shape) mask[minr:maxr, minc:maxc] = mask1 if test: vi.plot_multy(to_plot, 2, 4, 'cup') return (mask, img_cut, props)
def sobel_watershed_method(img, op="disc", veins=False, test=False): if op == "disc" or op == "cup": to_plot = [] img_red = img[:, :, 0] if test: to_plot.append(("Red Channel", img_red)) img_red = skimage.util.img_as_ubyte(img_red) img_red = skimage.filters.gaussian(img_red, 0.1) if test: to_plot.append(("Gaussian Filter", img_red)) img_red = enhance_contrast(img_red, disk(6)) if test: to_plot.append(("Enhace Contrast", img_red)) elevation_map = sobel(img_red) if test: to_plot.append(("gradientes", elevation_map)) markers = np.zeros_like(img_red) s2 = f.target_set_mean(img_red, 8.5) markers[img_red < 150] = 1 markers[img_red > s2] = 2 seg_img = segmentation.watershed(elevation_map, markers) mask = (seg_img - 1) > 0 if test: to_plot.append(("Sobel + WaterShed", seg_img)) mask = binary_opening(mask, disk(2)) mask = skimage.morphology.remove_small_objects(mask, 400) if test: to_plot.append(("Removing small objects", th.apply(img_red, mask))) mask = binary_closing(mask, disk(6)) if test: to_plot.append(("Closing Region", th.apply(img[:, :, 0], mask))) mask = skimage.morphology.remove_small_objects(mask, 1700) if test: to_plot.append( ("Removing Big Objects", th.apply(img[:, :, 0], mask))) mask = binary_closing(mask, disk(6)) mask = msr.closest_prop(img[:, :, 0], mask) if test: to_plot.append( ("Removing non brighter region", th.apply(img[:, :, 0], mask))) mask = binary_dilation(mask, disk(3)) mask = binary_closing(mask, disk(12)) if test: to_plot.append( ("Dilate result region", th.apply(img[:, :, 0], mask))) props = msr.props(img_red, mask)[0] minr, minc, maxr, maxc = props.bbox img_cut = img[minr:maxr, minc:maxc] if test: vi.plot_multy(to_plot, 3, 4, 'Sobel Watershed') if op == "cup": minr, minc, maxr, maxc = props.bbox img_green = img[minr:maxr, minc:maxc, 1] to_plot = [] columns = 1 if test: to_plot.append(("green channel", img_green)) if not veins: columns = 4 v_mask, _ = segment_veins(img_green, test) img_aux = closing(img_green, disk(6)) img_aux = dilation(img_aux, disk(6)) img_aux2 = dilation(img_green, disk(3)) if test: to_plot.append(("closed + dilated", img_aux)) to_plot.append(("dilated", img_aux2)) img_v_closed = th.apply(img_aux, v_mask) img_t_dilated = th.apply(img_aux2, v_mask == False) if test: to_plot.append(("veins part", img_v_closed)) to_plot.append(("target part", img_t_dilated)) img_green = img_v_closed + img_t_dilated if test: to_plot.append(("without veins", img_green)) img_green = dilation(img_green, disk(6)) img_green = enhance_contrast(img_green, disk(10)) elevation_map = sobel(img_green) markers = np.zeros_like(img_green) s2 = f.target_set_mean(img_green, 8.5) markers[img_green < 150] = 1 markers[img_green > s2] = 2 seg_img = segmentation.watershed(elevation_map, markers) mask = (seg_img - 1) > 0 if test: to_plot.append(("P-tile", th.apply(img_green, mask))) mask1 = mask mask = np.zeros(img[:, :, 1].shape) mask[minr:maxr, minc:maxc] = mask1 if test: vi.plot_multy(to_plot, 2, columns, 'cup') return (mask, img_cut, props)