def boost_contrast_local(image, radius=3): """Returns a new image with contrast boosted locally. Args: image: assumed to be grayscale. radius: the size of the radial neighborhood to use. """ return rank.enhance_contrast(image, disk(radius))
def prepare_image(filename): img = io.imread(filename) img_out = rgb2gray(img) img_out = transform.resize(img_out, (IMAGE_DIM,IMAGE_DIM)) img_out = enhance_contrast(img_out, disk(5)) t = lambda x : 1.0 - x/255. t = np.vectorize(t) img_out = t(img_out) image = np.empty((1, 1, IMAGE_DIM,IMAGE_DIM), dtype=np.float32) for i in range(0,IMAGE_DIM): for j in range(0,IMAGE_DIM): image[:,:,i,j] = img_out[i,j] return image
def prepare_image(filename): img = io.imread(filename) img_out = rgb2gray(img) img_out = transform.resize(img_out, (IMAGE_DIM, IMAGE_DIM)) img_out = enhance_contrast(img_out, disk(5)) t = lambda x: 1.0 - x / 255. t = np.vectorize(t) img_out = t(img_out) image = np.empty((1, 1, IMAGE_DIM, IMAGE_DIM), dtype=np.float32) for i in range(0, IMAGE_DIM): for j in range(0, IMAGE_DIM): image[:, :, i, j] = img_out[i, j] return image
def label_from_thresh(frame, thresh, parameters): smooth = parameters['smooth'] min_distance = np.int(parameters['nuc_distance']) image = rank.median(frame, disk(smooth)) image = rank.enhance_contrast(image, disk(smooth)) im_max = image.max() if im_max < thresh: return np.zeros(image.shape, dtype=np.int32) else: image = image > thresh distance = ndimage.distance_transform_edt(image) local_maxi = peak_local_max(distance, footprint=disk(min_distance), #min_distance=min_distance, indices=False, labels=image) markers = ndimage.label(local_maxi)[0] return watershed(-distance, markers, mask=image)
ax.axis('off') """ .. image:: PLOT2RST.current_figure The morphological contrast enhancement filter replaces the central pixel by the local maximum if the original pixel value is closest to local maximum, otherwise by the minimum local. """ from skimage.filter.rank import enhance_contrast noisy_image = img_as_ubyte(data.camera()) enh = enhance_contrast(noisy_image, disk(5)) fig = plt.figure(figsize=[10, 7]) plt.subplot(2, 2, 1) plt.imshow(noisy_image, cmap=plt.cm.gray) plt.title('Original') plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(enh, cmap=plt.cm.gray) plt.title('Local morphological contrast enhancement') plt.axis('off') plt.subplot(2, 2, 2) plt.imshow(noisy_image[200:350, 350:450], cmap=plt.cm.gray) plt.axis('off')
from skimage import data from skimage.morphology import disk from skimage.filter.rank import autolevel, enhance_contrast # Load test image ima = data.camera() # Stretch image contrast locally auto = enhance_contrast(ima, disk(20))
""" .. image:: PLOT2RST.current_figure The morphological contrast enhancement filter replaces the central pixel by the local maximum if the original pixel value is closest to local maximum, otherwise by the minimum local. """ from skimage.filter.rank import enhance_contrast noisy_image = img_as_ubyte(data.camera()) enh = enhance_contrast(noisy_image, disk(5)) fig = plt.figure(figsize=[10, 7]) plt.subplot(2, 2, 1) plt.imshow(noisy_image, cmap=plt.cm.gray) plt.title('Original') plt.axis('off') plt.subplot(2, 2, 3) plt.imshow(enh, cmap=plt.cm.gray) plt.title('Local morphological contrast enhancement') plt.axis('off') plt.subplot(2, 2, 2) plt.imshow(noisy_image[200:350, 350:450], cmap=plt.cm.gray) plt.axis('off')
def preprocessing(image, smooth_size, folder): """ 'The image low contrast and under segmentation problem is not yet addressed by most of the researchers' 'Other researchers also proposed different method to remedy the problem of watershed. Li, El- moataz, Fadili, and Ruan, S. (2003) proposed an improved image segmentation approach based on level set and mathematical morphology' THE SPHERES MUST BE ALMOST ALONG THE SAME PLANES IN Z DIRECTION IF THEY ARE TOUCHING AND OVERLAP, WHILE BEING ALMOST MERGED IT IS IMPOSSIBLE TO RESOLVE THEM ONE IDEA MIGHT BE TO DETECT CENTRES ALONG ONE AXIS AND THEN ANOTHER AFTER ALL THE CENTRES WERE FOUND COMBINE THEM SOMEHOW... """ from skimage.restoration import denoise_tv_chambolle dim = int(image.shape[0] / 50.) smoothed = rank.median(image, disk(smooth_size)) #smoothed = denoise_tv_chambolle(image, weight=0.002) smoothed = rank.enhance_contrast(smoothed, disk(smooth_size)) pl.subplot(2, 3, 1) pl.title("after median") pl.imshow(smoothed) pl.gray() # If after smoothing the "dot" disappears # use the image value # TODO: wat do with thresh? try: im_max = smoothed.max() thresh = threshold_otsu(image) except: im_max = image.max() thresh = threshold_otsu(image) if im_max < thresh: labeled = np.zeros(smoothed.shape, dtype=np.int32) else: binary = smoothed > thresh # TODO: this array size is the fault of errors bin_open = binary_opening(binary, np.ones((dim, dim)), iterations=5) bin_close = binary_closing(bin_open, np.ones((5,5)), iterations=5) pl.subplot(2, 3, 2) pl.title("threshold") pl.imshow(binary, interpolation='nearest') pl.subplot(2, 3, 3) pl.title("opening") pl.imshow(bin_open, interpolation='nearest') pl.subplot(2, 3, 4) pl.title("closing") pl.imshow(bin_close, interpolation='nearest') distance = ndimage.distance_transform_edt(bin_open) local_maxi = peak_local_max(distance, indices=False, labels=bin_open) markers = ndimage.label(local_maxi)[0] labeled = watershed(-distance, markers, mask=bin_open) pl.subplot(2, 3, 5) pl.title("label") pl.imshow(labeled) #pl.show() pl.savefig(folder) pl.close('all') #misc.imsave(folder, labeled) # labels_rw = random_walker(bin_close, markers, mode='cg_mg') # # pl.imshow(labels_rw, interpolation='nearest') # pl.show() return labeled
def preprocessing(image, smooth_size, folder): """ 'The image low contrast and under segmentation problem is not yet addressed by most of the researchers' 'Other researchers also proposed different method to remedy the problem of watershed. Li, El- moataz, Fadili, and Ruan, S. (2003) proposed an improved image segmentation approach based on level set and mathematical morphology' THE SPHERES MUST BE ALMOST ALONG THE SAME PLANES IN Z DIRECTION IF THEY ARE TOUCHING AND OVERLAP, WHILE BEING ALMOST MERGED IT IS IMPOSSIBLE TO RESOLVE THEM ONE IDEA MIGHT BE TO DETECT CENTRES ALONG ONE AXIS AND THEN ANOTHER AFTER ALL THE CENTRES WERE FOUND COMBINE THEM SOMEHOW... """ from skimage.restoration import denoise_tv_chambolle dim = int(image.shape[0] / 50.) smoothed = rank.median(image, disk(smooth_size)) #smoothed = denoise_tv_chambolle(image, weight=0.002) smoothed = rank.enhance_contrast(smoothed, disk(smooth_size)) pl.subplot(2, 3, 1) pl.title("after median") pl.imshow(smoothed) pl.gray() # If after smoothing the "dot" disappears # use the image value # TODO: wat do with thresh? try: im_max = smoothed.max() thresh = threshold_otsu(image) except: im_max = image.max() thresh = threshold_otsu(image) if im_max < thresh: labeled = np.zeros(smoothed.shape, dtype=np.int32) else: binary = smoothed > thresh # TODO: this array size is the fault of errors bin_open = binary_opening(binary, np.ones((dim, dim)), iterations=5) bin_close = binary_closing(bin_open, np.ones((5, 5)), iterations=5) pl.subplot(2, 3, 2) pl.title("threshold") pl.imshow(binary, interpolation='nearest') pl.subplot(2, 3, 3) pl.title("opening") pl.imshow(bin_open, interpolation='nearest') pl.subplot(2, 3, 4) pl.title("closing") pl.imshow(bin_close, interpolation='nearest') distance = ndimage.distance_transform_edt(bin_open) local_maxi = peak_local_max(distance, indices=False, labels=bin_open) markers = ndimage.label(local_maxi)[0] labeled = watershed(-distance, markers, mask=bin_open) pl.subplot(2, 3, 5) pl.title("label") pl.imshow(labeled) #pl.show() pl.savefig(folder) pl.close('all') #misc.imsave(folder, labeled) # labels_rw = random_walker(bin_close, markers, mode='cg_mg') # # pl.imshow(labels_rw, interpolation='nearest') # pl.show() return labeled