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.array(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_hdx_rgb_roundtrip_float(self): from cucim.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_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_pepper(): seed = 42 cam = img_as_float(camerad) data_signed = cam * 2.0 - 1.0 # Same image, on range [-1, 1] cam_noisy = random_noise(cam, seed=seed, mode='pepper', amount=0.15) peppermask = cam != cam_noisy # Ensure all changes are to 1.0 assert_allclose(cam_noisy[peppermask], cp.zeros(int(peppermask.sum()))) # Ensure approximately correct amount of noise was added proportion = float(peppermask.sum()) / (cam.shape[0] * cam.shape[1]) assert 0.11 < proportion <= 0.15 # Check to make sure pepper gets added properly to signed images orig_zeros = (data_signed == -1).sum() cam_noisy_signed = random_noise(data_signed, seed=seed, mode='pepper', amount=.15) proportion = (float((cam_noisy_signed == -1).sum() - orig_zeros) / (cam.shape[0] * cam.shape[1])) assert 0.11 < proportion <= 0.15
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_rgb2yiq_conversion(self): rgb = img_as_float(self.img_rgb)[::16, ::16] yiq = rgb2yiq(rgb).reshape(-1, 3) gt = np.asarray([colorsys.rgb_to_yiq(pt[0], pt[1], pt[2]) for pt in cp.asnumpy(rgb).reshape(-1, 3)] ) assert_array_almost_equal(yiq, gt, decimal=2)
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.asarray([colorsys.rgb_to_hsv(pt[0], pt[1], pt[2]) for pt in cp.asnumpy(rgb).reshape(-1, 3)]) assert_array_almost_equal(hsv, gt)
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_poisson(): seed = 42 data = camerad # 512x512 grayscale uint8 cam_noisy = random_noise(data, mode='poisson', seed=seed) cam_noisy2 = random_noise(data, mode='poisson', seed=seed, clip=False) cp.random.seed(seed=seed) expected = cp.random.poisson(img_as_float(data) * 256) / 256.0 assert_allclose(cam_noisy, cp.clip(expected, 0.0, 1.0)) assert_allclose(cam_noisy2, expected)
def test_equalize_masked(): img = util.img_as_float(test_img) mask = cp.zeros(test_img.shape) mask[100:400, 100:400] = 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 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(cp.array(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_salt(): seed = 42 cam = img_as_float(camerad) cam_noisy = random_noise(cam, seed=seed, mode='salt', amount=0.15) saltmask = cam != cam_noisy # Ensure all changes are to 1.0 assert_allclose(cam_noisy[saltmask], cp.ones(int(saltmask.sum()))) # Ensure approximately correct amount of noise was added proportion = float(saltmask.sum()) / (cam.shape[0] * cam.shape[1]) assert 0.11 < proportion <= 0.15
def test_adapthist_clip_limit(): img_u = cp.array(data.moon()) img_f = util.img_as_float(img_u) # uint8 input img_clahe0 = exposure.equalize_adapthist(img_u, clip_limit=0) img_clahe1 = exposure.equalize_adapthist(img_u, clip_limit=1) assert_array_equal(img_clahe0, img_clahe1) # float64 input img_clahe0 = exposure.equalize_adapthist(img_f, clip_limit=0) img_clahe1 = exposure.equalize_adapthist(img_f, clip_limit=1) assert_array_equal(img_clahe0, img_clahe1)
def test_adapthist_alpha(): """Test an RGBA color image""" img = util.img_as_float(cp.array(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_borders(): """Test border processing """ img = rgb2gray(util.img_as_float(cp.array(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], # noqa [False, False, False, True, False, False], # noqa [False, False, False, True, False, False], # noqa [False, False, True, True, False, False], # noqa [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_invalid_use_quantiles(self): image = img_as_float(cp.array(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_salt_and_pepper(): seed = 42 cam = img_as_float(camerad) cam_noisy = random_noise(cam, seed=seed, mode='s&p', amount=0.15, salt_vs_pepper=0.25) saltmask = cp.logical_and(cam != cam_noisy, cam_noisy == 1.) peppermask = cp.logical_and(cam != cam_noisy, cam_noisy == 0.) # Ensure all changes are to 0. or 1. assert_allclose(cam_noisy[saltmask], cp.ones(int(saltmask.sum()))) assert_allclose(cam_noisy[peppermask], cp.zeros(int(peppermask.sum()))) # Ensure approximately correct amount of noise was added proportion = float(saltmask.sum() + peppermask.sum()) / (cam.shape[0] * cam.shape[1]) assert 0.11 < proportion <= 0.18 # Verify the relative amount of salt vs. pepper is close to expected assert 0.18 < saltmask.sum() / peppermask.sum() < 0.35
def test_clip_gaussian(): seed = 42 data = camerad # 512x512 grayscale uint8 data_signed = img_as_float( data) * 2.0 - 1.0 # Same image, on range [-1, 1] # Signed and unsigned, clipped cam_gauss = random_noise(data, mode='gaussian', seed=seed, clip=True) cam_gauss2 = random_noise(data_signed, mode='gaussian', seed=seed, clip=True) assert (cam_gauss.max() == 1.0) and (cam_gauss.min() == 0.0) assert (cam_gauss2.max() == 1.0) and (cam_gauss2.min() == -1.0) # Signed and unsigned, unclipped cam_gauss = random_noise(data, mode='gaussian', seed=seed, clip=False) cam_gauss2 = random_noise(data_signed, mode='gaussian', seed=seed, clip=False) assert (cam_gauss.max() > 1.22) and (cam_gauss.min() < -0.33) assert (cam_gauss2.max() > 1.219) and (cam_gauss2.min() < -1.219)
def test_clip_speckle(): seed = 42 data = camerad # 512x512 grayscale uint8 data_signed = img_as_float( data) * 2.0 - 1.0 # Same image, on range [-1, 1] # Signed and unsigned, clipped cam_speckle = random_noise(data, mode='speckle', seed=seed, clip=True) cam_speckle_sig = random_noise(data_signed, mode='speckle', seed=seed, clip=True) assert (cam_speckle.max() == 1.0) and (cam_speckle.min() == 0.0) assert (cam_speckle_sig.max() == 1.0) and (cam_speckle_sig.min() == -1.0) # Signed and unsigned, unclipped cam_speckle = random_noise(data, mode='speckle', seed=seed, clip=False) cam_speckle_sig = random_noise(data_signed, mode='speckle', seed=seed, clip=False) assert (cam_speckle.max() > 1.219) and (cam_speckle.min() == 0.0) assert (cam_speckle_sig.max() > 1.219) and (cam_speckle_sig.min() < -1.219)
def test_clip_poisson(): seed = 42 data = camerad # 512x512 grayscale uint8 data_signed = img_as_float( data) * 2.0 - 1.0 # Same image, on range [-1, 1] # Signed and unsigned, clipped cam_poisson = random_noise(data, mode='poisson', seed=seed, clip=True) cam_poisson2 = random_noise(data_signed, mode='poisson', seed=seed, clip=True) assert (cam_poisson.max() == 1.0) and (cam_poisson.min() == 0.0) assert (cam_poisson2.max() == 1.0) and (cam_poisson2.min() == -1.0) # Signed and unsigned, unclipped cam_poisson = random_noise(data, mode='poisson', seed=seed, clip=False) cam_poisson2 = random_noise(data_signed, mode='poisson', seed=seed, clip=False) assert (cam_poisson.max() > 1.15) and (cam_poisson.min() == 0.0) assert (cam_poisson2.max() > 1.3) and (cam_poisson2.min() == -1.0)
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 _get_lab0(self): rgb = img_as_float(self.img_rgb[:1, :1, :]) return rgb2lab(rgb)[0, 0, :]
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_li_coins_image_as_float(): coins = util.img_as_float(coinsd) assert 94 / 255 < threshold_li(coins) < 95 / 255
def test_otsu_coins_image_as_float(): coins = util.img_as_float(coinsd) assert 0.41 < threshold_otsu(coins) < 0.42
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_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)