def _define_kernel(shape, size, dtype): """Build a kernel to apply a filter on images. Parameters ---------- shape : str Shape of the kernel used to compute the filter ('diamond', 'disk', 'rectangle' or 'square'). size : int, Tuple(int) or List(int) The size of the kernel: - For the rectangle we expect two values (width, height). - For the square one value (width). - For the disk and the diamond one value (radius). dtype : type Dtype used for the kernel (the same as the image). Returns ------- kernel : skimage.morphology.selem object Kernel to use with a skimage filter. """ # build the kernel if shape == "diamond": kernel = diamond(size, dtype=dtype) elif shape == "disk": kernel = disk(size, dtype=dtype) elif shape == "rectangle" and isinstance(size, tuple): kernel = rectangle(size[0], size[1], dtype=dtype) elif shape == "square": kernel = square(size, dtype=dtype) else: raise ValueError("Kernel definition is wrong.") return kernel
def _get_border(self, a): pic_border = np.pad(np.zeros(tuple(map(lambda x: x - 2, a.shape))), 1, 'constant', constant_values=1) inner = binary_erosion(a, selem=square(3)) inner_without_pic_border = np.logical_and(inner, np.logical_not(pic_border)) return np.logical_and(a, np.logical_not(inner_without_pic_border))
def setUp(self): self.black_pixel = 255 * np.ones((4, 4), dtype=np.uint8) self.black_pixel[1, 1] = 0 self.white_pixel = 255 - self.black_pixel self.selems = [ selem.square(2), selem.rectangle(2, 2), selem.rectangle(2, 1), selem.rectangle(1, 2) ]
def _define_kernel(shape, size, dtype): """Build a kernel to apply a filter on images. Parameters ---------- shape : str Shape of the kernel used to compute the filter (`diamond`, `disk`, `rectangle` or `square`). size : int, Tuple(int) or List(int) The size of the kernel: - For the rectangle we expect two values (`height`, `width`). - For the square one value (`width`). - For the disk and the diamond one value (`radius`). dtype : type Dtype used for the kernel (the same as the image). Returns ------- kernel : skimage.morphology.selem object Kernel to use with a skimage filter. """ # build the kernel if shape == "diamond": kernel = diamond(size, dtype=dtype) elif shape == "disk": kernel = disk(size, dtype=dtype) elif shape == "rectangle" and isinstance(size, (tuple, list)): kernel = rectangle(size[0], size[1], dtype=dtype) elif shape == "square": kernel = square(size, dtype=dtype) else: raise ValueError("Kernel definition is wrong. Shape of the kernel " "should be 'diamond', 'disk', 'rectangle' or " "'square'. Not {0}.".format(shape)) return kernel
def test_binary_opening(): strel = selem.square(3) binary_res = binary.binary_opening(bw_img, strel) grey_res = img_as_bool(grey.opening(bw_img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_non_square_image(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img[:100, :200], strel) grey_res = img_as_bool(grey.erosion(bw_img[:100, :200], strel)) testing.assert_array_equal(binary_res, grey_res)
def test_binary_opening(): strel = selem.square(3) binary_res = binary.binary_opening(bw_img, strel) gray_res = img_as_bool(gray.opening(bw_img, strel)) testing.assert_array_equal(binary_res, gray_res)
def test_non_square_image(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img[:100, :200], strel) gray_res = img_as_bool(gray.erosion(bw_img[:100, :200], strel)) testing.assert_array_equal(binary_res, gray_res)
def test_binary_closing(): strel = selem.square(3) binary_res = binary.binary_closing(bw_lena, strel) grey_res = img_as_bool(grey.closing(bw_lena, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_square_selem(self): for k in range(0, 5): actual_mask = selem.square(k) expected_mask = np.ones((k, k), dtype="uint8") assert_equal(expected_mask, actual_mask)
def test_binary_dilation(): strel = selem.square(3) binary_res = binary.binary_dilation(bw_img, strel) grey_res = img_as_bool(grey.dilation(bw_img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_square_selem(self): """Test square structuring elements""" for k in range(0, 5): actual_mask = selem.square(k) expected_mask = np.ones((k, k), dtype='uint8') assert_equal(expected_mask, actual_mask)
def test_binary_opening(): strel = selem.square(3) binary_res = binary.binary_opening(bw_img, strel) with expected_warnings(['precision loss']): grey_res = img_as_bool(grey.opening(bw_img, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_non_square_image(): strel = selem.square(3) binary_res = binary.binary_erosion(bw_img[:100, :200], strel) with expected_warnings(['precision loss']): grey_res = img_as_bool(grey.erosion(bw_img[:100, :200], strel)) testing.assert_array_equal(binary_res, grey_res)
def setUp(self): self.black_pixel = 255 * np.ones((4, 4), dtype=np.uint8) self.black_pixel[1, 1] = 0 self.white_pixel = 255 - self.black_pixel self.selems = [selem.square(2), selem.rectangle(2, 2), selem.rectangle(2, 1), selem.rectangle(1, 2)]
def test_binary_dilation(): strel = selem.square(3) binary_res = binary.binary_dilation(bw_lena, strel) grey_res = img_as_bool(grey.dilation(bw_lena, strel)) testing.assert_array_equal(binary_res, grey_res)
def test_binary_opening(): strel = selem.square(3) binary_res = binary.binary_opening(bw_lena, strel) grey_res = grey.opening(bw_lena, strel) testing.assert_array_equal(binary_res, grey_res)