Skip to content

Commit

Permalink
Merge pull request #47 from CellProfiler/features/kirsch-operator
Browse files Browse the repository at this point in the history
Kirsch operator
  • Loading branch information
LeeKamentsky committed Feb 29, 2016
2 parents c5c5333 + 47932f2 commit 4847f09
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
addons:
apt:
packages:
- python-imaging
- python-matplotlib
- python-numpy
- python-scipy
Expand Down
21 changes: 21 additions & 0 deletions centrosome/kirsch.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def run_tests(self):
]),
install_requires=[
"numpy",
"pillow",
"scikit-image",
"scipy",
],
Expand Down
27 changes: 27 additions & 0 deletions tests/test_kirsch.py
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit 4847f09

Please sign in to comment.