def test_rescale_nan_warning(in_range, out_range): image = cp.arange(12, dtype=float).reshape(3, 4) image[1, 1] = np.nan msg = (r"One or more intensity levels are NaN\." r" Rescaling will broadcast NaN to the full image\.") with expected_warnings([msg]): exposure.rescale_intensity(image, in_range, out_range)
def test_rescale_out_range(): """Check that output range is correct. .. versionchanged:: 0.17 This function used to return dtype matching the input dtype. It now matches the output. """ image = cp.asarray([-10, 0, 10], dtype=np.int8) out = exposure.rescale_intensity(image, out_range=(0, 127)) assert out.dtype == np.float_ assert_array_almost_equal(out, [0, 63.5, 127])
def test_adapthist_alpha(): """Test an RGBA color image""" img = util.img_as_float(data.astronaut()) alpha = cp.ones((img.shape[0], img.shape[1]), dtype=float) img = cp.dstack((img, alpha)) adapted = exposure.equalize_adapthist(img) assert adapted.shape != img.shape img = img[:, :, :3] full_scale = exposure.rescale_intensity(img) assert img.shape == adapted.shape assert_almost_equal(float(peak_snr(full_scale, adapted)), 109.393, 2) assert_almost_equal(float(norm_brightness_err(full_scale, adapted)), 0.0248, 3)
def test_adapthist_color(): """Test an RGB color uint16 image""" img = util.img_as_uint(data.astronaut()) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") hist, bin_centers = exposure.histogram(img) assert len(w) > 0 adapted = exposure.equalize_adapthist(img, clip_limit=0.01) assert adapted.min() == 0 assert adapted.max() == 1.0 assert img.shape == adapted.shape full_scale = exposure.rescale_intensity(img) assert_almost_equal(float(peak_snr(full_scale, adapted)), 109.393, 1) assert_almost_equal(float(norm_brightness_err(full_scale, adapted)), 0.02, 2) return data, adapted
def test_rescale_output_dtype(out_range, out_dtype): image = cp.asarray([-128, 0, 127], dtype=np.int8) output_image = exposure.rescale_intensity(image, out_range=out_range) assert output_image.dtype == out_dtype
def test_rescale_same_values(): image = cp.ones((2, 2)) out = exposure.rescale_intensity(image) assert ~cp.isnan(out).all() assert_array_almost_equal(out, image)
def test_rescale_constant(): image = cp.asarray([130, 130], dtype=np.uint16) out = exposure.rescale_intensity(image, out_range=(0, 127)) assert_array_almost_equal(out, [127, 127])
def test_rescale_all_zeros(): image = cp.zeros((2, 2), dtype=np.uint8) out = exposure.rescale_intensity(image) assert ~cp.isnan(out).all() assert_array_almost_equal(out, image)
def test_rescale_uint14_limits(): image = cp.asarray([0, uint16_max], dtype=np.uint16) out = exposure.rescale_intensity(image, out_range="uint14") assert_array_almost_equal(out, [0, uint14_max])
def test_rescale_in_range_clip(): image = cp.asarray([51.0, 102.0, 153.0]) out = exposure.rescale_intensity(image, in_range=(0, 102)) assert_array_almost_equal(out, [0.5, 1, 1])
def test_rescale_in_range(): image = cp.asarray([51.0, 102.0, 153.0]) out = exposure.rescale_intensity(image, in_range=(0, 255)) assert_array_almost_equal(out, [0.2, 0.4, 0.6])
def test_rescale_shrink(): image = cp.asarray([51.0, 102.0, 153.0]) out = exposure.rescale_intensity(image) assert_array_almost_equal(out, [0, 0.5, 1])
def test_rescale_stretch(): image = cp.asarray([51, 102, 153], dtype=np.uint8) out = exposure.rescale_intensity(image) assert out.dtype == np.uint8 assert_array_almost_equal(out, [0, 127, 255])
source_range="dtype", normalize=True) expected /= 3.0 assert_array_equal(frequencies, expected) # Test histogram equalization # =========================== np.random.seed(0) test_img_int = data.camera() test_img_int = cp.asarray(test_img_int) # squeeze image intensities to lower image contrast test_img = util.img_as_float(test_img_int) test_img = exposure.rescale_intensity(test_img / 5.0 + 100) test_img = cp.asarray(test_img) def test_equalize_uint8_approx(): """Check integer bins used for uint8 images.""" img_eq0 = exposure.equalize_hist(test_img_int) img_eq1 = exposure.equalize_hist(test_img_int, nbins=3) cp.testing.assert_allclose(img_eq0, img_eq1) def test_equalize_ubyte(): img = util.img_as_ubyte(test_img) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_eq)
def test_rescale_float_output(): image = cp.asarray([-128, 0, 127], dtype=np.int8) output_image = exposure.rescale_intensity(image, out_range=(0, 255)) cp.testing.assert_array_equal(output_image, [0, 128, 255]) assert output_image.dtype == np.float_
def test_rescale_raises_on_incorrect_out_range(): image = cp.asarray([-128, 0, 127], dtype=np.int8) with pytest.raises(ValueError): _ = exposure.rescale_intensity(image, out_range="flat")
def test_rescale_named_in_range(): image = cp.asarray([0, uint10_max, uint10_max + 100], dtype=np.uint16) out = exposure.rescale_intensity(image, in_range="uint10") assert_array_almost_equal(out, [0, uint16_max, uint16_max])