Beispiel #1
0
def sample_fs(a: np.array, grid_sz: np.array = None, rescale=True):
    """Samples the Fourier series."""

    # Size of the fourier series
    sz = np.array([a.shape[2], 2 * a.shape[3] - 1], 'float32')

    # Default grid
    if grid_sz is None or sz[0] == grid_sz[0] and sz[1] == grid_sz[1]:
        if rescale:
            return np.prod(sz) * cifft2(a)
        return cifft2(a)

    if sz[0] > grid_sz[0] or sz[1] > grid_sz[1]:
        raise ValueError(
            "Only grid sizes that are smaller than the Fourier series size are supported."
        )

    tot_pad = (grid_sz - sz).tolist()
    is_even = [s % 2 == 0 for s in sz]

    # Compute paddings
    pad_top = int((tot_pad[0] + 1) / 2) if is_even[0] else int(tot_pad[0] / 2)
    pad_bottom = int(tot_pad[0] - pad_top)
    pad_right = int((tot_pad[1] + 1) / 2)

    if rescale:
        return np.prod(grid_sz) * cifft2(
            _padding(a, (0, 0, 0, pad_right, pad_top, pad_bottom)),
            signal_sizes=grid_sz.astype('long').tolist())
    else:
        return cifft2(_padding(a, (0, 0, 0, pad_right, pad_top, pad_bottom)),
                      signal_sizes=grid_sz.astype('long').tolist())
Beispiel #2
0
    def crop_to_output(self, image, shift=None):
        if isinstance(image, PTensor):
            imsz = image.shape[2:]
        else:
            imsz = image.shape[:2]

        if self.output_sz is None:
            pad_h = 0
            pad_w = 0
        else:
            pad_h = (self.output_sz[0] - imsz[0]) / 2
            pad_w = (self.output_sz[1] - imsz[1]) / 2
        if shift is None:
            shift = self.shift
        pad_left = math.floor(pad_w) + shift[1]
        pad_right = math.ceil(pad_w) - shift[1]
        pad_top = math.floor(pad_h) + shift[0]
        pad_bottom = math.ceil(pad_h) - shift[0]

        if isinstance(image, PTensor):
            return _padding(image, (pad_left, pad_right, pad_top, pad_bottom),
                            mode='replicate')
        else:
            return _padding(image,
                            (0, 0, pad_left, pad_right, pad_top, pad_bottom),
                            mode='replicate')
Beispiel #3
0
def sample_patch(im: np.ndarray,
                 pos: np.ndarray,
                 sample_sz: np.ndarray,
                 output_sz: np.ndarray=None):
    """Sample an image patch.

    args:
        im: Image
        pos: center position of crop
        sample_sz: size to crop
        output_sz: size to resize to
    """

    # copy and convert
    posl = pos.astype('long')

    # Compute pre-downsampling factor
    if output_sz is not None:
        resize_factor = np.min(
            sample_sz.astype('float32') / output_sz.astype('float32'))
        df = int(max(int(resize_factor - 0.1), 1))
    else:
        df = int(1)

    sz = sample_sz.astype('float32') / df  # new size

    # Do downsampling
    if df > 1:
        os = posl % df  # offset
        posl = ((posl - os) / df).astype('long')  # new position
        im2 = im[os[0]::df, os[1]::df]  # downsample
    else:
        im2 = im

    # compute size to crop
    szl = np.maximum(
        np.round(sz), np.array(
            [2., 2.], dtype='float32')).astype('long')

    # Extract top and bottom coordinates
    tl = posl - (szl - 1) // 2
    br = posl + szl // 2

    # Get image patch
    im_patch = _padding(
        im2, (0, 0, -tl[1], br[1] - im2.shape[1] + 1, -tl[0],
              br[0] - im2.shape[0] + 1),
        mode='replicate')

    if output_sz is None or (im_patch.shape[0] == output_sz[0] and
                             im_patch.shape[1] == output_sz[1]):
        return im_patch

    # Resample
    osz = output_sz.astype('long')
    im_patch = cv.resize(
        im_patch, (osz[1], osz[0]), interpolation=cv.INTER_LINEAR)
    return im_patch
Beispiel #4
0
def sample_patch_with_mean_pad(im: np.ndarray,
                               pos: np.ndarray,
                               sample_sz: np.ndarray,
                               output_sz: np.ndarray=None):
    """Sample an image patch.

    args:
        im: Image
        pos: center position of crop
        sample_sz: size to crop
        output_sz: size to resize to
    """

    # copy and convert
    # posl = np.round(pos).astype('long')  # TODO: maybe we should use round
    posl = pos.astype('long')

    im2 = im
    sz = sample_sz.astype('float32')
    # compute size to crop
    szl = np.maximum(
        np.round(sz), np.array(
            [2., 2.], dtype='float32')).astype('long')

    # Extract top and bottom coordinates
    tl = posl - (szl - 1) // 2
    br = posl + szl // 2

    # Get image patch
    im_patch = _padding(
        im2, (0, 0, -tl[1], br[1] - im2.shape[1] + 1, -tl[0],
              br[0] - im2.shape[0] + 1),
        mode='replicate')

    if output_sz is None or (im_patch.shape[0] == output_sz[0] and
                             im_patch.shape[1] == output_sz[1]):
        return im_patch

    # Resample
    osz = output_sz.astype('long')
    im_patch = cv.resize(
        im_patch, (osz[1], osz[0]), interpolation=cv.INTER_LINEAR)
    return im_patch
Beispiel #5
0
def hann2d_clipped(sz: np.ndarray,
                   effective_sz: np.ndarray,
                   centered=True) -> np.ndarray:
    """1D clipped cosine window."""

    # Ensure that the difference is even
    effective_sz += (effective_sz - sz) % 2
    effective_window = np.reshape(hann1d(effective_sz[0], True), (1, 1, -1, 1)) * \
                       np.reshape(hann1d(effective_sz[1], True), (1, 1, 1, -1))

    pad = np.int32((sz - effective_sz) / 2)
    window = _padding(effective_window, (pad[1], pad[1], pad[0], pad[0]),
                      mode='replicate')

    if centered:
        return window
    else:
        mid = np.int32((sz / 2))
        window_shift_lr = np.concatenate(
            (window[..., mid[1]:], window[..., :mid[1]]), 3)
        return np.concatenate(
            (window_shift_lr[...,
                             mid[0]:, :], window_shift_lr[..., :mid[0], :]), 2)