def test_blob_overlap(): img = np.ones((256, 256), dtype=np.uint8) xs, ys = circle(100, 100, 20) img[xs, ys] = 255 xs, ys = circle(120, 100, 30) img[xs, ys] = 255 blobs = blob_doh(img, min_sigma=1, max_sigma=60, num_sigma=10, threshold=.05) assert len(blobs) == 1 r1, r2 = 7, 6 pad1, pad2 = 11, 12 blob1 = ellipsoid(r1, r1, r1) blob1 = util.pad(blob1, pad1, mode='constant') blob2 = ellipsoid(r2, r2, r2) blob2 = util.pad(blob2, [(pad2, pad2), (pad2 - 9, pad2 + 9), (pad2, pad2)], mode='constant') im3 = np.logical_or(blob1, blob2) blobs = blob_log(im3, min_sigma=2, max_sigma=10, overlap=0.1) assert len(blobs) == 1 # Two circles with distance between centers equal to radius overlap = _blob_overlap(np.array([0, 0, 10 / math.sqrt(2)]), np.array([0, 10, 10 / math.sqrt(2)])) assert_almost_equal( overlap, 1. / math.pi * (2 * math.acos(1. / 2) - math.sqrt(3) / 2.))
def test_blob_log_overlap_3d(): r1, r2 = 7, 6 pad1, pad2 = 11, 12 blob1 = ellipsoid(r1, r1, r1) blob1 = np.pad(blob1, pad1, mode='constant') blob2 = ellipsoid(r2, r2, r2) blob2 = np.pad(blob2, [(pad2, pad2), (pad2 - 9, pad2 + 9), (pad2, pad2)], mode='constant') im3 = np.logical_or(blob1, blob2) blobs = blob_log(im3, min_sigma=2, max_sigma=10, overlap=0.1) assert len(blobs) == 1
def test_blob_dog_3d(dtype, threshold_type): # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = np.pad(im3, pad, mode='constant') if threshold_type == 'absolute': threshold = 0.001 threshold_rel = 0 elif threshold_type == 'relative': threshold = 0 threshold_rel = 0.5 blobs = blob_dog( im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=threshold, threshold_rel=threshold_rel, ) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1.1
def test_blob_dog_3d_anisotropic(dtype, threshold_type): # Testing 3D anisotropic r = 10 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = np.pad(im3, pad, mode='constant') if threshold_type == 'absolute': threshold = 0.001 threshold_rel = None elif threshold_type == 'relative': threshold = None threshold_rel = 0.5 blobs = blob_dog( im3.astype(dtype, copy=False), min_sigma=[1.5, 3, 3], max_sigma=[5, 10, 10], sigma_ratio=1.2, threshold=threshold, threshold_rel=threshold_rel, ) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1.1 assert abs(math.sqrt(3) * b[4] - r) < 1.1 assert abs(math.sqrt(3) * b[5] - r) < 1.1
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = circle(400, 130, 5) img[xs, ys] = 255 xs, ys = circle(100, 300, 25) img[xs, ys] = 255 xs, ys = circle(200, 350, 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_log_overlap_3d_anisotropic(): r1, r2 = 7, 6 pad1, pad2 = 11, 12 blob1 = ellipsoid(r1, r1, r1) blob1 = np.pad(blob1, pad1, mode='constant') blob2 = ellipsoid(r2, r2, r2) blob2 = np.pad(blob2, [(pad2, pad2), (pad2 - 9, pad2 + 9), (pad2, pad2)], mode='constant') im3 = np.logical_or(blob1, blob2) blobs = blob_log(im3, min_sigma=[2, 2.01, 2.005], max_sigma=10, overlap=0.1) assert len(blobs) == 1 # Two circles with distance between centers equal to radius overlap = _blob_overlap(np.array([0, 0, 10 / math.sqrt(2)]), np.array([0, 10, 10 / math.sqrt(2)])) assert_almost_equal(overlap, 1./math.pi * (2 * math.acos(1./2) - math.sqrt(3)/2.))
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = circle(400, 130, 5) img[xs, ys] = 255 xs, ys = circle(100, 300, 25) img[xs, ys] = 255 xs, ys = circle(200, 350, 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100,100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_log_3d(): # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = np.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_overlap(): img = np.ones((256, 256), dtype=np.uint8) xs, ys = circle(100, 100, 20) img[xs, ys] = 255 xs, ys = circle(120, 100, 30) img[xs, ys] = 255 blobs = blob_doh( img, min_sigma=1, max_sigma=60, num_sigma=10, threshold=.05) assert len(blobs) == 1 r1, r2 = 7, 6 pad1, pad2 = 11, 12 blob1 = ellipsoid(r1, r1, r1) blob1 = util.pad(blob1, pad1, mode='constant') blob2 = ellipsoid(r2, r2, r2) blob2 = util.pad(blob2, [(pad2, pad2), (pad2 - 9, pad2 + 9), (pad2, pad2)], mode='constant') im3 = np.logical_or(blob1, blob2) blobs = blob_log(im3, min_sigma=2, max_sigma=10, overlap=0.1) assert len(blobs) == 1 # Two circles with distance between centers equal to radius overlap = _blob_overlap(np.array([0, 0, 10 / math.sqrt(2)]), np.array([0, 10, 10 / math.sqrt(2)])) assert_almost_equal(overlap, 1./math.pi * (2 * math.acos(1./2) - math.sqrt(3)/2.))
def get_3D_sphere_coordinates(a_coordZYX=[0, 0, 0], radius=10, limit_shape=(9999, 9999, 9999), xy_vs_z=1, verbose=False): ''' get coordinates of a sphere on a certain locations, and within the perimeter set out by limits outputs an 3D array with the coordinates [Z,Y,X] :param a_coordZYX: where the center of the sphere must be placed :param radius: the radius in l_pixels (in the xy-plane, the z radius will be scale appropriately) :param limit_shape: the dimensions of the frame (Z,Y,X) :param verbose: :output [Z,Y,X] representing the coordinates of the filled sphere ''' #get a filled sphere (in real size) as a boolean matrix (True=part of sphere) na_sphere = ellipsoid(max(1, round(radius / xy_vs_z)), radius, radius, levelset=False) if verbose: print('na_sphere', na_sphere, 'shape', na_sphere.shape) # place this sphere in a X Y Z space defined by limits a_center_of_sphere = [int((i - 1) / 2) for i in na_sphere.shape] if verbose: print('a_center_of_sphere', a_center_of_sphere) a_delta_move = np.subtract(a_coordZYX, a_center_of_sphere) if verbose: print('a_delta_move', a_delta_move) Z, Y, X, = np.where(na_sphere) Z = np.add(Z, a_delta_move[0]) Y = np.add(Y, a_delta_move[1]) X = np.add(X, a_delta_move[2]) z_lim, y_lim, x_lim = limit_shape sphere_coord = [ coord for coord in zip(Z, Y, X) if (coord[0] >= 0 and coord[1] >= 0 and coord[2] >= 0 and coord[0] < z_lim and coord[1] < y_lim and coord[2] < x_lim) ] return tuple( tuple(map(int, i)) for i in zip(*sphere_coord) ) #turn list of 3-tuples into a 3-tuple of tuples (Z,Y,X) and convert to int (used for indexing) (tuple->tuple->int)
def test_blob_log_3d_anisotropic(): # Testing 3D anisotropic r = 6 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = np.pad(im3, pad, mode='constant') blobs = blob_log( im3, min_sigma=[1, 2, 2], max_sigma=[5, 10, 10], ) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = circle(400, 130, 5) img[xs, ys] = 255 xs, ys = circle(100, 300, 25) img[xs, ys] = 255 xs, ys = circle(200, 350, 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100,100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b.shape == (4,) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 10 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_dog( im3, min_sigma=[1.5, 3, 3], max_sigma=[5, 10, 10], sigma_ratio=1.2, threshold=0.1 ) b = blobs[0] assert b.shape == (6,) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = circle(5, 5, 5) img[xs, ys] = 255 blobs = blob_dog( img, min_sigma=1.5, max_sigma=5, sigma_ratio=1.2, ) assert blobs.shape[0] == 1 b = blobs[0] assert b[0] == b[1] == 5, "blob should be 5 px from x and y borders" blobs = blob_dog( img, min_sigma=1.5, max_sigma=5, sigma_ratio=1.2, exclude_border=5 ) msg = "zero blobs should be detected, as only blob is 5 px from border" assert blobs.shape[0] == 0, msg
def test_blob_dog(): r2 = math.sqrt(2) img = np.ones((512, 512)) xs, ys = disk((400, 130), 5) img[xs, ys] = 255 xs, ys = disk((100, 300), 25) img[xs, ys] = 255 xs, ys = disk((200, 350), 45) img[xs, ys] = 255 blobs = blob_dog(img, min_sigma=5, max_sigma=50) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = np.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 10 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = np.pad(im3, pad, mode='constant') blobs = blob_dog(im3, min_sigma=[1.5, 3, 3], max_sigma=[5, 10, 10], sigma_ratio=1.2, threshold=0.1) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = disk((5, 5), 5) img[xs, ys] = 255
def test_blob_log(): r2 = math.sqrt(2) img = np.ones((256, 256)) xs, ys = circle(200, 65, 5) img[xs, ys] = 255 xs, ys = circle(80, 25, 15) img[xs, ys] = 255 xs, ys = circle(50, 150, 25) img[xs, ys] = 255 xs, ys = circle(100, 175, 30) img[xs, ys] = 255 blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 3 b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing log scale blobs = blob_log( img, min_sigma=5, max_sigma=20, threshold=1, log_scale=True) b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing no peaks img_empty = np.zeros((100,100)) assert blob_log(img_empty).size == 0 # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_log(): r2 = math.sqrt(2) img = np.ones((256, 256)) xs, ys = circle(200, 65, 5) img[xs, ys] = 255 xs, ys = circle(80, 25, 15) img[xs, ys] = 255 xs, ys = circle(50, 150, 25) img[xs, ys] = 255 xs, ys = circle(100, 175, 30) img[xs, ys] = 255 blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 3 b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing log scale blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1, log_scale=True) b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_log(img_empty).size == 0 # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 6 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log( im3, min_sigma=[1, 2, 2], max_sigma=[5, 10, 10], ) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = circle(5, 5, 5) img[xs, ys] = 255 blobs = blob_log( img, min_sigma=1.5, max_sigma=5, ) assert blobs.shape[0] == 1 b = blobs[0] assert b[0] == b[1] == 5, "blob should be 5 px from x and y borders" blobs = blob_dog(img, min_sigma=1.5, max_sigma=5, exclude_border=5) msg = "zero blobs should be detected, as only blob is 5 px from border" assert blobs.shape[0] == 0, msg
def test_blob_log(): r2 = math.sqrt(2) img = np.ones((256, 256)) xs, ys = circle(200, 65, 5) img[xs, ys] = 255 xs, ys = circle(80, 25, 15) img[xs, ys] = 255 xs, ys = circle(50, 150, 25) img[xs, ys] = 255 xs, ys = circle(100, 175, 30) img[xs, ys] = 255 blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 3 b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing log scale blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1, log_scale=True) b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing no peaks img_empty = np.zeros((100, 100)) assert blob_log(img_empty).size == 0 # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1
def test_blob_dog(dtype): r2 = math.sqrt(2) img = np.ones((512, 512), dtype=dtype) xs, ys = disk((400, 130), 5) img[xs, ys] = 255 xs, ys = disk((100, 300), 25) img[xs, ys] = 255 xs, ys = disk((200, 350), 45) img[xs, ys] = 255 threshold = 2.0 if img.dtype.kind != 'f': # account for internal scaling to [0, 1] by img_as_float threshold /= img.ptp() blobs = blob_dog(img, min_sigma=5, max_sigma=50, threshold=threshold) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 5 b = s[0] assert abs(b[0] - 400) <= thresh assert abs(b[1] - 130) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 300) <= thresh assert abs(radius(b) - 25) <= thresh b = s[2] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 350) <= thresh assert abs(radius(b) - 45) <= thresh # Testing no peaks img_empty = np.zeros((100, 100), dtype=dtype) assert blob_dog(img_empty).size == 0 # Testing 3D r = 10 pad = 10 im3 = ellipsoid(r, r, r) im3 = np.pad(im3, pad, mode='constant') threshold = 0.1 if img.dtype.kind != 'f': # account for internal scaling to [0, 1] by img_as_float threshold /= img.ptp() blobs = blob_dog(im3, min_sigma=3, max_sigma=10, sigma_ratio=1.2, threshold=threshold) b = blobs[0] assert b.shape == (4, ) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 10 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = np.pad(im3, pad, mode='constant') blobs = blob_dog(im3.astype(dtype, copy=False), min_sigma=[1.5, 3, 3], max_sigma=[5, 10, 10], sigma_ratio=1.2, threshold=threshold) b = blobs[0] assert b.shape == (6, ) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1
def test_blob_log(): r2 = math.sqrt(2) img = np.ones((256, 256)) xs, ys = circle(200, 65, 5) img[xs, ys] = 255 xs, ys = circle(80, 25, 15) img[xs, ys] = 255 xs, ys = circle(50, 150, 25) img[xs, ys] = 255 xs, ys = circle(100, 175, 30) img[xs, ys] = 255 blobs = blob_log(img, min_sigma=5, max_sigma=20, threshold=1) radius = lambda x: r2 * x[2] s = sorted(blobs, key=radius) thresh = 3 b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing log scale blobs = blob_log( img, min_sigma=5, max_sigma=20, threshold=1, log_scale=True) b = s[0] assert abs(b[0] - 200) <= thresh assert abs(b[1] - 65) <= thresh assert abs(radius(b) - 5) <= thresh b = s[1] assert abs(b[0] - 80) <= thresh assert abs(b[1] - 25) <= thresh assert abs(radius(b) - 15) <= thresh b = s[2] assert abs(b[0] - 50) <= thresh assert abs(b[1] - 150) <= thresh assert abs(radius(b) - 25) <= thresh b = s[3] assert abs(b[0] - 100) <= thresh assert abs(b[1] - 175) <= thresh assert abs(radius(b) - 30) <= thresh # Testing no peaks img_empty = np.zeros((100,100)) assert blob_log(img_empty).size == 0 # Testing 3D r = 6 pad = 10 im3 = ellipsoid(r, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log(im3, min_sigma=3, max_sigma=10) b = blobs[0] assert b.shape == (4,) assert b[0] == r + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r) < 1 # Testing 3D anisotropic r = 6 pad = 10 im3 = ellipsoid(r / 2, r, r) im3 = util.pad(im3, pad, mode='constant') blobs = blob_log( im3, min_sigma=[1, 2, 2], max_sigma=[5, 10, 10], ) b = blobs[0] assert b.shape == (6,) assert b[0] == r / 2 + pad + 1 assert b[1] == r + pad + 1 assert b[2] == r + pad + 1 assert abs(math.sqrt(3) * b[3] - r / 2) < 1 assert abs(math.sqrt(3) * b[4] - r) < 1 assert abs(math.sqrt(3) * b[5] - r) < 1 # Testing exclude border # image where blob is 5 px from borders, radius 5 img = np.ones((512, 512)) xs, ys = circle(5, 5, 5) img[xs, ys] = 255 blobs = blob_log( img, min_sigma=1.5, max_sigma=5, ) assert blobs.shape[0] == 1 b = blobs[0] assert b[0] == b[1] == 5, "blob should be 5 px from x and y borders" blobs = blob_dog( img, min_sigma=1.5, max_sigma=5, exclude_border=5 ) msg = "zero blobs should be detected, as only blob is 5 px from border" assert blobs.shape[0] == 0, msg