mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-14 11:18:06 +08:00
Add tests for intensity_range and rename parameter
This commit is contained in:
@@ -3,7 +3,7 @@ import numpy as np
|
||||
|
||||
from skimage import img_as_float
|
||||
from skimage.util.dtype import dtype_range, dtype_limits
|
||||
from skimage._shared.utils import deprecated, deprecation_warning
|
||||
from skimage._shared.utils import deprecation_warning
|
||||
|
||||
|
||||
__all__ = ['histogram', 'cumulative_distribution', 'equalize',
|
||||
@@ -136,7 +136,7 @@ def equalize_hist(image, nbins=256):
|
||||
return out.reshape(image.shape)
|
||||
|
||||
|
||||
def intensity_range(image, range_values='image', zero_min=False):
|
||||
def intensity_range(image, range_values='image', clip_negative=False):
|
||||
"""Return image intensity range (min, max) based on desired value type.
|
||||
|
||||
Parameters
|
||||
@@ -160,9 +160,9 @@ def intensity_range(image, range_values='image', zero_min=False):
|
||||
intensity range explicitly. This option is included for functions
|
||||
that use `intensity_range` to support all desired range types.
|
||||
|
||||
zero_min : bool
|
||||
If True, the image dtype's min is truncated to 0. Note that this only
|
||||
applies to the output range if `range_values` specifies a dtype.
|
||||
clip_negative : bool
|
||||
If True, clip the negative range (i.e. return 0 for min intensity)
|
||||
even if the image dtype allows negative values.
|
||||
"""
|
||||
if range_values == 'dtype':
|
||||
range_values = image.dtype.type
|
||||
@@ -172,7 +172,7 @@ def intensity_range(image, range_values='image', zero_min=False):
|
||||
i_max = np.max(image)
|
||||
elif range_values in DTYPE_RANGE:
|
||||
i_min, i_max = DTYPE_RANGE[range_values]
|
||||
if zero_min:
|
||||
if clip_negative:
|
||||
i_min = 0
|
||||
else:
|
||||
i_min, i_max = range_values
|
||||
@@ -263,7 +263,7 @@ def rescale_intensity(image, in_range='image', out_range='dtype'):
|
||||
deprecation_warning(msg.format(out_range))
|
||||
|
||||
imin, imax = intensity_range(image, in_range)
|
||||
omin, omax = intensity_range(image, out_range, zero_min=(imin >= 0))
|
||||
omin, omax = intensity_range(image, out_range, clip_negative=(imin >= 0))
|
||||
|
||||
image = np.clip(image, imin, imax)
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@ import warnings
|
||||
import numpy as np
|
||||
from numpy.testing import assert_array_almost_equal as assert_close
|
||||
from numpy.testing import assert_array_equal, assert_raises
|
||||
|
||||
import skimage
|
||||
from skimage import data
|
||||
from skimage import exposure
|
||||
from skimage.exposure.exposure import intensity_range
|
||||
from skimage.color import rgb2gray
|
||||
from skimage.util.dtype import dtype_range
|
||||
|
||||
@@ -41,6 +43,36 @@ def check_cdf_slope(cdf):
|
||||
assert 0.9 < slope < 1.1
|
||||
|
||||
|
||||
# Test intensity range
|
||||
# ====================
|
||||
|
||||
|
||||
def test_intensity_range_uint8():
|
||||
image = np.array([0, 1], dtype=np.uint8)
|
||||
input_and_expected = [('image', [0, 1]),
|
||||
('dtype', [0, 255]),
|
||||
((10, 20), [10, 20])]
|
||||
for range_values, expected_values in input_and_expected:
|
||||
out = intensity_range(image, range_values=range_values)
|
||||
yield assert_array_equal, out, expected_values
|
||||
|
||||
|
||||
def test_intensity_range_float():
|
||||
image = np.array([0.1, 0.2], dtype=np.float64)
|
||||
input_and_expected = [('image', [0.1, 0.2]),
|
||||
('dtype', [-1, 1]),
|
||||
((0.3, 0.4), [0.3, 0.4])]
|
||||
for range_values, expected_values in input_and_expected:
|
||||
out = intensity_range(image, range_values=range_values)
|
||||
yield assert_array_equal, out, expected_values
|
||||
|
||||
|
||||
def test_intensity_range_clipped_float():
|
||||
image = np.array([0.1, 0.2], dtype=np.float64)
|
||||
out = intensity_range(image, range_values='dtype', clip_negative=True)
|
||||
assert_array_equal(out, (0, 1))
|
||||
|
||||
|
||||
# Test rescale intensity
|
||||
# ======================
|
||||
|
||||
@@ -134,7 +166,7 @@ def test_adapthist_grayscale():
|
||||
img = rgb2gray(img)
|
||||
img = np.dstack((img, img, img))
|
||||
adapted = exposure.equalize_adapthist(img, 10, 9, clip_limit=0.01,
|
||||
nbins=128)
|
||||
nbins=128)
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
assert img.shape == adapted.shape
|
||||
assert_almost_equal(peak_snr(img, adapted), 97.531, 3)
|
||||
@@ -374,6 +406,6 @@ def test_adjust_inv_sigmoid_cutoff_half():
|
||||
assert_array_equal(result, expected)
|
||||
|
||||
|
||||
def test_neggative():
|
||||
def test_negative():
|
||||
image = np.arange(-10, 245, 4).reshape(8, 8).astype(np.double)
|
||||
assert_raises(ValueError, exposure.adjust_gamma, image)
|
||||
|
||||
Reference in New Issue
Block a user