def test_all_negative_image(): im = cp.array([-100, -1], dtype=np.int8) frequencies, bin_centers = exposure.histogram(im) assert_array_equal(bin_centers, cp.arange(-100, 0)) assert frequencies[0] == 1 assert frequencies[-1] == 1 assert_array_equal(frequencies[1:-1], 0)
def test_normalize(): im = cp.array([0, 255, 255], dtype=np.uint8) frequencies, bin_centers = exposure.histogram(im, source_range='dtype', normalize=False) expected = cp.zeros(256) expected[0] = 1 expected[-1] = 2 assert_array_equal(frequencies, expected) frequencies, bin_centers = exposure.histogram(im, source_range='dtype', normalize=True) expected /= 3.0 assert_array_equal(frequencies, expected)
def test_peak_int_range_dtype(): im = cp.array([10, 100], dtype=np.int8) frequencies, bin_centers = exposure.histogram(im, source_range="dtype") assert_array_equal(bin_centers, cp.arange(-128, 128)) assert frequencies[128 + 10] == 1 assert frequencies[128 + 100] == 1 assert frequencies[128 + 101] == 0 assert frequencies.shape == (256, )
def test_peak_float_out_of_range_dtype(): im = cp.array([10, 100], dtype=np.float16) nbins = 10 frequencies, bin_centers = exposure.histogram(im, nbins=nbins, source_range='dtype') assert_almost_equal(cp.min(bin_centers).get(), -0.9, 3) assert_almost_equal(cp.max(bin_centers).get(), 0.9, 3) assert len(bin_centers) == 10
def test_dask_histogram(): pytest.importorskip('dask', reason="dask python library is not installed") import dask.array as da dask_array = da.from_array(cp.array([[0, 1], [1, 2]]), chunks=(1, 2)) output_hist, output_bins = exposure.histogram(dask_array) expected_bins = [0, 1, 2] expected_hist = [1, 2, 1] assert cp.allclose(expected_bins, output_bins) assert cp.allclose(expected_hist, output_hist)
def test_multiotsu_lut(): for classes in [2, 3, 4]: for name in ['camera', 'moon', 'coins', 'text', 'clock', 'page']: img = cp.array(getattr(data, name)()) prob, bin_centers = histogram(img.ravel(), nbins=256, source_range='image', normalize=True) prob = prob.astype('float32') result_lut = _get_multiotsu_thresh_indices_lut(prob, classes - 1) result = _get_multiotsu_thresh_indices(prob, classes - 1) assert_array_equal(result_lut, result)
def test_adapthist_color(): """Test an RGB color uint16 image """ img = util.img_as_uint(cp.array(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_threshold_minimum_counts(): camera = util.img_as_ubyte(camerad) counts, bin_centers = histogram(camera.ravel(), 256, source_range='image') threshold = threshold_minimum(hist=counts) assert_array_equal(threshold, 85)
def test_threshold_minimum_histogram(): camera = util.img_as_ubyte(camerad) hist = histogram(camera.ravel(), 256, source_range='image') threshold = threshold_minimum(hist=hist) assert_array_equal(threshold, 85)
def test_isodata_camera_image_counts(): camera = util.img_as_ubyte(camerad) counts, bin_centers = histogram(camera.ravel(), 256, source_range='image') threshold = threshold_isodata(hist=counts) assert threshold == 102
def test_isodata_camera_image_histogram(): camera = util.img_as_ubyte(camerad) hist = histogram(camera.ravel(), 256, source_range='image') threshold = threshold_isodata(hist=hist) assert threshold == 102
def test_yen_camera_image_counts(): camera = util.img_as_ubyte(camerad) counts, bin_centers = histogram(camera.ravel(), 256, source_range='image') assert 145 < threshold_yen(hist=counts) < 147
def test_yen_camera_image_histogram(): camera = util.img_as_ubyte(camerad) hist = histogram(camera.ravel(), 256, source_range="image") assert 145 < threshold_yen(hist=hist) < 147
def test_otsu_camera_image_counts(): camera = util.img_as_ubyte(camerad) counts, bin_centers = histogram(camera.ravel(), 256, source_range="image") assert 101 < threshold_otsu(hist=counts) < 103
def test_otsu_camera_image_histogram(): camera = util.img_as_ubyte(camerad) hist = histogram(camera.ravel(), 256, source_range="image") assert 101 < threshold_otsu(hist=hist) < 103
def test_wrong_source_range(): im = cp.array([-1, 100], dtype=np.int8) with pytest.raises(ValueError): frequencies, bin_centers = exposure.histogram(im, source_range="foobar")
def test_peak_float_out_of_range_image(): im = cp.array([10, 100], dtype=np.float16) frequencies, bin_centers = exposure.histogram(im, nbins=90) # offset values by 0.5 for float... assert_array_equal(bin_centers, cp.arange(10, 100) + 0.5)
def test_flat_int_range_dtype(): im = cp.linspace(-128, 128, 256, dtype=np.int8) frequencies, bin_centers = exposure.histogram(im, source_range="dtype") assert_array_equal(bin_centers, cp.arange(-128, 128)) assert frequencies.shape == (256, )
def test_int_range_image(): im = cp.array([10, 100], dtype=np.int8) frequencies, bin_centers = exposure.histogram(im) assert len(bin_centers) == len(frequencies) assert bin_centers[0] == 10 assert bin_centers[-1] == 100