diff --git a/.travis.yml b/.travis.yml index b57bae2..0f42d68 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ addons: apt: packages: + - python-imaging - python-matplotlib - python-numpy - python-scipy diff --git a/centrosome/kirsch.py b/centrosome/kirsch.py new file mode 100644 index 0000000..2b2501c --- /dev/null +++ b/centrosome/kirsch.py @@ -0,0 +1,21 @@ +import numpy +import scipy.ndimage.filters + + +def kirsch(image): + convolution_mask = [5, -3, -3, -3, -3, -3, 5, 5] + + derivatives = numpy.zeros(image.shape) + + kernel = numpy.zeros((3, 3), image.dtype) + kindex = numpy.array([[0, 1, 2], + [7, -1, 3], + [6, 5, 4]]) + for _ in range(len(convolution_mask)): + kernel[kindex >= 0] = numpy.array(convolution_mask)[kindex[kindex >= 0]] + derivatives = numpy.maximum( + derivatives, scipy.ndimage.filters.convolve(image, kernel)) + + convolution_mask = convolution_mask[-1:] + convolution_mask[:-1] + + return derivatives diff --git a/setup.py b/setup.py index bd6df48..23e294d 100644 --- a/setup.py +++ b/setup.py @@ -89,6 +89,7 @@ def run_tests(self): ]), install_requires=[ "numpy", + "pillow", "scikit-image", "scipy", ], diff --git a/tests/test_kirsch.py b/tests/test_kirsch.py new file mode 100644 index 0000000..dad7f46 --- /dev/null +++ b/tests/test_kirsch.py @@ -0,0 +1,27 @@ +import centrosome.kirsch +import scipy.misc +import numpy as np +import unittest + +class TestKirsch(unittest.TestCase): + def test_01_01_kirsch(self): + # + # Test a maximum at all possible orientations + # + r = np.random.RandomState([ord(_) for _ in "kirsch"]) + for coords in (((0, -1), (-1, -1), (-1, 0)), + ((-1, -1), (-1, 0), (-1, 1)), + ((-1, 0), (-1, 1), (0, 1)), + ((-1, 1), ( 0, 1), (1, 1)), + (( 0, 1), ( 1, 1), (1, 0)), + (( 1, 1), ( 1, 0), (1, -1)), + (( 1, 0), ( 1, -1), (0, -1)), + (( 1, -1), ( 0, -1), (-1, -1))): + img = r.uniform(size=(3, 3)) * .1 + expected = -3 * img + for ioff, joff in coords: + img[ioff + 1, joff + 1] += .5 + expected[ioff + 1, joff+1] = img[ioff + 1, joff+1] * 5 + expected[1, 1] = 0 + result = centrosome.kirsch.kirsch(img) + self.assertEqual(result[1, 1], np.sum(expected))