def test_smallest_selem16(): # check that min, max and mean returns identity if structuring element # contains only central pixel image = np.zeros((5, 5), dtype=np.uint16) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 elem = np.array([[1]], dtype=np.uint8) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out) rank.minimum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out) rank.maximum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out)
def test_empty_selem(): # check that min, max and mean returns zeros if structuring element is # empty image = np.zeros((5, 5), dtype=np.uint16) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) res = np.zeros_like(image) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 elem = np.array([[0, 0, 0], [0, 0, 0]], dtype=np.uint8) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out) rank.minimum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out) rank.maximum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out)
def test_selem_dtypes(): image = np.zeros((5, 5), dtype=np.uint8) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 for dtype in (np.uint8, np.uint16, np.int32, np.int64, np.float32, np.float64): elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=dtype) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out) rank.mean_percentile(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out)
def test_random_sizes(): # make sure the size is not a problem niter = 10 elem = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8) for m, n in np.random.random_integers(1, 100, size=(10, 2)): mask = np.ones((m, n), dtype=np.uint8) image8 = np.ones((m, n), dtype=np.uint8) out8 = np.empty_like(image8) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=0, shift_y=0) assert_array_equal(image8.shape, out8.shape) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=+1, shift_y=+1) assert_array_equal(image8.shape, out8.shape) image16 = np.ones((m, n), dtype=np.uint16) out16 = np.empty_like(image8, dtype=np.uint16) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=0, shift_y=0) assert_array_equal(image16.shape, out16.shape) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=+1, shift_y=+1) assert_array_equal(image16.shape, out16.shape) rank.mean_percentile(image=image16, mask=mask, out=out16, selem=elem, shift_x=0, shift_y=0, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape) rank.mean_percentile(image=image16, mask=mask, out=out16, selem=elem, shift_x=+1, shift_y=+1, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape)
def test_random_sizes(): # make sure the size is not a problem niter = 10 elem = np.array([[1, 1, 1], [1, 1, 1], [1, 1, 1]], dtype=np.uint8) for m, n in np.random.random_integers(1, 100, size=(10, 2)): mask = np.ones((m, n), dtype=np.uint8) image8 = np.ones((m, n), dtype=np.uint8) out8 = np.empty_like(image8) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=0, shift_y=0) assert_array_equal(image8.shape, out8.shape) rank.mean(image=image8, selem=elem, mask=mask, out=out8, shift_x=+1, shift_y=+1) assert_array_equal(image8.shape, out8.shape) image16 = np.ones((m, n), dtype=np.uint16) out16 = np.empty_like(image8, dtype=np.uint16) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=0, shift_y=0) assert_array_equal(image16.shape, out16.shape) rank.mean(image=image16, selem=elem, mask=mask, out=out16, shift_x=+1, shift_y=+1) assert_array_equal(image16.shape, out16.shape) rank.percentile_mean(image=image16, mask=mask, out=out16, selem=elem, shift_x=0, shift_y=0, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape) rank.percentile_mean(image=image16, mask=mask, out=out16, selem=elem, shift_x=+1, shift_y=+1, p0=.1, p1=.9) assert_array_equal(image16.shape, out16.shape)
def substract_background(data, radius, background='mean', show=0): dmin, dmax = data.min(), data.max() tmp = ((data - dmin) / (dmax - dmin) * 255).astype(np.uint8) lforeground = rank.minimum(tmp, disk(radius // 2)) if background == 'mean': lbackground = rank.mean(lforeground, disk(radius * 4)).astype(int) elif background == 'median': lbackground = rank.median(lforeground, disk(radius * 4)).astype(int) elif background == 'maximum': lbackground = rank.maximum(lforeground, disk(radius * 4)).astype(int) else: lbackground = rank.modal(lforeground, disk(radius * 4)).astype(int) rdata = (lforeground - lbackground) if show > 2: fig, axes = plt.subplots(ncols=3, nrows=1, figsize=(30, 10)) for ax, im, title in zip(axes, [lforeground, lbackground, rdata], ['Foreground', 'Background', 'Substraction']): ax.imshow(im, 'optiona') ax.set_xticks([]) ax.set_yticks([]) ax.set_title(title) plt.show() return rdata
def split_object(self, labeled_image): """ split object when it's necessary """ labeled_image = labeled_image.astype(np.uint16) labeled_mask = np.zeros_like(labeled_image, dtype=np.uint16) labeled_mask[labeled_image != 0] = 1 #ift structuring element about center point. This only affects eccentric structuring elements (i.e. selem with even num=============================== labeled_image = skr.median(labeled_image, skm.disk(4)) labeled_mask = np.zeros_like(labeled_image, dtype=np.uint16) labeled_mask[labeled_image != 0] = 1 distance = scipym.distance_transform_edt(labeled_image).astype(np.uint16) #======================================================================= # binary = np.zeros(np.shape(labeled_image)) # binary[labeled_image > 0] = 1 #======================================================================= distance = skr.mean(distance, skm.disk(15)) l_max = skr.maximum(distance, skm.disk(5)) #l_max = skf.peak_local_max(distance, indices=False,labels=labeled_image, footprint=np.ones((3,3))) l_max = l_max - distance <= 0 l_max = skr.maximum(l_max.astype(np.uint8), skm.disk(6)) marker = ndimage.label(l_max)[0] split_image = skm.watershed(-distance, marker) split_image[split_image[0,0] == split_image] = 0 return split_image
def test_selem_dtypes(): image = np.zeros((5, 5), dtype=np.uint8) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 for dtype in (np.uint8, np.uint16, np.int32, np.int64, np.float32, np.float64): elem = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=dtype) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out) rank.percentile_mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(image, out)
def test_16bit(): image = np.zeros((21, 21), dtype=np.uint16) selem = np.ones((3, 3), dtype=np.uint8) for bitdepth in range(17): value = 2 ** bitdepth - 1 image[10, 10] = value assert rank.minimum(image, selem)[10, 10] == 0 assert rank.maximum(image, selem)[10, 10] == value assert rank.mean(image, selem)[10, 10] == int(value / selem.size)
def test_16bit(): image = np.zeros((21, 21), dtype=np.uint16) selem = np.ones((3, 3), dtype=np.uint8) for bitdepth in range(17): value = 2**bitdepth - 1 image[10, 10] = value assert rank.minimum(image, selem)[10, 10] == 0 assert rank.maximum(image, selem)[10, 10] == value assert rank.mean(image, selem)[10, 10] == int(value / selem.size)
def rolling_ball_float_background(float_array, radius, smoothing, ball, jit): """ Create background for a float image by rolling a ball over the image """ shrink = ball.shrink_factor > 1 if smoothing: float_array = rank.mean(float_array, selem=disk(3)) pixels = roll_ball(ball, float_array.astype('float32'), jit) return numpy.reshape(pixels, float_array.shape)
def test_empty_selem(): # check that min, max and mean returns zeros if structuring element is empty image = np.zeros((5, 5), dtype=np.uint16) out = np.zeros_like(image) mask = np.ones_like(image, dtype=np.uint8) res = np.zeros_like(image) image[2, 2] = 255 image[2, 3] = 128 image[1, 2] = 16 elem = np.array([[0, 0, 0], [0, 0, 0]], dtype=np.uint8) rank.mean(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out) rank.minimum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out) rank.maximum(image=image, selem=elem, out=out, mask=mask, shift_x=0, shift_y=0) assert_array_equal(res, out)
def _segment_edge_areas(edges, disk_size=9, mean_threshold=200, min_object_size=500): """ Takes a greyscale image (with brighter colors corresponding to edges) and returns a binary image where white indicates an area with high edge density and black indicates low density. """ # Convert the greyscale edge information into black and white (ie binary) image threshold = threshold_otsu(edges) # Filter out the edge data below the threshold, effectively removing some noise raw_channel_areas = edges <= threshold # Smooth out the data channel_areas = rank.mean(raw_channel_areas, disk(disk_size)) < mean_threshold # Remove specks and blobs that are the result of artifacts clean_channel_areas = remove_small_objects(channel_areas, min_size=min_object_size) # Fill in any areas that are completely surrounded by the areas (hopefully) covering the channels return ndimage.binary_fill_holes(clean_channel_areas)
def getImg(symbol): I = Image.new("L", (30, 30)) for stroke in symbol.strokes: p = stroke.asPoints() draw = ImageDraw.Draw(I) draw.line(p, fill=255) img = NP.asarray(list(I.getdata()), dtype=NP.uint8) img = NP.reshape(img, (I.size[1], I.size[0])) if (img.max() > 0): img = img / img.max() # if(min(img.shape)>2): img = rank.mean(img, selem=square(1)) img = binary_closing(img, selem=square(1)) if (img.max() > 0): img = img / img.max() # scale = max(img.shape)/img.shape[0] # if(scale!=1): # img = rescale(img,scale) img[img >= 0.5] = 1 img[img < 0.5] = 0 return (img)
wide), a small filter radius is sufficient. As the radius is increasing, objects with a bigger size are filtered as well, such as the camera tripod. The median filter is commonly used for noise removal because borders are preserved. Image smoothing ================ The example hereunder shows how a local **mean** smoothes the camera man image. """ from skimage.filter.rank import mean fig = plt.figure(figsize=[10, 7]) loc_mean = mean(nima, disk(10)) plt.subplot(1, 2, 1) plt.imshow(ima, cmap=plt.cm.gray, vmin=0, vmax=255) plt.xlabel('original') plt.subplot(1, 2, 2) plt.imshow(loc_mean, cmap=plt.cm.gray, vmin=0, vmax=255) plt.xlabel('local mean $r=10$') """ .. image:: PLOT2RST.current_figure One may be interested in smoothing an image while preserving important borders (median filters already achieved this), here we use the **bilateral** filter that restricts the local neighborhood to pixel having a greylevel similar to the central one.
median filter is often used for noise removal because borders are preserved and e.g. salt and pepper noise typically does not distort the gray-level. Image smoothing ================ The example hereunder shows how a local **mean** filter smooths the camera man image. """ from skimage.filter.rank import mean fig = plt.figure(figsize=[10, 7]) loc_mean = mean(noisy_image, disk(10)) plt.subplot(1, 2, 1) plt.imshow(noisy_image, vmin=0, vmax=255) plt.title('Original') plt.axis('off') plt.subplot(1, 2, 2) plt.imshow(loc_mean, vmin=0, vmax=255) plt.title('Local mean $r=10$') plt.axis('off') """ .. image:: PLOT2RST.current_figure One may be interested in smoothing an image while preserving important borders
""" import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk from skimage.filter import rank image = (data.coins()).astype(np.uint16) * 16 selem = disk(20) percentile_result = rank.mean_percentile(image, selem=selem, p0=.1, p1=.9) bilateral_result = rank.mean_bilateral(image, selem=selem, s0=500, s1=500) normal_result = rank.mean(image, selem=selem) fig, axes = plt.subplots(nrows=3, figsize=(8, 10)) ax0, ax1, ax2 = axes ax0.imshow(np.hstack((image, percentile_result))) ax0.set_title('Percentile mean') ax0.axis('off') ax1.imshow(np.hstack((image, bilateral_result))) ax1.set_title('Bilateral mean') ax1.axis('off') ax2.imshow(np.hstack((image, normal_result))) ax2.set_title('Local mean')
median filter is often used for noise removal because borders are preserved and e.g. salt and pepper noise typically does not distort the gray-level. Image smoothing ================ The example hereunder shows how a local **mean** filter smooths the camera man image. """ from skimage.filter.rank import mean fig = plt.figure(figsize=[10, 7]) loc_mean = mean(noisy_image, disk(10)) plt.subplot(1, 2, 1) plt.imshow(noisy_image, vmin=0, vmax=255) plt.title('Original') plt.axis('off') plt.subplot(1, 2, 2) plt.imshow(loc_mean, vmin=0, vmax=255) plt.title('Local mean $r=10$') plt.axis('off') """ .. image:: PLOT2RST.current_figure
frequencies remain untouched. """ import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk from skimage.filter import rank image = (data.coins()).astype(np.uint16) * 16 selem = disk(20) percentile_result = rank.mean_percentile(image, selem=selem, p0=.1, p1=.9) bilateral_result = rank.mean_bilateral(image, selem=selem, s0=500, s1=500) normal_result = rank.mean(image, selem=selem) fig, axes = plt.subplots(nrows=3, figsize=(8, 10)) ax0, ax1, ax2 = axes ax0.imshow(np.hstack((image, percentile_result))) ax0.set_title('Percentile mean') ax0.axis('off') ax1.imshow(np.hstack((image, bilateral_result))) ax1.set_title('Bilateral mean') ax1.axis('off') ax2.imshow(np.hstack((image, normal_result))) ax2.set_title('Local mean') ax2.axis('off')
complete image (background and details). Bilateral mean exhibits a high filtering rate for continuous area (i.e. background) while higher image frequencies remain untouched. """ import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk import skimage.filter.rank as rank a16 = (data.coins()).astype(np.uint16) * 16 selem = disk(20) f1 = rank.percentile_mean(a16, selem=selem, p0=.1, p1=.9) f2 = rank.bilateral_mean(a16, selem=selem, s0=500, s1=500) f3 = rank.mean(a16, selem=selem) # display results fig, axes = plt.subplots(nrows=3, figsize=(15, 10)) ax0, ax1, ax2 = axes ax0.imshow(np.hstack((a16, f1))) ax0.set_title('percentile mean') ax1.imshow(np.hstack((a16, f2))) ax1.set_title('bilateral mean') ax2.imshow(np.hstack((a16, f3))) ax2.set_title('local mean') plt.show()