def test_masked_registration_padfield_data():
    """Masked translation registration should behave like in the original
    publication"""
    # Test translated from MATLABimplementation `MaskedFFTRegistrationTest`
    # file. You can find the source code here:
    # http://www.dirkpadfield.com/Home/MaskedFFTRegistrationCode.zip

    shifts = [(75, 75), (-130, 130), (130, 130)]
    for xi, yi in shifts:

        fixed_image = cp.array(imread(
            fetch('registration/tests/data/OriginalX{:d}Y{:d}.png'
                  ''.format(xi, yi))))
        moving_image = cp.array(imread(
            fetch('registration/tests/data/TransformedX{:d}Y{:d}.png'
                  ''.format(xi, yi))))

        # Valid pixels are 1
        fixed_mask = fixed_image != 0
        moving_mask = moving_image != 0

        # Note that shifts in x and y and shifts in cols and rows
        shift_y, shift_x = cp.asnumpy(masked_register_translation(
            fixed_image, moving_image, reference_mask=fixed_mask,
            moving_mask=moving_mask, overlap_ratio=0.1))
        # Note: by looking at the test code from Padfield's
        # MaskedFFTRegistrationCode repository, the
        # shifts were not xi and yi, but xi and -yi
        np.testing.assert_array_equal((shift_x, shift_y), (-xi, yi))
Example #2
0
    def test_lab2xyz(self):
        assert_array_almost_equal(lab2xyz(self.lab_array),
                                  self.xyz_array, decimal=3)

        # Test the conversion with the rest of the illuminants.
        for i in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/lab_array_{0}_{1}.npy".format(i, obs)
                lab_array_i_obs = cp.array(np.load(fetch(fname)))
                assert_array_almost_equal(lab2xyz(lab_array_i_obs, i, obs),
                                          self.xyz_array, decimal=3)
        for i in ["a", "e"]:
            fname = "lab_array_{0}_2.npy".format(i)
            lab_array_i_obs = cp.array(
                np.load(fetch('color/tests/data/' + fname)))
            assert_array_almost_equal(lab2xyz(lab_array_i_obs, i, "2"),
                                      self.xyz_array, decimal=3)

        # And we include a call to test the exception handling in the code.
        try:
            lab2xyz(lab_array_i_obs, "Nai", "2")  # Not an illuminant
        except ValueError:
            pass

        try:
            lab2xyz(lab_array_i_obs, "d50", "42")  # Not a degree
        except ValueError:
            pass
Example #3
0
def test_wiener(dtype):
    psf = np.ones((5, 5)) / 25
    data = signal.convolve2d(cp.asnumpy(test_img), psf, "same")
    np.random.seed(0)
    data += 0.1 * data.std() * np.random.standard_normal(data.shape)

    psf = cp.asarray(psf, dtype=dtype)
    data = cp.asarray(data, dtype=dtype)

    deconvolved = restoration.wiener(data, psf, 0.05)
    assert deconvolved.dtype == dtype

    path = fetch('restoration/tests/camera_wiener.npy')
    rtol = 1e-5 if dtype == np.float32 else 1e-12
    atol = rtol
    cp.testing.assert_allclose(deconvolved,
                               np.load(path),
                               rtol=rtol,
                               atol=atol)

    _, laplacian = uft.laplacian(2, data.shape)
    otf = uft.ir2tf(psf, data.shape, is_real=False)
    deconvolved = restoration.wiener(data,
                                     otf,
                                     0.05,
                                     reg=laplacian,
                                     is_real=False)
    cp.testing.assert_allclose(cp.real(deconvolved),
                               np.load(path),
                               rtol=rtol,
                               atol=atol)
Example #4
0
def load_ciede2000_data():
    dtype = [('pair', int),
             ('1', int),
             ('L1', float),
             ('a1', float),
             ('b1', float),
             ('a1_prime', float),
             ('C1_prime', float),
             ('h1_prime', float),
             ('hbar_prime', float),
             ('G', float),
             ('T', float),
             ('SL', float),
             ('SC', float),
             ('SH', float),
             ('RT', float),
             ('dE', float),
             ('2', int),
             ('L2', float),
             ('a2', float),
             ('b2', float),
             ('a2_prime', float),
             ('C2_prime', float),
             ('h2_prime', float),
             ]

    # note: ciede_test_data.txt contains several intermediate quantities
    path = fetch('color/tests/ciede2000_test_data.txt')
    return np.loadtxt(path, dtype=dtype)
Example #5
0
    def test_luv2xyz(self):
        assert_array_almost_equal(luv2xyz(self.luv_array),
                                  self.xyz_array, decimal=3)

        # Test the conversion with the rest of the illuminants.
        for i in ["d50", "d55", "d65", "d75"]:
            for obs in ["2", "10"]:
                fname = "color/tests/data/luv_array_{0}_{1}.npy".format(i, obs)
                luv_array_i_obs = cp.array(np.load(fetch(fname)))
                assert_array_almost_equal(luv2xyz(luv_array_i_obs, i, obs),
                                          self.xyz_array, decimal=3)
        for i in ["a", "e"]:
            fname = "color/tests/data/luv_array_{0}_2.npy".format(i)
            luv_array_i_obs = cp.array(np.load(fetch(fname)))
            assert_array_almost_equal(luv2xyz(luv_array_i_obs, i, "2"),
                                      self.xyz_array, decimal=3)
Example #6
0
 def strel_worker(self, fn, func):
     matlab_masks = np.load(fetch(fn))
     k = 0
     for arrname in sorted(matlab_masks):
         expected_mask = matlab_masks[arrname]
         actual_mask = func(k)
         if expected_mask.shape == (1, ):
             expected_mask = expected_mask[:, np.newaxis]
         assert_array_equal(expected_mask, actual_mask)
         k = k + 1
Example #7
0
def test_richardson_lucy():
    rstate = np.random.RandomState(0)
    psf = np.ones((5, 5)) / 25
    data = signal.convolve2d(cp.asnumpy(test_img), psf, 'same')
    np.random.seed(0)
    data += 0.1 * data.std() * rstate.standard_normal(data.shape)

    data = cp.asarray(data)
    psf = cp.asarray(psf)
    deconvolved = restoration.richardson_lucy(data, psf, 5)

    path = fetch('restoration/tests/camera_rl.npy')
    cp.testing.assert_allclose(deconvolved, np.load(path), rtol=1e-5)
Example #8
0
 def strel_worker_3d(self, fn, func):
     matlab_masks = np.load(fetch(fn))
     k = 0
     for arrname in sorted(matlab_masks):
         expected_mask = matlab_masks[arrname]
         actual_mask = func(k)
         if expected_mask.shape == (1, ):
             expected_mask = expected_mask[:, np.newaxis]
         # Test center slice for each dimension. This gives a good
         # indication of validity without the need for a 3D reference
         # mask.
         c = int(expected_mask.shape[0] / 2)
         assert_array_equal(expected_mask, actual_mask[c, :, :])
         assert_array_equal(expected_mask, actual_mask[:, c, :])
         assert_array_equal(expected_mask, actual_mask[:, :, c])
         k = k + 1
Example #9
0
def test_richardson_lucy_filtered(dtype_image, dtype_psf):
    if dtype_image == np.float64:
        atol = 1e-8
    else:
        atol = 1e-4

    test_img_astro = rgb2gray(astronaut())

    psf = cp.ones((5, 5), dtype=dtype_psf) / 25
    data = cp.array(signal.convolve2d(cp.asnumpy(test_img_astro),
                                      cp.asnumpy(psf), 'same'),
                    dtype=dtype_image)
    deconvolved = restoration.richardson_lucy(data,
                                              psf,
                                              5,
                                              filter_epsilon=1e-6)
    assert deconvolved.dtype == data.dtype

    path = fetch('restoration/tests/astronaut_rl.npy')
    cp.testing.assert_allclose(deconvolved,
                               np.load(path),
                               rtol=1e-3,
                               atol=atol)
Example #10
0
 def test_gray_morphology(self):
     expected = dict(np.load(fetch('data/gray_morph_output.npz')))
     calculated = self._build_expected_output()
     for k, v in calculated.items():
         cp.testing.assert_array_equal(cp.asarray(expected[k]), v)