Skip to content

Commit

Permalink
Added unit test and then discovered kernel wasn't properly constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee Kamentsky committed Feb 29, 2016
1 parent c2aa414 commit 47932f2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
8 changes: 7 additions & 1 deletion centrosome/kirsch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ def kirsch(image):

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)):
derivatives = numpy.maximum(derivatives, scipy.ndimage.filters.convolve(image, numpy.array(convolution_mask[:4] + [0] + convolution_mask[4:]).reshape(3, 3)))
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]

Expand Down
Binary file removed tests/resources/kirsch.tiff
Binary file not shown.
30 changes: 24 additions & 6 deletions tests/test_kirsch.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import centrosome.kirsch
import skimage.io
import skimage.data
import scipy.misc
import numpy
import numpy as np
import unittest


def test_kirsch():
assert numpy.array_equal(centrosome.kirsch.kirsch(skimage.data.camera()), scipy.misc.imread("tests/resources/kirsch.tiff"))
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 47932f2

Please sign in to comment.