1import numpy as np
2import pytest
3from scipy import ndimage as ndi
4from numpy.testing import assert_allclose, assert_array_equal, assert_equal
5
6from skimage import color, data, transform
7from skimage._shared._warnings import expected_warnings
8from skimage._shared.testing import TestCase, fetch
9from skimage.morphology import gray, footprints
10from skimage.util import img_as_uint, img_as_ubyte
11
12
13class TestMorphology():
14
15    # These expected outputs were generated with skimage v0.12.1
16    # using:
17    #
18    #   from skimage.morphology.tests.test_gray import TestMorphology
19    #   import numpy as np
20    #   output = TestMorphology()._build_expected_output()
21    #   np.savez_compressed('gray_morph_output.npz', **output)
22
23    def _build_expected_output(self):
24        funcs = (gray.erosion, gray.dilation, gray.opening, gray.closing,
25                 gray.white_tophat, gray.black_tophat)
26        footprints_2D = (footprints.square, footprints.diamond,
27                         footprints.disk, footprints.star)
28
29        image = img_as_ubyte(transform.downscale_local_mean(
30            color.rgb2gray(data.coffee()), (20, 20)))
31
32        output = {}
33        for n in range(1, 4):
34            for strel in footprints_2D:
35                for func in funcs:
36                    key = f'{strel.__name__}_{n}_{func.__name__}'
37                    output[key] = func(image, strel(n))
38
39        return output
40
41    def test_gray_morphology(self):
42        expected = dict(np.load(fetch('data/gray_morph_output.npz')))
43        calculated = self._build_expected_output()
44        assert_equal(expected, calculated)
45
46
47class TestEccentricStructuringElements(TestCase):
48    def setUp(self):
49        self.black_pixel = 255 * np.ones((4, 4), dtype=np.uint8)
50        self.black_pixel[1, 1] = 0
51        self.white_pixel = 255 - self.black_pixel
52        self.footprints = [footprints.square(2), footprints.rectangle(2, 2),
53                           footprints.rectangle(2, 1),
54                           footprints.rectangle(1, 2)]
55
56    def test_dilate_erode_symmetry(self):
57        for s in self.footprints:
58            c = gray.erosion(self.black_pixel, s)
59            d = gray.dilation(self.white_pixel, s)
60            assert np.all(c == (255 - d))
61
62    def test_open_black_pixel(self):
63        for s in self.footprints:
64            gray_open = gray.opening(self.black_pixel, s)
65            assert np.all(gray_open == self.black_pixel)
66
67    def test_close_white_pixel(self):
68        for s in self.footprints:
69            gray_close = gray.closing(self.white_pixel, s)
70            assert np.all(gray_close == self.white_pixel)
71
72    def test_open_white_pixel(self):
73        for s in self.footprints:
74            assert np.all(gray.opening(self.white_pixel, s) == 0)
75
76    def test_close_black_pixel(self):
77        for s in self.footprints:
78            assert np.all(gray.closing(self.black_pixel, s) == 255)
79
80    def test_white_tophat_white_pixel(self):
81        for s in self.footprints:
82            tophat = gray.white_tophat(self.white_pixel, s)
83            assert np.all(tophat == self.white_pixel)
84
85    def test_black_tophat_black_pixel(self):
86        for s in self.footprints:
87            tophat = gray.black_tophat(self.black_pixel, s)
88            assert np.all(tophat == (255 - self.black_pixel))
89
90    def test_white_tophat_black_pixel(self):
91        for s in self.footprints:
92            tophat = gray.white_tophat(self.black_pixel, s)
93            assert np.all(tophat == 0)
94
95    def test_black_tophat_white_pixel(self):
96        for s in self.footprints:
97            tophat = gray.black_tophat(self.white_pixel, s)
98            assert np.all(tophat == 0)
99
100
101gray_functions = [gray.erosion, gray.dilation,
102                  gray.opening, gray.closing,
103                  gray.white_tophat, gray.black_tophat]
104
105
106@pytest.mark.parametrize("function", gray_functions)
107def test_default_footprint(function):
108    strel = footprints.diamond(radius=1)
109    image = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
110                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
111                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
112                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
113                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
114                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
115                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
116                      [0, 0, 1, 1, 1, 0, 0, 1, 0, 0],
117                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
118                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
119                      [0, 0, 1, 1, 1, 1, 1, 1, 0, 0],
120                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
121                      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], np.uint8)
122    im_expected = function(image, strel)
123    im_test = function(image)
124    assert_array_equal(im_expected, im_test)
125
126
127def test_3d_fallback_default_footprint():
128    # 3x3x3 cube inside a 7x7x7 image:
129    image = np.zeros((7, 7, 7), bool)
130    image[2:-2, 2:-2, 2:-2] = 1
131
132    opened = gray.opening(image)
133
134    # expect a "hyper-cross" centered in the 5x5x5:
135    image_expected = np.zeros((7, 7, 7), dtype=bool)
136    image_expected[2:5, 2:5, 2:5] = ndi.generate_binary_structure(3, 1)
137    assert_array_equal(opened, image_expected)
138
139
140gray_3d_fallback_functions = [gray.closing, gray.opening]
141
142
143@pytest.mark.parametrize("function", gray_3d_fallback_functions)
144def test_3d_fallback_cube_footprint(function):
145    # 3x3x3 cube inside a 7x7x7 image:
146    image = np.zeros((7, 7, 7), bool)
147    image[2:-2, 2:-2, 2:-2] = 1
148
149    cube = np.ones((3, 3, 3), dtype=np.uint8)
150
151    new_image = function(image, cube)
152    assert_array_equal(new_image, image)
153
154
155def test_3d_fallback_white_tophat():
156    image = np.zeros((7, 7, 7), dtype=bool)
157    image[2, 2:4, 2:4] = 1
158    image[3, 2:5, 2:5] = 1
159    image[4, 3:5, 3:5] = 1
160
161    with expected_warnings([r'operator.*deprecated|\A\Z']):
162        new_image = gray.white_tophat(image)
163    footprint = ndi.generate_binary_structure(3, 1)
164    with expected_warnings([r'operator.*deprecated|\A\Z']):
165        image_expected = ndi.white_tophat(
166            image.view(dtype=np.uint8), footprint=footprint)
167    assert_array_equal(new_image, image_expected)
168
169
170def test_3d_fallback_black_tophat():
171    image = np.ones((7, 7, 7), dtype=bool)
172    image[2, 2:4, 2:4] = 0
173    image[3, 2:5, 2:5] = 0
174    image[4, 3:5, 3:5] = 0
175
176    with expected_warnings([r'operator.*deprecated|\A\Z']):
177        new_image = gray.black_tophat(image)
178    footprint = ndi.generate_binary_structure(3, 1)
179    with expected_warnings([r'operator.*deprecated|\A\Z']):
180        image_expected = ndi.black_tophat(
181            image.view(dtype=np.uint8), footprint=footprint)
182    assert_array_equal(new_image, image_expected)
183
184
185def test_2d_ndimage_equivalence():
186    image = np.zeros((9, 9), np.uint8)
187    image[2:-2, 2:-2] = 128
188    image[3:-3, 3:-3] = 196
189    image[4, 4] = 255
190
191    opened = gray.opening(image)
192    closed = gray.closing(image)
193
194    footprint = ndi.generate_binary_structure(2, 1)
195    ndimage_opened = ndi.grey_opening(image, footprint=footprint)
196    ndimage_closed = ndi.grey_closing(image, footprint=footprint)
197
198    assert_array_equal(opened, ndimage_opened)
199    assert_array_equal(closed, ndimage_closed)
200
201
202# float test images
203im = np.array([[ 0.55,  0.72,  0.6 ,  0.54,  0.42],
204               [ 0.65,  0.44,  0.89,  0.96,  0.38],
205               [ 0.79,  0.53,  0.57,  0.93,  0.07],
206               [ 0.09,  0.02,  0.83,  0.78,  0.87],
207               [ 0.98,  0.8 ,  0.46,  0.78,  0.12]])
208
209eroded = np.array([[ 0.55,  0.44,  0.54,  0.42,  0.38],
210                   [ 0.44,  0.44,  0.44,  0.38,  0.07],
211                   [ 0.09,  0.02,  0.53,  0.07,  0.07],
212                   [ 0.02,  0.02,  0.02,  0.78,  0.07],
213                   [ 0.09,  0.02,  0.46,  0.12,  0.12]])
214
215dilated = np.array([[ 0.72,  0.72,  0.89,  0.96,  0.54],
216                    [ 0.79,  0.89,  0.96,  0.96,  0.96],
217                    [ 0.79,  0.79,  0.93,  0.96,  0.93],
218                    [ 0.98,  0.83,  0.83,  0.93,  0.87],
219                    [ 0.98,  0.98,  0.83,  0.78,  0.87]])
220
221opened = np.array([[ 0.55,  0.55,  0.54,  0.54,  0.42],
222                   [ 0.55,  0.44,  0.54,  0.44,  0.38],
223                   [ 0.44,  0.53,  0.53,  0.78,  0.07],
224                   [ 0.09,  0.02,  0.78,  0.78,  0.78],
225                   [ 0.09,  0.46,  0.46,  0.78,  0.12]])
226
227closed = np.array([[ 0.72,  0.72,  0.72,  0.54,  0.54],
228                   [ 0.72,  0.72,  0.89,  0.96,  0.54],
229                   [ 0.79,  0.79,  0.79,  0.93,  0.87],
230                   [ 0.79,  0.79,  0.83,  0.78,  0.87],
231                   [ 0.98,  0.83,  0.78,  0.78,  0.78]])
232
233
234def test_float():
235    assert_allclose(gray.erosion(im), eroded)
236    assert_allclose(gray.dilation(im), dilated)
237    assert_allclose(gray.opening(im), opened)
238    assert_allclose(gray.closing(im), closed)
239
240
241def test_uint16():
242    im16, eroded16, dilated16, opened16, closed16 = (
243        map(img_as_uint, [im, eroded, dilated, opened, closed]))
244    assert_allclose(gray.erosion(im16), eroded16)
245    assert_allclose(gray.dilation(im16), dilated16)
246    assert_allclose(gray.opening(im16), opened16)
247    assert_allclose(gray.closing(im16), closed16)
248
249
250def test_discontiguous_out_array():
251    image = np.array([[5, 6, 2],
252                      [7, 2, 2],
253                      [3, 5, 1]], np.uint8)
254    out_array_big = np.zeros((5, 5), np.uint8)
255    out_array = out_array_big[::2, ::2]
256    expected_dilation = np.array([[7, 0, 6, 0, 6],
257                                  [0, 0, 0, 0, 0],
258                                  [7, 0, 7, 0, 2],
259                                  [0, 0, 0, 0, 0],
260                                  [7, 0, 5, 0, 5]], np.uint8)
261    expected_erosion = np.array([[5, 0, 2, 0, 2],
262                                 [0, 0, 0, 0, 0],
263                                 [2, 0, 2, 0, 1],
264                                 [0, 0, 0, 0, 0],
265                                 [3, 0, 1, 0, 1]], np.uint8)
266    gray.dilation(image, out=out_array)
267    assert_array_equal(out_array_big, expected_dilation)
268    gray.erosion(image, out=out_array)
269    assert_array_equal(out_array_big, expected_erosion)
270
271
272def test_1d_erosion():
273    image = np.array([1, 2, 3, 2, 1])
274    expected = np.array([1, 1, 2, 1, 1])
275    eroded = gray.erosion(image)
276    assert_array_equal(eroded, expected)
277
278
279def test_deprecated_import():
280    msg = "Importing from skimage.morphology.grey is deprecated."
281    with expected_warnings([msg + r"|\A\Z"]):
282        from skimage.morphology.grey import erosion
283
284
285@pytest.mark.parametrize(
286    'function', ['erosion', 'dilation', 'closing', 'opening', 'white_tophat',
287                 'black_tophat'],
288)
289def test_selem_kwarg_deprecation(function):
290    with expected_warnings(["`selem` is a deprecated argument name"]):
291        getattr(gray, function)(np.zeros((4, 4)), selem=np.ones((3, 3)))
292