def test_threshold_adaptive_gaussian(self): ref = np.array( [[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [ True, True, False, False, False]] ) out = threshold_adaptive(self.image, 3, method='gaussian') assert_equal(ref, out) out = threshold_adaptive(self.image, 3, method='gaussian', param=1.0 / 3.0) assert_equal(ref, out)
def test_threshold_adaptive_gaussian(self): ref = np.array([[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [True, True, False, False, False]]) out = threshold_adaptive(self.image, 3, method='gaussian') assert_equal(ref, out) out = threshold_adaptive(self.image, 3, method='gaussian', param=1. / 3.) assert_equal(ref, out)
def test_threshold_adaptive_median(self): ref = np.array([[False, False, False, False, True], [False, False, True, False, False], [False, False, True, False, False], [False, False, True, True, False], [False, True, False, False, False]]) out = threshold_adaptive(self.image, 3, method='median') assert_equal(ref, out)
def test_threshold_adaptive_median(self): ref = np.array( [[False, False, False, False, True], [False, False, True, False, False], [False, False, True, False, False], [False, False, True, True, False], [False, True, False, False, False]] ) out = threshold_adaptive(self.image, 3, method='median') assert_equal(ref, out)
def test_threshold_local_equals_adaptive(self): def func(arr): return arr.sum() / arr.shape[0] with expected_warnings(['deprecated', 'return value']): thresholded_original = threshold_adaptive(self.image, 3, method='generic', param=func) threshold_new = threshold_local(self.image, 3, method='generic', param=func) assert_equal(thresholded_original, self.image > threshold_new)
def test_threshold_adaptive_generic(self): def func(arr): return arr.sum() / arr.shape[0] ref = np.array([[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [True, True, False, False, False]]) out = threshold_adaptive(self.image, 3, method='generic', param=func) assert_equal(ref, out)
def test_threshold_adaptive_generic(self): def func(arr): return arr.sum() / arr.shape[0] ref = np.array( [[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [ True, True, False, False, False]] ) out = threshold_adaptive(self.image, 3, method='generic', param=func) assert_equal(ref, out)
def test_threshold_adaptive_generic(self): def func(arr): return arr.sum() / arr.shape[0] ref = np.array( [[False, False, False, False, True], [False, False, True, False, True], [False, False, True, True, False], [False, True, True, False, False], [ True, True, False, False, False]] ) with expected_warnings(['deprecated', 'return value']): out = threshold_adaptive(self.image, 3, method='generic', param=func) assert_equal(ref, out)
from skimage.filters.thresholding import threshold_adaptive from skimage.morphology import square, erosion from skimage.exposure import rescale_intensity from skimage.measure import label, regionprops from scipy.ndimage import binary_fill_holes import numpy as np from barcode import draw_regions if __name__ == '__main__': img = imread('./../../images/bloodcells.jpg') p1, p2 = np.percentile(img, (0, 90)) img = rescale_intensity(img, in_range=(p1, p2)) img_gray = rgb2gray(img) img_gray_filtered = 1. - threshold_adaptive( img_gray, block_size=31, offset=0.02) img_gray_filtered = erosion(img_gray_filtered, selem=square(3)) filled = binary_fill_holes(img_gray_filtered) labels = label(filled) regions = regionprops(labels) cells = [] for region in regions: bbox = region.bbox height = bbox[2] - bbox[0] width = bbox[3] - bbox[1] if 20 < height and 10 < width: cells.append(region) print "Number if cells:", len(cells) cells_result = draw_regions(cells, img_gray.shape) plt.imshow(cells_result, 'gray') plt.show()
from matplotlib import pyplot as plt from skimage.color import rgb2gray from skimage.filters.thresholding import threshold_adaptive from skimage.morphology import opening, square from skimage.measure import label, regionprops def draw_regions(regs, img_size): img = np.ndarray((img_size[0], img_size[1]), dtype='float64') for reg in regs: coords = reg.coords for coord in coords: img[coord[0]][coord[1]] = 1. return img if __name__ == '__main__': barcode_img = imread('./../../images/barcode.jpg') barcode_gray = rgb2gray(barcode_img) barcode_filtered = 1. - threshold_adaptive(barcode_gray, block_size=75, offset=0.04) barcode_filtered = opening(barcode_filtered, selem=square(3)) labels = label(barcode_filtered) regions = regionprops(labels) num_regions = [] for region in regions: bbox = region.bbox height = bbox[2] - bbox[0] width = bbox[3] - bbox[1] if 25 < height < 45 and 1 <= float(height) / width <= 6 and (region.extent < 0.57 or region.extent > 0.80): num_regions.append(region) barcode_result = draw_regions(num_regions, barcode_gray.shape) plt.imshow(barcode_result, 'gray') plt.show()