mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-06 03:41:10 +08:00
Update adapthist tests, add test for alpha channel and rgb mode handling.
This commit is contained in:
@@ -11,6 +11,7 @@ from skimage.exposure.exposure import intensity_range
|
||||
from skimage.color import rgb2gray
|
||||
from skimage.util.dtype import dtype_range
|
||||
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
# Test histogram equalization
|
||||
# ===========================
|
||||
@@ -147,13 +148,13 @@ def test_adapthist_scalar():
|
||||
'''
|
||||
img = skimage.img_as_ubyte(data.moon())
|
||||
adapted = exposure.equalize_adapthist(img, clip_limit=0.02)
|
||||
assert adapted.min() == 0
|
||||
assert adapted.max() == (1 << 16) - 1
|
||||
assert adapted.min() == 0.0
|
||||
assert adapted.max() == 1.0
|
||||
assert img.shape == adapted.shape
|
||||
full_scale = skimage.exposure.rescale_intensity(skimage.img_as_uint(img))
|
||||
full_scale = skimage.exposure.rescale_intensity(skimage.img_as_float(img))
|
||||
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
assert_almost_equal(peak_snr(full_scale, adapted), 101.231, 3)
|
||||
assert_almost_equal(peak_snr(full_scale, adapted), 101.2295, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_scale, adapted),
|
||||
0.041, 3)
|
||||
return img, adapted
|
||||
@@ -169,8 +170,8 @@ def test_adapthist_grayscale():
|
||||
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)
|
||||
assert_almost_equal(norm_brightness_err(img, adapted), 0.0313, 3)
|
||||
assert_almost_equal(peak_snr(img, adapted), 101.4070, 3)
|
||||
assert_almost_equal(norm_brightness_err(img, adapted), 0.03856, 3)
|
||||
return data, adapted
|
||||
|
||||
|
||||
@@ -183,19 +184,36 @@ def test_adapthist_color():
|
||||
hist, bin_centers = exposure.histogram(img)
|
||||
assert len(w) > 0
|
||||
adapted = exposure.equalize_adapthist(img, clip_limit=0.01)
|
||||
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
assert adapted.min() == 0
|
||||
assert adapted.max() == 1.0
|
||||
assert img.shape == adapted.shape
|
||||
full_scale = skimage.exposure.rescale_intensity(img)
|
||||
assert_almost_equal(peak_snr(full_scale, adapted), 102.940, 3)
|
||||
assert_almost_equal(peak_snr(full_scale, adapted), 106.865, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_scale, adapted),
|
||||
0.0110, 3)
|
||||
0.0509, 3)
|
||||
return data, adapted
|
||||
|
||||
|
||||
def test_adapthist_modes():
|
||||
'''Test adaptist `mode` parameter values.
|
||||
def test_adapthist_alpha():
|
||||
'''Test an RGBA color image
|
||||
'''
|
||||
img = skimage.img_as_float(data.lena())
|
||||
alpha = np.ones((img.shape[0], img.shape[1]), dtype=float)
|
||||
img = np.dstack((img, alpha))
|
||||
adapted = exposure.equalize_adapthist(img)
|
||||
assert adapted.shape != img.shape
|
||||
img = img[:, :, :3]
|
||||
full_scale = skimage.exposure.rescale_intensity(img)
|
||||
assert img.shape == adapted.shape
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
assert_almost_equal(peak_snr(full_scale, adapted), 106.862, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_scale, adapted), 0.0509, 3)
|
||||
|
||||
|
||||
def test_adapthist_modes_scalar():
|
||||
'''Test adaptist `mode` parameter values for ndim==2 image.
|
||||
'''
|
||||
img = skimage.img_as_float(data.moon())
|
||||
full_scale = skimage.exposure.rescale_intensity(skimage.img_as_uint(img))
|
||||
@@ -211,12 +229,40 @@ def test_adapthist_modes():
|
||||
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
full_cropped = full_scale[:crop.shape[0], :crop.shape[1]]
|
||||
assert_almost_equal(peak_snr(full_scale, ignore), 56.175, 3)
|
||||
assert_almost_equal(peak_snr(full_scale, zero), 94.7988, 3)
|
||||
assert_almost_equal(peak_snr(full_cropped, crop), 120.4598, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_scale, ignore), 0.223, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_scale, zero), 0.01589, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_cropped, crop), 0.02878, 3)
|
||||
zero_cropped = zero[:crop.shape[0], :crop.shape[1]]
|
||||
ignore_cropped = ignore[:crop.shape[0], :crop.shape[1]]
|
||||
|
||||
for crop_img in [ignore_cropped, zero_cropped, crop]:
|
||||
assert_almost_equal(peak_snr(full_cropped, crop_img), 120.456, 3)
|
||||
assert_almost_equal(norm_brightness_err(full_cropped, crop_img),
|
||||
0.4398, 3)
|
||||
|
||||
|
||||
def test_adapthist_modes_rgb():
|
||||
'''Test adaptist `mode` parameter values for rgb image.
|
||||
'''
|
||||
img = skimage.img_as_float(data.lena())
|
||||
full_scale = skimage.exposure.rescale_intensity(skimage.img_as_uint(img))
|
||||
ignore = exposure.equalize_adapthist(img.copy(), ntiles_x=9, ntiles_y=10)
|
||||
zero = exposure.equalize_adapthist(img.copy(), ntiles_x=9, ntiles_y=10,
|
||||
mode='zero')
|
||||
crop = exposure.equalize_adapthist(img.copy(), ntiles_x=9, ntiles_y=10,
|
||||
mode='crop')
|
||||
assert ignore.shape == zero.shape
|
||||
assert ignore.shape != crop.shape
|
||||
|
||||
assert_array_equal(zero[crop.shape[0]:, :, :], 0)
|
||||
assert_array_equal(zero[:, crop.shape[1]:, :], 0)
|
||||
|
||||
assert_almost_equal = np.testing.assert_almost_equal
|
||||
full_cropped = full_scale[:crop.shape[0], :crop.shape[1], :]
|
||||
zero_cropped = zero[:crop.shape[0], :crop.shape[1], :]
|
||||
ignore_cropped = ignore[:crop.shape[0], :crop.shape[1], :]
|
||||
|
||||
for crop_img in [ignore_cropped, zero_cropped, crop]:
|
||||
assert np.floor(peak_snr(full_cropped, crop_img)) == 106
|
||||
assert_almost_equal(norm_brightness_err(full_cropped, crop_img),
|
||||
0.0517, 3)
|
||||
|
||||
|
||||
def peak_snr(img1, img2):
|
||||
@@ -260,6 +306,8 @@ def norm_brightness_err(img1, img2):
|
||||
nbe = ambe / dtype_range[img1.dtype.type][1]
|
||||
return nbe
|
||||
|
||||
test_adapthist_modes_rgb()
|
||||
|
||||
|
||||
# Test Gamma Correction
|
||||
# =====================
|
||||
@@ -431,6 +479,6 @@ def test_negative():
|
||||
assert_raises(ValueError, exposure.adjust_gamma, image)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
from numpy import testing
|
||||
testing.run_module_suite()
|
||||
#if __name__ == '__main__':
|
||||
# from numpy import testing
|
||||
# testing.run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user