Permalink
Browse files

Merge pull request #199 from tonysyu/fix-dilation

Fix ndimage.grey_dilation
  • Loading branch information...
2 parents b2afa94 + 2761212 commit 9fdd7545667c1bbc8961c3e54c6d4afe56273318 @rgommers rgommers committed Jun 3, 2012
Showing with 43 additions and 11 deletions.
  1. +19 −11 scipy/ndimage/morphology.py
  2. +24 −0 scipy/ndimage/tests/test_ndimage.py
@@ -1163,8 +1163,8 @@ def grey_erosion(input, size = None, footprint = None, structure = None,
Array over which the grayscale erosion is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale erosion. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ erosion. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1266,6 +1266,8 @@ def grey_erosion(input, size = None, footprint = None, structure = None,
[0, 0, 0, 0, 0, 0, 0]])
"""
+ if size is None and footprint is None and structure is None:
+ raise ValueError("size, footprint or structure must be specified")
return filters._min_or_max_filter(input, size, footprint, structure,
output, mode, cval, origin, 1)
@@ -1287,8 +1289,8 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
Array over which the grayscale dilation is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale dilation. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ dilation. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1406,6 +1408,8 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
[1, 1, 1, 1, 1, 1, 1]])
"""
+ if size is None and footprint is None and structure is None:
+ raise ValueError("size, footprint or structure must be specified")
if structure is not None:
structure = numpy.asarray(structure)
structure = structure[tuple([slice(None, None, -1)] *
@@ -1420,6 +1424,10 @@ def grey_dilation(input, size = None, footprint = None, structure = None,
origin[ii] = -origin[ii]
if footprint is not None:
sz = footprint.shape[ii]
+ elif structure is not None:
+ sz = structure.shape[ii]
+ elif numpy.isscalar(size):
+ sz = size
else:
sz = size[ii]
if not sz & 1:
@@ -1443,8 +1451,8 @@ def grey_opening(input, size = None, footprint = None, structure = None,
Array over which the grayscale opening is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale opening. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ opening. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1538,8 +1546,8 @@ def grey_closing(input, size = None, footprint = None, structure = None,
Array over which the grayscale closing is to be computed.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- grayscale closing. Optional if `footprint` is provided.
+ Shape of a flat and full structuring element used for the grayscale
+ closing. Optional if `footprint` or `structure` is provided.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -1635,9 +1643,9 @@ def morphological_gradient(input, size = None, footprint = None,
Array over which to compute the morphlogical gradient.
size : tuple of ints
- Shape of a flat and full structuring element used for the
- mathematical morphology operations. Optional if `footprint`
- is provided. A larger `size` yields a more blurred gradient.
+ Shape of a flat and full structuring element used for the mathematical
+ morphology operations. Optional if `footprint` or `structure` is
+ provided. A larger `size` yields a more blurred gradient.
footprint : array of ints, optional
Positions of non-infinite elements of a flat structuring element
@@ -4667,6 +4667,30 @@ def test_hit_or_miss03(self):
assert_array_almost_equal(expected, out)
+class TestDilateFix:
+
+ def setUp(self):
+ # dilation related setup
+ self.array = numpy.array([[0, 0, 0, 0, 0,],
+ [0, 0, 0, 0, 0,],
+ [0, 0, 0, 1, 0,],
+ [0, 0, 1, 1, 0,],
+ [0, 0, 0, 0, 0,]], dtype=numpy.uint8)
+
+ self.sq3x3 = numpy.ones((3, 3))
+ dilated3x3 = ndimage.binary_dilation(self.array, structure=self.sq3x3)
+ self.dilated3x3 = dilated3x3.view(numpy.uint8)
+
+ def test_dilation_square_structure(self):
+ result = ndimage.grey_dilation(self.array, structure=self.sq3x3)
+ # +1 accounts for difference between grey and binary dilation
+ assert_array_almost_equal(result, self.dilated3x3 + 1)
+
+ def test_dilation_scalar_size(self):
+ result = ndimage.grey_dilation(self.array, size=3)
+ assert_array_almost_equal(result, self.dilated3x3)
+
+
#class NDImageTestResult(unittest.TestResult):
# separator1 = '=' * 70 + '\n'
# separator2 = '-' * 70 + '\n'

0 comments on commit 9fdd754

Please sign in to comment.