mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-02 02:43:05 +08:00
Rename greyscale morphology functions.
Remove "greyscale" prefix on functions in ``morphology.grey``. To avoid confusion (and name clashes), ``greyscale_open`` and ``greyscale_close`` were renamed ``opening`` and ``closing`` (instead of ``open`` and ``close``). As a bonus, these names match scipy.ndimage.
This commit is contained in:
+47
-20
@@ -5,11 +5,15 @@
|
||||
|
||||
__docformat__ = 'restructuredtext en'
|
||||
|
||||
import warnings
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
eps = np.finfo(float).eps
|
||||
|
||||
def greyscale_erode(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
|
||||
def erosion(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
"""Return greyscale morphological erosion of an image.
|
||||
|
||||
Morphological erosion sets a pixel at (i,j) to the minimum over all pixels
|
||||
@@ -46,7 +50,7 @@ def greyscale_erode(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
... [0, 1, 1, 1, 0],
|
||||
... [0, 1, 1, 1, 0],
|
||||
... [0, 0, 0, 0, 0]], dtype=np.uint8)
|
||||
>>> greyscale_erode(bright_square, square(3))
|
||||
>>> erosion(bright_square, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0],
|
||||
@@ -64,7 +68,7 @@ def greyscale_erode(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
except ImportError:
|
||||
raise ImportError("cmorph extension not available.")
|
||||
|
||||
def greyscale_dilate(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
def dilation(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
"""Return greyscale morphological dilation of an image.
|
||||
|
||||
Morphological dilation sets a pixel at (i,j) to the maximum over all pixels
|
||||
@@ -102,7 +106,7 @@ def greyscale_dilate(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
... [0, 0, 1, 0, 0],
|
||||
... [0, 0, 0, 0, 0],
|
||||
... [0, 0, 0, 0, 0]], dtype=np.uint8)
|
||||
>>> greyscale_dilate(bright_pixel, square(3))
|
||||
>>> dilation(bright_pixel, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[0, 1, 1, 1, 0],
|
||||
[0, 1, 1, 1, 0],
|
||||
@@ -120,7 +124,7 @@ def greyscale_dilate(image, selem, out=None, shift_x=False, shift_y=False):
|
||||
except ImportError:
|
||||
raise ImportError("cmorph extension not available.")
|
||||
|
||||
def greyscale_open(image, selem, out=None):
|
||||
def opening(image, selem, out=None):
|
||||
"""Return greyscale morphological opening of an image.
|
||||
|
||||
The morphological opening on an image is defined as an erosion followed by
|
||||
@@ -154,7 +158,7 @@ def greyscale_open(image, selem, out=None):
|
||||
... [1, 1, 1, 1, 1],
|
||||
... [1, 1, 0, 1, 1],
|
||||
... [1, 0, 0, 0, 1]], dtype=np.uint8)
|
||||
>>> greyscale_open(bad_connection, square(3))
|
||||
>>> opening(bad_connection, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[1, 1, 0, 1, 1],
|
||||
[1, 1, 0, 1, 1],
|
||||
@@ -166,12 +170,11 @@ def greyscale_open(image, selem, out=None):
|
||||
shift_x = True if (w % 2) == 0 else False
|
||||
shift_y = True if (h % 2) == 0 else False
|
||||
|
||||
eroded = greyscale_erode(image, selem)
|
||||
out = greyscale_dilate(eroded, selem, out=out,
|
||||
shift_x=shift_x, shift_y=shift_y)
|
||||
eroded = erosion(image, selem)
|
||||
out = dilation(eroded, selem, out=out, shift_x=shift_x, shift_y=shift_y)
|
||||
return out
|
||||
|
||||
def greyscale_close(image, selem, out=None):
|
||||
def closing(image, selem, out=None):
|
||||
"""Return greyscale morphological closing of an image.
|
||||
|
||||
The morphological closing on an image is defined as a dilation followed by
|
||||
@@ -205,7 +208,7 @@ def greyscale_close(image, selem, out=None):
|
||||
... [1, 1, 0, 1, 1],
|
||||
... [0, 0, 0, 0, 0],
|
||||
... [0, 0, 0, 0, 0]], dtype=np.uint8)
|
||||
>>> greyscale_close(broken_line, square(3))
|
||||
>>> closing(broken_line, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[0, 0, 0, 0, 0],
|
||||
[1, 1, 1, 1, 1],
|
||||
@@ -217,12 +220,11 @@ def greyscale_close(image, selem, out=None):
|
||||
shift_x = True if (w % 2) == 0 else False
|
||||
shift_y = True if (h % 2) == 0 else False
|
||||
|
||||
dilated = greyscale_dilate(image, selem)
|
||||
out = greyscale_erode(dilated, selem, out=out,
|
||||
shift_x=shift_x, shift_y=shift_y)
|
||||
dilated = dilation(image, selem)
|
||||
out = erosion(dilated, selem, out=out, shift_x=shift_x, shift_y=shift_y)
|
||||
return out
|
||||
|
||||
def greyscale_white_top_hat(image, selem, out=None):
|
||||
def white_tophat(image, selem, out=None):
|
||||
"""Return white top hat of an image.
|
||||
|
||||
The white top hat of an image is defined as the image minus its
|
||||
@@ -255,7 +257,7 @@ def greyscale_white_top_hat(image, selem, out=None):
|
||||
... [3, 5, 9, 5, 3],
|
||||
... [3, 4, 5, 4, 3],
|
||||
... [2, 3, 3, 3, 2]], dtype=np.uint8)
|
||||
>>> greyscale_white_top_hat(bright_on_grey, square(3))
|
||||
>>> white_tophat(bright_on_grey, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0],
|
||||
[0, 1, 5, 1, 0],
|
||||
@@ -266,11 +268,11 @@ def greyscale_white_top_hat(image, selem, out=None):
|
||||
if image is out:
|
||||
raise NotImplementedError("Cannot perform white top hat in place.")
|
||||
|
||||
out = greyscale_open(image, selem, out=out)
|
||||
out = opening(image, selem, out=out)
|
||||
out = image - out
|
||||
return out
|
||||
|
||||
def greyscale_black_top_hat(image, selem, out=None):
|
||||
def black_tophat(image, selem, out=None):
|
||||
"""Return black top hat of an image.
|
||||
|
||||
The black top hat of an image is defined as its morphological closing minus
|
||||
@@ -304,7 +306,7 @@ def greyscale_black_top_hat(image, selem, out=None):
|
||||
... [6, 4, 0, 4, 6],
|
||||
... [6, 5, 4, 5, 6],
|
||||
... [7, 6, 6, 6, 7]], dtype=np.uint8)
|
||||
>>> greyscale_black_top_hat(dark_on_grey, square(3))
|
||||
>>> black_tophat(dark_on_grey, square(3))
|
||||
array([[0, 0, 0, 0, 0],
|
||||
[0, 0, 1, 0, 0],
|
||||
[0, 1, 5, 1, 0],
|
||||
@@ -314,7 +316,32 @@ def greyscale_black_top_hat(image, selem, out=None):
|
||||
"""
|
||||
if image is out:
|
||||
raise NotImplementedError("Cannot perform white top hat in place.")
|
||||
out = greyscale_close(image, selem, out=out)
|
||||
out = closing(image, selem, out=out)
|
||||
out = out - image
|
||||
return out
|
||||
|
||||
|
||||
def greyscale_erode(*args, **kwargs):
|
||||
warnings.warn("`greyscale_erode` renamed `erosion`.")
|
||||
return erosion(*args, **kwargs)
|
||||
|
||||
def greyscale_dilate(*args, **kwargs):
|
||||
warnings.warn("`greyscale_dilate` renamed `dilation`.")
|
||||
return dilation(*args, **kwargs)
|
||||
|
||||
def greyscale_open(*args, **kwargs):
|
||||
warnings.warn("`greyscale_open` renamed `opening`.")
|
||||
return opening(*args, **kwargs)
|
||||
|
||||
def greyscale_close(*args, **kwargs):
|
||||
warnings.warn("`greyscale_close` renamed `closing`.")
|
||||
return closing(*args, **kwargs)
|
||||
|
||||
def greyscale_white_top_hat(*args, **kwargs):
|
||||
warnings.warn("`greyscale_white_top_hat` renamed `white_tophat`.")
|
||||
return white_tophat(*args, **kwargs)
|
||||
|
||||
def greyscale_black_top_hat(*args, **kwargs):
|
||||
warnings.warn("`greyscale_black_top_hat` renamed `black_tophat`.")
|
||||
return black_tophat(*args, **kwargs)
|
||||
|
||||
|
||||
@@ -4,7 +4,9 @@ import numpy as np
|
||||
from numpy.testing import assert_equal
|
||||
|
||||
from skimage import data_dir
|
||||
from skimage.morphology import *
|
||||
from skimage.morphology.grey import *
|
||||
from skimage.morphology import selem
|
||||
|
||||
|
||||
lena = np.load(os.path.join(data_dir, 'lena_GRAY_U8.npy'))
|
||||
|
||||
@@ -22,43 +24,43 @@ class TestMorphology():
|
||||
|
||||
def test_erode_diamond(self):
|
||||
self.morph_worker(lena, "diamond-erode-matlab-output.npz",
|
||||
greyscale_erode, diamond)
|
||||
erosion, selem.diamond)
|
||||
|
||||
def test_dilate_diamond(self):
|
||||
self.morph_worker(lena, "diamond-dilate-matlab-output.npz",
|
||||
greyscale_dilate, diamond)
|
||||
dilation, selem.diamond)
|
||||
|
||||
def test_open_diamond(self):
|
||||
self.morph_worker(lena, "diamond-open-matlab-output.npz",
|
||||
greyscale_open, diamond)
|
||||
opening, selem.diamond)
|
||||
|
||||
def test_close_diamond(self):
|
||||
self.morph_worker(lena, "diamond-close-matlab-output.npz",
|
||||
greyscale_close, diamond)
|
||||
closing, selem.diamond)
|
||||
|
||||
def test_tophat_diamond(self):
|
||||
self.morph_worker(lena, "diamond-tophat-matlab-output.npz",
|
||||
greyscale_white_top_hat, diamond)
|
||||
white_tophat, selem.diamond)
|
||||
|
||||
def test_bothat_diamond(self):
|
||||
self.morph_worker(lena, "diamond-bothat-matlab-output.npz",
|
||||
greyscale_black_top_hat, diamond)
|
||||
black_tophat, selem.diamond)
|
||||
|
||||
def test_erode_disk(self):
|
||||
self.morph_worker(lena, "disk-erode-matlab-output.npz",
|
||||
greyscale_erode, disk)
|
||||
erosion, selem.disk)
|
||||
|
||||
def test_dilate_disk(self):
|
||||
self.morph_worker(lena, "disk-dilate-matlab-output.npz",
|
||||
greyscale_dilate, disk)
|
||||
dilation, selem.disk)
|
||||
|
||||
def test_open_disk(self):
|
||||
self.morph_worker(lena, "disk-open-matlab-output.npz",
|
||||
greyscale_open, disk)
|
||||
opening, selem.disk)
|
||||
|
||||
def test_close_disk(self):
|
||||
self.morph_worker(lena, "disk-close-matlab-output.npz",
|
||||
greyscale_close, disk)
|
||||
closing, selem.disk)
|
||||
|
||||
|
||||
class TestEccentricStructuringElements():
|
||||
@@ -67,50 +69,50 @@ class TestEccentricStructuringElements():
|
||||
self.black_pixel = 255 * np.ones((4, 4), dtype=np.uint8)
|
||||
self.black_pixel[1, 1] = 0
|
||||
self.white_pixel = 255 - self.black_pixel
|
||||
self.selems = [square(2), rectangle(2, 2),
|
||||
rectangle(2, 1), rectangle(1, 2)]
|
||||
self.selems = [selem.square(2), selem.rectangle(2, 2),
|
||||
selem.rectangle(2, 1), selem.rectangle(1, 2)]
|
||||
|
||||
def test_dilate_erode_symmetry(self):
|
||||
for s in self.selems:
|
||||
c = greyscale_erode(self.black_pixel, s)
|
||||
d = greyscale_dilate(self.white_pixel, s)
|
||||
c = erosion(self.black_pixel, s)
|
||||
d = dilation(self.white_pixel, s)
|
||||
assert np.all(c == (255 - d))
|
||||
|
||||
def test_open_black_pixel(self):
|
||||
for s in self.selems:
|
||||
grey_open = greyscale_open(self.black_pixel, s)
|
||||
grey_open = opening(self.black_pixel, s)
|
||||
assert np.all(grey_open == self.black_pixel)
|
||||
|
||||
def test_close_white_pixel(self):
|
||||
for s in self.selems:
|
||||
grey_close = greyscale_close(self.white_pixel, s)
|
||||
grey_close = closing(self.white_pixel, s)
|
||||
assert np.all(grey_close == self.white_pixel)
|
||||
|
||||
def test_open_white_pixel(self):
|
||||
for s in self.selems:
|
||||
assert np.all(greyscale_open(self.white_pixel, s) == 0)
|
||||
assert np.all(opening(self.white_pixel, s) == 0)
|
||||
|
||||
def test_close_black_pixel(self):
|
||||
for s in self.selems:
|
||||
assert np.all(greyscale_close(self.black_pixel, s) == 255)
|
||||
assert np.all(closing(self.black_pixel, s) == 255)
|
||||
|
||||
def test_white_tophat_white_pixel(self):
|
||||
for s in self.selems:
|
||||
tophat = greyscale_white_top_hat(self.white_pixel, s)
|
||||
tophat = white_tophat(self.white_pixel, s)
|
||||
assert np.all(tophat == self.white_pixel)
|
||||
|
||||
def test_black_tophat_black_pixel(self):
|
||||
for s in self.selems:
|
||||
tophat = greyscale_black_top_hat(self.black_pixel, s)
|
||||
tophat = black_tophat(self.black_pixel, s)
|
||||
assert np.all(tophat == (255 - self.black_pixel))
|
||||
|
||||
def test_white_tophat_black_pixel(self):
|
||||
for s in self.selems:
|
||||
tophat = greyscale_white_top_hat(self.black_pixel, s)
|
||||
tophat = white_tophat(self.black_pixel, s)
|
||||
assert np.all(tophat == 0)
|
||||
|
||||
def test_black_tophat_white_pixel(self):
|
||||
for s in self.selems:
|
||||
tophat = greyscale_black_top_hat(self.white_pixel, s)
|
||||
tophat = black_tophat(self.white_pixel, s)
|
||||
assert np.all(tophat == 0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user