Esempio n. 1
0
    def image_gradient_T(x):
        """
        Transpose of the operator that function image_gradient implements.

        Computes the transpose of the matrix given by function image_gradient.

        Parameters
        ----------
        x : numpy.ndarray or ctorch.ComplexTensor
            stack of two identically shaped arrays

        Returns
        -------
        numpy.ndarray or ctorch.ComplexTensor
            result of applying to x the transpose of function image_gradient
        """
        if isinstance(x, np.ndarray):
            x_h = x[0]
            x_v = x[1]
            # Wrap the first column of x_h around to the end.
            x_h_ext = np.hstack((x_h, x_h[:, :1]))
            # Wrap the first row of x_v around to the end.
            x_v_ext = np.vstack((x_v, x_v[:1]))
            # Apply forward differences to the columns of x.
            d_x = x_h_ext[:, :-1] - x_h_ext[:, 1:]
            # Apply forward differences to the rows of x.
            d_y = x_v_ext[:-1] - x_v_ext[1:]
            return d_x + d_y
        elif isinstance(x, ctorch.ComplexTensor):
            x_h = x[0]
            x_v = x[1]
            # Wrap the first column of x_h around to the end.
            x_h_ext = ctorch.cat((x_h, x_h[:, :1]), dim=1)
            # Wrap the first row of x_v around to the end.
            x_v_ext = ctorch.cat((x_v, x_v[:1]), dim=0)
            # Apply forward differences to the columns of x.
            d_x = x_h_ext[:, :-1] - x_h_ext[:, 1:]
            # Apply forward differences to the rows of x.
            d_y = x_v_ext[:-1] - x_v_ext[1:]
            return d_x + d_y
        else:
            raise TypeError('Input must be a numpy.ndarray ' +
                            'or a ctorch.ComplexTensor.')
Esempio n. 2
0
    def image_gradient(x):
        """
        First-order finite-differencing both horizontally and vertically.

        Computes a first-order finite-difference approximation to the gradient.

        Parameters
        ----------
        x : numpy.ndarray or ctorch.ComplexTensor
            image (that is, two-dimensional array)

        Returns
        -------
        numpy.ndarray or ctorch.ComplexTensor
            horizontal finite differences of x stacked on top of the vertical
            finite differences (separating horizontal from vertical via the
            initial dimension)
        """
        if isinstance(x, np.ndarray):
            # Wrap the last column of x around to the beginning.
            x_h = np.hstack((x[:, -1:], x))
            # Wrap the last row of x around to the beginning.
            x_v = np.vstack((x[-1:], x))
            # Apply forward differences to the columns of x.
            d_x = (x_h[:, 1:] - x_h[:, :-1])
            # Apply forward differences to the rows of x.
            d_y = (x_v[1:] - x_v[:-1])
            return np.vstack((d_x.ravel(), d_y.ravel()))
        elif isinstance(x, ctorch.ComplexTensor):
            # Wrap the last column of x around to the beginning.
            x_h = ctorch.cat((x[:, -1:], x), dim=1)
            # Wrap the last row of x around to the beginning.
            x_v = ctorch.cat((x[-1:], x), dim=0)
            # Apply forward differences to the columns of x.
            d_x = (x_h[:, 1:] - x_h[:, :-1])
            # Apply forward differences to the rows of x.
            d_y = (x_v[1:] - x_v[:-1])
            return ctorch.cat((d_x, d_y)).view(2, -1)
        else:
            raise TypeError('Input must be a numpy.ndarray ' +
                            'or a ctorch.ComplexTensor.')