def test_stress_test_slic(): lengths = range(100, 200, 3) for i in range(len(lengths)): n_segments = lengths[i] ** 3 / 10 ** 3 blob = data.binary_blobs(length=lengths[i], n_dim=3, seed=2) blob = np.float32(blob) slic(blob, n_segments=n_segments, compactness=2)
def test_cherry_picked_results_without_enforce_connectivity(): # fmt: off plane = [[1,1,0,0], [1,1,0,0], [1,1,1,1], [1,1,1,1]] vol = np.asarray([plane, plane, plane]) expected = np.array([[[3, 3, 2, 2], [3, 3, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]], [[3, 3, 2, 2], [3, 3, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]], [[3, 3, 2, 2], [3, 3, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]]]) # fmt: on labels = slic( vol, n_segments=2, compactness=0.1, multichannel=False, enforce_connectivity=False, ) assert (labels == expected).all()
def test_slic_raises_value_error_when_input_dimention_more_than_4(): blob = data.binary_blobs(length=33, n_dim=5, seed=2) blob = np.float32(blob) with pytest.raises(ValueError): labels = slic(blob, n_segments=100, compactness=3)
def test_slic_3channels_give_good_number_of_superpixels(): blob = data.binary_blobs(length=33, n_dim=4, seed=2) blob = blob[:3] blob = filters.gaussian(blob) blob = img_as_float32(blob) labels = slic(blob, n_segments=100, compactness=3) assert len(np.unique(labels)) < 500
def test_slic_3channels_runs(): blob = data.binary_blobs(length=50, n_dim=4, seed=2) blob = blob[..., :3] blob = filters.gaussian(blob) blob = img_as_float32(blob) labels = slic(blob, n_segments=100, multichannel=False, compactness=3) assert isinstance(labels, np.ndarray) assert labels.ndim == 3
def test_slic_with_saved_results(): n = 200 blob = data.binary_blobs(length=n, n_dim=3, seed=2) blob = np.float32(blob) n_segments = n**3 // 5**3 # 5x5x5 initial segment size labels = slic( blob, n_segments=n_segments, compactness=0.01, multichannel=False, enforce_connectivity=True, ) expected = np.load("tests/test_big_data.npy") assert (labels == expected).all()
def __init__(self, img, ROI, region_size=50, compactness=5): h, w = img.shape[0:2] img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) img = cv2.GaussianBlur(img, (3, 3), 0) n_segment = int((h * w) / (region_size**2)) labels = slic(img, n_segment, compactness=compactness) contour_mask = find_boundaries(labels) contour_mask[ROI == 0] = 0 labels[ROI == 0] = -1 self.labels_position = self.__get_labels_position(labels) self.__remap_labels(labels) self.labels = labels self.numOfPixel = np.max(labels) + 1 self.contour_mask = contour_mask
def __init__(self, img, ROI, region_size=50): h, w = img.shape[0:2] img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB) img = cv2.GaussianBlur(img, (3, 3), 0) n_segment = int((h * w) / (region_size**2)) labels = slic(img, n_segment, compactness=10) contour_mask = find_boundaries(labels) contour_mask[ROI == 0] = 0 labels[ROI == 0] = -1 print(f'finished slic') self.labels_position = self.__get_labels_position(labels) print(f'finished get position') self.__remap_labels(labels) print(f'finished get position') self.labels = labels self.numOfPixel = np.max(labels) + 1 self.contour_mask = contour_mask self.adjacent_pairs = self.__construct_adjacency(labels)
def test_slic_grayscale_runs(): blob = data.binary_blobs(length=50, n_dim=3, seed=2) blob = img_as_float32(blob) labels = slic(blob, n_segments=100, multichannel=False, compactness=3) assert isinstance(labels, np.ndarray) assert labels.ndim == 3
def test_slic_grayscale_give_good_number_of_superpixels(): blob = data.binary_blobs(length=50, n_dim=3, seed=2) blob = img_as_float32(blob) labels = slic(blob, n_segments=100, multichannel=False, compactness=3) assert len(np.unique(labels)) < 150