def preProcess(img): img[:,:,0] = mean_bilateral(img[:,:,0], morphology.disk(20), s0=10, s1=10) img[:,:,1] = mean_bilateral(img[:,:,1], morphology.disk(20), s0=10, s1=10) img[:,:,2] = mean_bilateral(img[:,:,2], morphology.disk(20), s0=10, s1=10) return img
def test_bilateral(): image = np.zeros((21, 21), dtype=np.uint16) selem = np.ones((3, 3), dtype=np.uint8) image[10, 10] = 1000 image[10, 11] = 1010 image[10, 9] = 900 assert rank.mean_bilateral(image, selem, s0=1, s1=1)[10, 10] == 1000 assert rank.pop_bilateral(image, selem, s0=1, s1=1)[10, 10] == 1 assert rank.mean_bilateral(image, selem, s0=11, s1=11)[10, 10] == 1005 assert rank.pop_bilateral(image, selem, s0=11, s1=11)[10, 10] == 2
def test_bilateral(self): image = np.zeros((21, 21), dtype=np.uint16) footprint = np.ones((3, 3), dtype=np.uint8) image[10, 10] = 1000 image[10, 11] = 1010 image[10, 9] = 900 kwargs = dict(s0=1, s1=1) assert rank.mean_bilateral(image, footprint, **kwargs)[10, 10] == 1000 assert rank.pop_bilateral(image, footprint, **kwargs)[10, 10] == 1 kwargs = dict(s0=11, s1=11) assert rank.mean_bilateral(image, footprint, **kwargs)[10, 10] == 1005 assert rank.pop_bilateral(image, footprint, **kwargs)[10, 10] == 2
def find_blobs(filename): feature = "" raw_image = io.imread(filename) for channel in range(0, 4): if channel < 3: image = raw_image[:,:,channel] image_gray = rgb2gray(image) # Smoothing image_gray = img_as_ubyte(image_gray) image_gray = mean_bilateral(image_gray.astype(numpy.uint16), disk(20), s0=10, s1=10) # Increase contrast image_gray = exposure.equalize_adapthist(image_gray, clip_limit=0.03) # Find blobs blobs_doh = blob_doh(image_gray, min_sigma=1, max_sigma=20, threshold=.005) count = 0 for blob in blobs_doh: y, x, r = blob if (x-400)**2 + (y-400)**2 > distance: continue count = count + 1 feature = feature + " " + str(channel + 1) + ":" + str(count) return feature
def check_all(): selem = morphology.disk(1) refs = np.load( os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(self.image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(self.image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(self.image, selem)) assert_equal(refs["equalize"], rank.equalize(self.image, selem)) assert_equal(refs["gradient"], rank.gradient(self.image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(self.image, selem)) assert_equal(refs["maximum"], rank.maximum(self.image, selem)) assert_equal(refs["mean"], rank.mean(self.image, selem)) assert_equal(refs["geometric_mean"], rank.geometric_mean(self.image, selem)), assert_equal(refs["mean_percentile"], rank.mean_percentile(self.image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(self.image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(self.image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(self.image, selem)) assert_equal(refs["median"], rank.median(self.image, selem)) assert_equal(refs["minimum"], rank.minimum(self.image, selem)) assert_equal(refs["modal"], rank.modal(self.image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(self.image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(self.image, selem)) assert_equal(refs["pop"], rank.pop(self.image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(self.image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(self.image, selem)) assert_equal(refs["sum"], rank.sum(self.image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(self.image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(self.image, selem)) assert_equal(refs["threshold"], rank.threshold(self.image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(self.image, selem)) assert_equal(refs["tophat"], rank.tophat(self.image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(self.image, selem)) assert_equal(refs["entropy"], rank.entropy(self.image, selem)) assert_equal(refs["otsu"], rank.otsu(self.image, selem)) assert_equal(refs["percentile"], rank.percentile(self.image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(self.image, selem))
def bilateral_filter(self, data, kernel, kern_size, s0, s1): from skimage.morphology import disk, square from skimage.filters import rank import numpy as np # https://scikit-image.org/docs/dev/api/skimage.filters.rank.html?highlight=mean_bilateral if (kernel == "disk"): selem = disk(kern_size) if (kernel == "square"): selem = square(kern_size) data_norm = 1.0 * data / np.max(np.abs(data)) data_filt = rank.mean_bilateral(data_norm, selem=selem, s0=s0, s1=s1) return data_filt
def cal_delta_abs(image, z): delta = image - z delta = adjust_extreme(image, delta) balance = (300 + delta).astype(np.uint16) #cv2.imwrite('127.jpg' ,(127 + delta).astype(np.uint8)) #cv2.imshow('balance', balance) #blur = gaussian(balance, 1).astype(np.uint8) #g_kernel = cv2.getGaussianKernel(5, 1) #blur = cv2.filter2D(balance, 0, g_kernel) #blur = cv2.medianBlur(balance, 11) blur = median(balance, selem=disk(7)) blur = mean_bilateral(blur, disk(13)) abs = np.abs(blur - 300.0).astype(np.uint8) #cv2.imshow('abs', abs) #cv2.waitKey() return abs
def FilterBilateral(img, stdFilter=False, winh=5, winw=15, S0=20, S1=50): if stdFilter == False: winsize = (winh, winw) win = np.ones(winsize) # win=255*win img255 = np.copy(img) img = img.astype('uint8') # img=img/255. result = mean_bilateral(img, win, shift_x=False, shift_y=False, s0=S0, s1=S1) img = img255 return result else: prog = 255 * denoise_bilateral(img) return prog
def check_all(): np.random.seed(0) image = np.random.rand(25, 25) selem = morphology.disk(1) refs = np.load(os.path.join(skimage.data_dir, "rank_filter_tests.npz")) assert_equal(refs["autolevel"], rank.autolevel(image, selem)) assert_equal(refs["autolevel_percentile"], rank.autolevel_percentile(image, selem)) assert_equal(refs["bottomhat"], rank.bottomhat(image, selem)) assert_equal(refs["equalize"], rank.equalize(image, selem)) assert_equal(refs["gradient"], rank.gradient(image, selem)) assert_equal(refs["gradient_percentile"], rank.gradient_percentile(image, selem)) assert_equal(refs["maximum"], rank.maximum(image, selem)) assert_equal(refs["mean"], rank.mean(image, selem)) assert_equal(refs["mean_percentile"], rank.mean_percentile(image, selem)) assert_equal(refs["mean_bilateral"], rank.mean_bilateral(image, selem)) assert_equal(refs["subtract_mean"], rank.subtract_mean(image, selem)) assert_equal(refs["subtract_mean_percentile"], rank.subtract_mean_percentile(image, selem)) assert_equal(refs["median"], rank.median(image, selem)) assert_equal(refs["minimum"], rank.minimum(image, selem)) assert_equal(refs["modal"], rank.modal(image, selem)) assert_equal(refs["enhance_contrast"], rank.enhance_contrast(image, selem)) assert_equal(refs["enhance_contrast_percentile"], rank.enhance_contrast_percentile(image, selem)) assert_equal(refs["pop"], rank.pop(image, selem)) assert_equal(refs["pop_percentile"], rank.pop_percentile(image, selem)) assert_equal(refs["pop_bilateral"], rank.pop_bilateral(image, selem)) assert_equal(refs["sum"], rank.sum(image, selem)) assert_equal(refs["sum_bilateral"], rank.sum_bilateral(image, selem)) assert_equal(refs["sum_percentile"], rank.sum_percentile(image, selem)) assert_equal(refs["threshold"], rank.threshold(image, selem)) assert_equal(refs["threshold_percentile"], rank.threshold_percentile(image, selem)) assert_equal(refs["tophat"], rank.tophat(image, selem)) assert_equal(refs["noise_filter"], rank.noise_filter(image, selem)) assert_equal(refs["entropy"], rank.entropy(image, selem)) assert_equal(refs["otsu"], rank.otsu(image, selem)) assert_equal(refs["percentile"], rank.percentile(image, selem)) assert_equal(refs["windowed_histogram"], rank.windowed_histogram(image, selem))
img_gauss_filtered = img_as_ubyte(filter.gaussian(img_noisy, 3)) gauss_im_entropy, _, _ = original_im_histogram.information_entropy( img_gauss_filtered) _, gauss_histogram = original_im_histogram.image_histogram( img_gauss_filtered, "on") img_median_filtered = img_as_ubyte(median(img_noisy, disk(1))) median_im_entropy, _, _ = original_im_histogram.information_entropy( img_median_filtered) _, median_histogram = original_im_histogram.image_histogram( img_median_filtered, "on") img_bilateral_filtered = img_as_ubyte( mean_bilateral(img_noisy, disk(5), s0=10, s1=10)) bilateral_im_entropy, _, _ = original_im_histogram.information_entropy( img_bilateral_filtered) _, bilateral_histogram = original_im_histogram.image_histogram( img_bilateral_filtered, "on") information_mutual_original_noisy = np.round( drv.information_mutual(original_histogram, noisy_histogram), 2) information_mutual_original_noisy_filtered_by_gauss = np.round( drv.information_mutual(original_histogram, gauss_histogram), 2) information_mutual_original_noisy_filtered_by_median = np.round( drv.information_mutual(original_histogram, median_histogram), 2) information_mutual_original_noisy_filtered_by_bilateral = np.round( drv.information_mutual(original_histogram, bilateral_histogram), 2) object_finder.add_image(img)
def transform(image): selem = disk(20) image = numpy.invert(image) changed = rank.mean_bilateral(image, selem=selem, s0=500, s1=500) changed = resize(changed, (50, 50)) return changed
def mean_bilateral_skimage(img): img = to_gray(img) img = img.astype(np.uint8) edges = mean_bilateral(img, disk(20), s0=10, s1=10) return edges
def prefilter(self, img, method='median'): ps = self.settings.prefilter_settings[method] print print 'prefiltering :', method if method=='median': radius = ps['median_size'] # with vigra #filtered= vigra.filters.discMedian(img, radius) # with skimage pref = rank.median(img, disk(radius)) elif method=='avg': # with skimage se = disk(ps['avg_size']) pref = rank.mean(img, se) elif method=='bilateral': # with skimage se = disk(ps['bil_radius']) pref = rank.mean_bilateral(img, se, s0=ps['bil_lower'], s1=ps['bil_upper']) elif method=='denoise_bilateral': #skimage.filters.denoise_bilateral(image, win_size=5, sigma_range=None, sigma_spatial=1, pref = restoration.denoise_bilateral(img, ps['win_size'], ps['sigma_signal'], ps['sigma_space'], ps['bins'], mode='constant', cval=0, multichannel=False) elif method=='close_rec': se = disk(ps['close_size']) dil = morphology.dilation(img, se) rec = morphology.reconstruction(dil, img, method='erosion') # reconstruction gives back a float image (for whatever reason). pref = rec.astype(dil.dtype) elif method=='denbi_clorec': temp = restoration.denoise_bilateral(img, ps['win_size'], ps['sigma_signal'], ps['sigma_space'], ps['bins'], mode='constant', cval=0, multichannel=False) temp = 255 * temp temp = temp.astype(img.dtype) se = disk(ps['close_size']) dil = morphology.dilation(temp, se) rec = morphology.reconstruction(dil, temp, method='erosion') # reconstruction gives back a float image (for whatever reason). pref = rec.astype(img.dtype) elif method=='denbi_asfrec': temp = restoration.denoise_bilateral(img, ps['win_size'], ps['sigma_signal'], ps['sigma_space'], ps['bins'], mode='constant', cval=0, multichannel=False) temp = 255 * temp temp = temp.astype(img.dtype) se = disk(ps['close_size']) dil = morphology.dilation(temp, se) rec = morphology.reconstruction(dil, temp, method='erosion') se = disk(ps['open_size']) ero = morphology.erosion(rec, se) rec2 = morphology.reconstruction(ero, rec, method='dilation') # reconstruction gives back a float image (for whatever reason). pref = rec2.astype(img.dtype) elif method=='med_denbi_asfrec': if ps['median_size'] > 1: radius = ps['median_size'] pref = rank.median(img, disk(radius)) else: pref = img temp = restoration.denoise_bilateral(pref, ps['win_size'], ps['sigma_signal'], ps['sigma_space'], ps['bins'], mode='constant', cval=0, multichannel=False) temp = 255 * temp temp = temp.astype(img.dtype) if ps['close_size'] > 0 : se = disk(ps['close_size']) dil = morphology.dilation(temp, se) rec = morphology.reconstruction(dil, temp, method='erosion') else: rec = temp if ps['open_size'] > 0: se = disk(ps['open_size']) ero = morphology.erosion(rec, se) rec2 = morphology.reconstruction(ero, rec, method='dilation') else: rec2 = rec # reconstruction gives back a float image (for whatever reason). pref = rec2.astype(img.dtype) return pref
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 from skimage.filters 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=2, ncols=2, figsize=(8, 10), sharex=True, sharey=True) ax = axes.ravel() titles = ['Original', 'Percentile mean', 'Bilateral mean', 'Local mean'] imgs = [image, percentile_result, bilateral_result, normal_result] for n in range(0, len(imgs)): ax[n].imshow(imgs[n]) ax[n].set_title(titles[n]) ax[n].set_adjustable('box-forced')
def process_image(img, disk_radius=20): return sobel(mean_bilateral(img, disk(disk_radius)))
frequencies remain untouched. """ import numpy as np import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk from skimage.filters import rank image = (data.coins()).astype(np.uint16) * 16 selem = disk(20) percentile_result = rank.mean_percentile(image, selem=selem, p0=0.1, p1=0.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)))
from skimage.filters.rank import mean_bilateral from skimage.morphology import disk bilat = np.empty_like(img) for i, aslice in enumerate(img): bilat[i] = mean_bilateral(aslice.astype(np.uint8), disk(3), s0=15, s1=15) slicing(img, 'gray')
from skimage.morphology import disk import numpy as np import res_drawer def Canny(image, draw=False): edge = feature.canny(image, sigma=3, use_quantiles=False, low_threshold=5, high_threshold=25) if draw == True: res_drawer.binary(image, edge) return edge if __name__ == "__main__": img = cv2.imread('E:\\File\\python\\proj2\\pic\\small1.jpg', 0) #img = denoise_bilateral(img, win_size=10, multichannel=False) g = gabor(img, 50) img = mean_bilateral(img, disk(25)) d1 = sobel(img) img1 = feature.canny(img, sigma=3, use_quantiles=False, low_threshold=20, high_threshold=40) plt.imshow(img1, cmap=plt.cm.gray) plt.show()
def find_boards(img): boards = [] debug_img = ski.img_as_ubyte(img) img = ski.img_as_ubyte(img) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 40, 70) boards_img = ski.img_as_bool(edges) boards_img = mp.remove_small_holes(boards_img, 250) boards_img = mp.dilation(boards_img, mp.disk(1)) boards_img = mp.thin(boards_img) boards_img = ndi.binary_fill_holes(boards_img) squares = mp.erosion(boards_img, mp.disk(3)) squares = mp.remove_small_objects(squares, 45) squares = ski.img_as_ubyte(squares) contours, hierarchy = cv2.findContours(squares, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) potential_boards = [] for c in contours: peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.05 * peri, True) if len(approx) == 4: potential_boards.append(approx) # cv2.drawContours(img, approx, -1, (0, 0, 255), 5) for b in potential_boards: M = cv2.moments(b) cx = int(M['m10'] / M['m00']) cy = int(M['m01'] / M['m00']) rotated_rect = cv2.minAreaRect(b) (x, y), (w, h), angle = rotated_rect box = cv2.boxPoints(rotated_rect) box = np.int0(box) src_pts = box.astype("float32") src_pts = transform_src_pts(src_pts) dst_pts = np.array([[0, h], [0, 0], [w, 0], [w, h]], dtype="float32") dst_pts *= 3 M = cv2.getPerspectiveTransform(src_pts, dst_pts) bg_color = np.mean(img) warped = cv2.warpPerspective(img, M, (int(w * 1.8 * 3), int(h * 1.8 * 3)), borderMode=cv2.BORDER_CONSTANT, borderValue=(bg_color, bg_color, bg_color)) gray = cv2.cvtColor(warped, cv2.COLOR_RGB2GRAY) gray = mean_bilateral(ski.img_as_ubyte(gray), mp.disk(5), s0=10, s1=10) gray = ski.img_as_float(gray) gray = 1 - gray gray = cv2.resize(gray, (100, 100)) boards.append(ski.img_as_float(gray)) cv2.destroyAllWindows() # show_boards(boards) return boards
def smooth(img): image= img_as_ubyte(img) image = mean_bilateral(image.astype(numpy.uint16), disk(20), s0=10, s1=10) return image
import matplotlib.pyplot as plt from skimage import data from skimage.morphology import disk from skimage.filters import rank image = data.coins() footprint = disk(20) percentile_result = rank.mean_percentile( image, footprint=footprint, p0=.1, p1=.9 ) bilateral_result = rank.mean_bilateral( image, footprint=footprint, s0=500, s1=500 ) normal_result = rank.mean(image, footprint=footprint) fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(10, 10), sharex=True, sharey=True) ax = axes.ravel() titles = ['Original', 'Percentile mean', 'Bilateral mean', 'Local mean'] imgs = [image, percentile_result, bilateral_result, normal_result] for n in range(0, len(imgs)): ax[n].imshow(imgs[n], cmap=plt.cm.gray) ax[n].set_title(titles[n]) ax[n].axis('off') plt.tight_layout()
# # 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 gray-level similar to the central one. # # .. note:: # # A different implementation is available for color images in # `skimage.filters.denoise_bilateral`. from skimage.filters.rank import mean_bilateral noisy_image = img_as_ubyte(data.camera()) bilat = mean_bilateral(noisy_image.astype(np.uint16), disk(20), s0=10, s1=10) fig, ax = plt.subplots(2, 2, figsize=(10, 7), sharex='row', sharey='row') ax1, ax2, ax3, ax4 = ax.ravel() ax1.imshow(noisy_image, cmap=plt.cm.gray) ax1.set_title('Original') ax1.axis('off') ax1.set_adjustable('box-forced') ax2.imshow(bilat, cmap=plt.cm.gray) ax2.set_title('Bilateral mean') ax2.axis('off') ax2.set_adjustable('box-forced') ax3.imshow(noisy_image[200:350, 350:450], cmap=plt.cm.gray)
def color_enhancement(noisy_image): bilat = mean_bilateral(noisy_image.astype(np.uint16), disk(20), s0=10, s1=10) return bilat