def test_01_03_adjacent_and_different(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 image[5, 6] = .5 labels[5, 5:6] = 1 expected = (image == 1) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == expected)) result = is_local_maximum(image, labels) self.assertTrue(np.all(result == expected))
def test_01_03_adjacent_and_different(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 image[5, 6] = .5 labels[5, 5:6] = 1 expected = (image == 1) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == expected)) result = is_local_maximum(image, labels) self.assertTrue(np.all(result == expected))
def test_03_01_disk_1(self): '''regression test of img-1194, footprint = [1] Test is_local_maximum when every point is a local maximum ''' np.random.seed(31) image = np.random.uniform(size=(10, 20)) footprint = np.array([[1]]) result = is_local_maximum(image, np.ones((10, 20)), footprint) self.assertTrue(np.all(result)) result = is_local_maximum(image, footprint=footprint) self.assertTrue(np.all(result))
def test_03_01_disk_1(self): '''regression test of img-1194, footprint = [1] Test is_local_maximum when every point is a local maximum ''' np.random.seed(31) image = np.random.uniform(size=(10, 20)) footprint = np.array([[1]]) result = is_local_maximum(image, np.ones((10, 20)), footprint) self.assertTrue(np.all(result)) result = is_local_maximum(image, footprint=footprint) self.assertTrue(np.all(result))
def test_01_02_adjacent_and_same(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5:6] = 1 labels[5, 5:6] = 1 result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == (labels == 1)))
def test_01_01_one_point(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 labels[5, 5] = 1 result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == (labels == 1)))
def test_01_02_adjacent_and_same(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5:6] = 1 labels[5, 5:6] = 1 result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == (labels == 1)))
def test_01_01_one_point(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 labels[5, 5] = 1 result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == (labels == 1)))
def test_01_06_adjacent_different_objects(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 image[5, 6] = .5 labels[5, 5] = 1 labels[5, 6] = 2 expected = (labels > 0) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == expected))
def test_01_06_adjacent_different_objects(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) image[5, 5] = 1 image[5, 6] = .5 labels[5, 5] = 1 labels[5, 6] = 2 expected = (labels > 0) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(result == expected))
def test_02_01_four_quadrants(self): np.random.seed(21) image = np.random.uniform(size=(40, 60)) i, j = np.mgrid[0:40, 0:60] labels = 1 + (i >= 20) + (j >= 30) * 2 i, j = np.mgrid[-3:4, -3:4] footprint = (i * i + j * j <= 9) expected = np.zeros(image.shape, float) for imin, imax in ((0, 20), (20, 40)): for jmin, jmax in ((0, 30), (30, 60)): expected[imin:imax, jmin:jmax] = scipy.ndimage.maximum_filter( image[imin:imax, jmin:jmax], footprint=footprint) expected = (expected == image) result = is_local_maximum(image, labels, footprint) self.assertTrue(np.all(result == expected))
def test_02_01_four_quadrants(self): np.random.seed(21) image = np.random.uniform(size=(40, 60)) i, j = np.mgrid[0:40, 0:60] labels = 1 + (i >= 20) + (j >= 30) * 2 i, j = np.mgrid[-3:4, -3:4] footprint = (i * i + j * j <= 9) expected = np.zeros(image.shape, float) for imin, imax in ((0, 20), (20, 40)): for jmin, jmax in ((0, 30), (30, 60)): expected[imin:imax, jmin:jmax] = scipy.ndimage.maximum_filter( image[imin:imax, jmin:jmax], footprint=footprint) expected = (expected == image) result = is_local_maximum(image, labels, footprint) self.assertTrue(np.all(result == expected))
def test_00_00_empty(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(~ result))
def test_00_00_empty(self): image = np.zeros((10, 20)) labels = np.zeros((10, 20), int) result = is_local_maximum(image, labels, np.ones((3, 3), bool)) self.assertTrue(np.all(~result))
def _filter_test(self, image, frame, target, dim, cthreshold, mi, ma): ''' if the convexity of the target is <threshold try to do a watershed segmentation make black image with white polygon do watershed segmentation find polygon center ''' def test(ti): ctest = ti.convexity > cthreshold centtest = self._near_center(ti.centroid, frame) atest = ma > ti.area > mi # print ma, ti.area, mi # print ctest, centtest, atest return ctest, centtest, atest # find the label with the max area ie max of histogram def get_limits(values, bins, width=1): ind = argmax(values) if ind == 0: bil = bins[ind] biu = bins[ind + width] elif ind == len(bins) - width: bil = bins[ind - width] biu = bins[ind] else: bil = bins[ind - width] biu = bins[ind + width] return bil, biu, ind ctest, centtest, atest = test(target) if not ctest and (atest and centtest): src = image.get_frame(0) draw_polygons(src, [target.poly_points], color=(0, 255, 255)) wh = get_size(src) # make image with polygon im = zeros(wh) points = asarray(target.poly_points) points = asarray([(pi.x, pi.y) for pi in points]) rr, cc = polygon(points[:, 0], points[:, 1]) im[cc, rr] = 255 # do watershedding distance = ndimage.distance_transform_edt(im) local_maxi = is_local_maximum(distance, im) markers, ns = ndimage.label(local_maxi) wsrc = watershed(-distance, markers, mask=im ) # bins = 3 * number of labels. this allows you to precisely pick the value of the max area values, bins = histogram(wsrc, bins=ns * 3) bil, biu, ind = get_limits(values, bins) if not bil: values = delete(values, ind) bins = delete(bins, (ind, ind + 1)) bil, biu, ind = get_limits(values, bins) nimage = ones_like(wsrc, dtype='uint8') * 255 nimage[wsrc < bil] = 0 nimage[wsrc > biu] = 0 nimage = invert(nimage) img = asMat(nimage) # locate new polygon from the segmented image tars = self._find_targets(image, img, dim, start=10, w=4, n=2) # do_later(lambda: self.debug_show(im, distance, wsrc, nimage)) if tars: target = tars[0] ctest, centtest, atest = test(target) else: return None, False return target, ctest and atest and centtest