def test_adapthist_grayscale_Nd(): """ Test for n-dimensional consistency with float images Note: Currently if img.ndim == 3, img.shape[2] > 4 must hold for the image not to be interpreted as a color image by @adapt_rgb """ # take 2d image, subsample and stack it img = util.img_as_float(cp.asarray(data.astronaut())) img = rgb2gray(img) a = 15 img2d = util.img_as_float(img[0:-1:a, 0:-1:a]) img3d = cp.stack([img2d] * (img.shape[0] // a), axis=0) # apply CLAHE adapted2d = exposure.equalize_adapthist(img2d, kernel_size=5, clip_limit=0.05) adapted3d = exposure.equalize_adapthist(img3d, kernel_size=5, clip_limit=0.05) # check that dimensions of input and output match assert img2d.shape == adapted2d.shape assert img3d.shape == adapted3d.shape # check that the result from the stack of 2d images is similar # to the underlying 2d image assert (cp.mean(cp.abs(adapted2d - adapted3d[adapted3d.shape[0] // 2])) < 0.02)
def test_yuv_roundtrip(self): img_rgb = img_as_float(self.img_rgb)[::16, ::16] assert_array_almost_equal(yuv2rgb(rgb2yuv(img_rgb)), img_rgb) assert_array_almost_equal(yiq2rgb(rgb2yiq(img_rgb)), img_rgb) assert_array_almost_equal(ypbpr2rgb(rgb2ypbpr(img_rgb)), img_rgb) assert_array_almost_equal(ycbcr2rgb(rgb2ycbcr(img_rgb)), img_rgb) assert_array_almost_equal(ydbdr2rgb(rgb2ydbdr(img_rgb)), img_rgb)
def test_rgb_lch_roundtrip(self): rgb = img_as_float(self.img_rgb) lab = rgb2lab(rgb) lch = lab2lch(lab) lab2 = lch2lab(lch) rgb2 = lab2rgb(lab2) assert_array_almost_equal(rgb, rgb2)
def test_hdx_rgb_roundtrip_float(self): from cupyimg.skimage.color.colorconv import hdx_from_rgb, rgb_from_hdx img_rgb = img_as_float(self.img_rgb) conv = combine_stains(separate_stains(img_rgb, hdx_from_rgb), rgb_from_hdx) assert_array_almost_equal(conv, img_rgb)
def test_rgb2yiq_conversion(self): rgb = img_as_float(self.img_rgb)[::16, ::16] yiq = rgb2yiq(rgb).reshape(-1, 3) gt = cp.asarray([ colorsys.rgb_to_yiq(pt[0], pt[1], pt[2]) for pt in rgb.reshape(-1, 3).get() ]) assert_array_almost_equal(yiq, gt, decimal=2)
def test_dtype(self): """Check that the same output is produced regardless of image dtype.""" image_uint8 = cp.asarray(data.camera()) image_float = img_as_float(image_uint8) result_uint8 = feature.canny(image_uint8) result_float = feature.canny(image_float) assert_array_equal(result_uint8, result_float)
def test_rgb2hsv_conversion(self): rgb = img_as_float(self.img_rgb)[::16, ::16] hsv = rgb2hsv(rgb).reshape(-1, 3) # ground truth from colorsys gt = np.array([ colorsys.rgb_to_hsv(pt[0], pt[1], pt[2]) for pt in rgb.reshape(-1, 3).get() ]) assert_array_almost_equal(hsv, gt)
def test_equalize_masked(): img = util.img_as_float(test_img) mask = cp.zeros(test_img.shape) mask[50:150, 50:250] = 1 img_mask_eq = exposure.equalize_hist(img, mask=mask) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_mask_eq) check_cdf_slope(cdf) assert not (img_eq == img_mask_eq).all()
def test_adapthist_clip_limit(): img_u = data.moon() img_f = util.img_as_float(img_u) # uint8 input img_clahe = exposure.equalize_adapthist(img_u, clip_limit=1) assert_array_equal(img_f, img_clahe) # float64 input img_clahe = exposure.equalize_adapthist(img_f, clip_limit=1) assert_array_equal(img_f, img_clahe)
def peak_snr(img1, img2): """Peak signal to noise ratio of two images Parameters ---------- img1 : array-like img2 : array-like Returns ------- peak_snr : float Peak signal to noise ratio """ if img1.ndim == 3: img1, img2 = rgb2gray(img1.copy()), rgb2gray(img2.copy()) img1 = util.img_as_float(img1) img2 = util.img_as_float(img2) mse = 1.0 / img1.size * cp.square(img1 - img2).sum() _, max_ = dtype_range[img1.dtype.type] return 20 * cp.log(max_ / mse)
def test_adapthist_grayscale(): """Test a grayscale float image""" img = util.img_as_float(data.astronaut()) img = rgb2gray(img) img = cp.dstack((img, img, img)) adapted = exposure.equalize_adapthist(img, kernel_size=(57, 51), clip_limit=0.01, nbins=128) assert img.shape == adapted.shape assert_almost_equal(float(peak_snr(img, adapted)), 100.140, 3) assert_almost_equal(float(norm_brightness_err(img, adapted)), 0.0529, 3)
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_invalid_use_quantiles(self): image = img_as_float(cp.asarray(data.camera()[::50, ::50])) self.assertRaises( ValueError, feature.canny, image, use_quantiles=True, low_threshold=0.5, high_threshold=3.6, ) self.assertRaises( ValueError, feature.canny, image, use_quantiles=True, low_threshold=-5, high_threshold=0.5, ) self.assertRaises( ValueError, feature.canny, image, use_quantiles=True, low_threshold=99, high_threshold=0.9, ) self.assertRaises( ValueError, feature.canny, image, use_quantiles=True, low_threshold=0.5, high_threshold=-100, ) # Example from issue #4282 image = data.camera() self.assertRaises( ValueError, feature.canny, image, use_quantiles=True, low_threshold=50, high_threshold=150, )
def test_adapthist_borders(): """Test border processing""" img = rgb2gray(cp.asarray(util.img_as_float(data.astronaut()))) # maximize difference between orig and processed img img /= 100.0 img[img.shape[0] // 2, img.shape[1] // 2] = 1.0 # check borders are processed for different kernel sizes border_index = -1 for kernel_size in range(51, 71, 2): adapted = exposure.equalize_adapthist(img, kernel_size, clip_limit=0.5) # Check last columns are processed assert (norm_brightness_err(adapted[:, border_index], img[:, border_index]) > 0.1) # Check last rows are processed assert (norm_brightness_err(adapted[border_index, :], img[border_index, :]) > 0.1)
def test_use_quantiles(self): image = img_as_float(cp.asarray(data.camera()[::100, ::100])) # Correct output produced manually with quantiles # of 0.8 and 0.6 for high and low respectively correct_output = cp.asarray([ [False, False, False, False, False, False], [False, True, True, True, False, False], [False, False, False, True, False, False], [False, False, False, True, False, False], [False, False, True, True, False, False], [False, False, False, False, False, False], ], ) result = feature.canny(image, low_threshold=0.6, high_threshold=0.8, use_quantiles=True) assert_array_equal(result, correct_output)
def test_yen_coins_image_as_float(): coins = util.img_as_float(coinsd) assert 0.43 < threshold_yen(coins) < 0.44
def test_hed_rgb_float_roundtrip(self): img_rgb = img_as_float(self.img_rgb) assert_array_almost_equal(hed2rgb(rgb2hed(img_rgb)), img_rgb)
def test_xyz_rgb_roundtrip(self): img_rgb = img_as_float(self.img_rgb) assert_array_almost_equal(xyz2rgb(rgb2xyz(img_rgb)), img_rgb)
def test_lab_lch_roundtrip(self): rgb = img_as_float(self.img_rgb) lab = rgb2lab(rgb) lab2 = lch2lab(lab2lch(lab)) assert_array_almost_equal(lab2, lab)
def test_luv_rgb_roundtrip(self): img_rgb = img_as_float(self.img_rgb) assert_array_almost_equal(luv2rgb(rgb2luv(img_rgb)), img_rgb)
def test_otsu_coins_image_as_float(): coins_ubyte = util.img_as_float(coinsd) assert 0.41 < threshold_otsu(coins_ubyte) < 0.42
def _get_lab0(self): rgb = img_as_float(self.img_rgb[:1, :1, :]) return rgb2lab(rgb)[0, 0, :]
def test_equalize_float(): img = util.img_as_float(test_img) img_eq = exposure.equalize_hist(img) cdf, bin_edges = exposure.cumulative_distribution(img_eq) check_cdf_slope(cdf)
frequencies, bin_centers = exposure.histogram(im, 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)
import cupy as cp import numpy as np from skimage.data import binary_blobs from skimage.data import camera, chelsea from cupyimg.skimage.metrics import mean_squared_error as mse from cupyimg.skimage.restoration import calibrate_denoiser # from cupyimg.skimage.restoration import denoise_wavelet from skimage.restoration import denoise_wavelet from cupyimg.skimage.restoration import denoise_tv_chambolle from cupyimg.skimage.restoration.j_invariant import _invariant_denoise from cupyimg.skimage.util import img_as_float, random_noise test_img = img_as_float(cp.asarray(camera())) test_img_color = img_as_float(cp.asarray(chelsea())) test_img_3d = img_as_float(cp.asarray(binary_blobs(64, n_dim=3))) / 2 noisy_img = random_noise(test_img, mode="gaussian", var=0.01) noisy_img_color = random_noise(test_img_color, mode="gaussian", var=0.01) noisy_img_3d = random_noise(test_img_3d, mode="gaussian", var=0.1) # TODO: replace with CuPy version once completed def _denoise_wavelet(image, rescale_sigma=True, **kwargs): return cp.asarray( denoise_wavelet(cp.asnumpy(image), rescale_sigma=rescale_sigma, **kwargs)) def test_invariant_denoise():
def test_li_coins_image_as_float(): coins_float = util.img_as_float(coinsd) assert 94 / 255 < threshold_li(coins_float) < 95 / 255
def test_rgb2hsv_dtype(self): rgb = img_as_float(self.img_rgb) rgb32 = img_as_float32(self.img_rgb) assert rgb2hsv(rgb).dtype == rgb.dtype assert rgb2hsv(rgb32).dtype == rgb32.dtype