Beispiel #1
0
    def __init__(self, background, target):
        '''Creates the filter, given background/target means and covariance.

        Arguments:

            `background` (`GaussianStats`):

                The Gaussian statistics for the background (e.g., the result
                of calling :func:`calc_stats`).

            `target` (ndarray):

                Length-K target mean
        '''
        from math import sqrt
        from spectral.algorithms.transforms import LinearTransform

        self.background = background
        self.u_b = background.mean
        self.u_t = target
        self._whitening_transform = None

        d_tb = (target - self.u_b)
        self.d_tb = d_tb
        C_1 = background.inv_cov
        self.C_1 = C_1

        # Normalization coefficient (inverse of  squared Mahalanobis distance
        # between u_t and u_b)
        self.coef = 1.0 / d_tb.dot(C_1).dot(d_tb)

        LinearTransform.__init__(
            self, (self.coef * d_tb).dot(C_1), pre=-self.u_b)
Beispiel #2
0
    def __init__(self, background, target):
        '''Creates the filter, given background/target means and covariance.

        Arguments:

            `background` (`GaussianStats`):

                The Gaussian statistics for the background (e.g., the result
                of calling :func:`calc_stats`).

            `target` (ndarray):

                Length-K target mean
        '''
        from math import sqrt
        from spectral.algorithms.transforms import LinearTransform

        self.background = background
        self.u_b = background.mean
        self.u_t = target
        self._whitening_transform = None

        d_tb = (target - self.u_b)
        self.d_tb = d_tb
        C_1 = background.inv_cov
        self.C_1 = C_1

        # Normalization coefficient (inverse of  squared Mahalanobis distance
        # between u_t and u_b)
        self.coef = 1.0 / d_tb.dot(C_1).dot(d_tb)

        LinearTransform.__init__(self, (self.coef * d_tb).dot(C_1),
                                 pre=-self.u_b)
    def __init__(self, transform, img):
        from spectral.algorithms.transforms import LinearTransform

        if not isinstance(img, Image):
            raise Exception(
                'Invalid image argument to to TransformedImage constructor.')

        if isinstance(transform, numpy.ndarray):
            transform = LinearTransform(transform)
        self.transform = transform

        if self.transform.dim_in not in (None, img.shape[-1]):
            raise Exception('Number of bands in image (%d) do not match the '
                            ' input dimension of the transform (%d).' %
                            (img.shape[-1], transform.dim_in))

        params = img.params()
        self.set_params(params, params.metadata)

        # If img is also a TransformedImage, then just modify the transform
        if isinstance(img, TransformedImage):
            self.transform = self.transform.chain(img.transform)
            self.image = img.image
        else:
            self.image = img
        if self.transform.dim_out is not None:
            self.shape = self.image.shape[:2] + (self.transform.dim_out, )
            self.nbands = self.transform.dim_out
        else:
            self.shape = self.image.shape
            self.nbands = self.image.nbands
Beispiel #4
0
 def test_pre_matrix_multiply_post(self):
     from spectral.algorithms.transforms import LinearTransform
     (i, j, k) = self.datum
     transform = LinearTransform(self.matrix, pre=self.pre, post=self.post)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result,
                         self.scalar * (self.pre + self.value) + self.post)
Beispiel #5
0
    def whiten(self, X):
        '''Transforms data to the whitened space of the background.

        Arguments:

            `X` (ndarray):

                Size (M,N,K) or (M*N,K) array of length K vectors to transform.

        Returns an array of same size as `X` but linearly transformed to the
        whitened space of the filter.
        '''
        import math
        from spectral.algorithms.transforms import LinearTransform
        from spectral.algorithms.spymath import matrix_sqrt

        if self._whitening_transform is None:
            A = math.sqrt(self.coef) * self.background.sqrt_inv_cov
            self._whitening_transform = LinearTransform(A, pre=-self.u_b)
        return self._whitening_transform(X)
Beispiel #6
0
 def test_pre_matrix_multiply(self):
     (i, j, k) = self.datum
     transform = LinearTransform(self.matrix, pre=self.pre)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result, self.scalar * (self.pre + self.value))
Beispiel #7
0
 def test_pre_scalar_multiply_post(self):
     (i, j, k) = self.datum
     transform = LinearTransform(self.scalar, pre=self.pre, post=self.post)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result,
                         self.scalar * (self.pre + self.value) + self.post)
Beispiel #8
0
 def test_scalar_multiply(self):
     (i, j, k) = self.datum
     transform = LinearTransform(self.scalar)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result, self.scalar * self.value)
Beispiel #9
0
 def test_matrix_multiply_post(self):
     (i, j, k) = self.datum
     transform = LinearTransform(self.matrix, post=self.post)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result, self.scalar * self.value + self.post)
Beispiel #10
0
 def test_scalar_multiply(self):
     from spectral.algorithms.transforms import LinearTransform
     (i, j, k) = self.datum
     transform = LinearTransform(self.scalar)
     result = transform(self.image[i, j])[k]
     assert_almost_equal(result, self.scalar * self.value)