Ejemplo n.º 1
0
def vector_128_dsift(x, dtype=np.float32):
    r"""
    Computes a SIFT feature vector from a square patch (or image). Patch
    **must** be square and the output vector will *always* be a ``(128,)``
    vector. Please see :func:`dsift` for more information.

    Parameters
    ----------
    x : :map:`Image` or subclass or ``(C, Y, Y)`` `ndarray`
        Either the image object itself or an array with the pixels. The first
        dimension is interpreted as channels. Must be square i.e.
        ``height == width``.
    dtype : ``np.dtype``, optional
        The dtype of the returned vector.

    Raises
    ------
    ValueError
        Only square images are supported.
    """
    if not isinstance(x, np.ndarray):
        x = x.pixels
    if x.shape[-1] != x.shape[-2]:
        raise ValueError('This feature only works with square images '
                         'i.e. width == height')
    patch_shape = x.shape[-1]
    n_bins = 4
    c_size = patch_shape // n_bins
    x = normalise_pixels_range(x, error_on_unknown_type=False)
    return dsift(x,
                 window_step_horizontal=patch_shape,
                 window_step_vertical=patch_shape,
                 num_bins_horizontal=n_bins, num_bins_vertical=n_bins,
                 cell_size_horizontal=c_size, cell_size_vertical=c_size,
                 num_or_bins=8, fast=True).astype(dtype)
Ejemplo n.º 2
0
 def _pil_to_numpy(self, normalise, convert=None):
     p = self._pil_image.convert(convert) if convert else self._pil_image
     p = channels_to_front(p)
     if normalise:
         return normalise_pixels_range(p)
     else:
         return p
Ejemplo n.º 3
0
        def imageio_to_menpo(imio_reader, index):
            pixels = imio_reader.get_data(index)
            pixels = channels_to_front(pixels)

            if self.normalise:
                return Image(normalise_pixels_range(pixels), copy=False)
            else:
                return Image(pixels, copy=False)
Ejemplo n.º 4
0
        def imageio_to_menpo(imio_reader, index):
            pixels = imio_reader.get_data(index)
            pixels = channels_to_front(pixels)

            if self.normalise:
                return Image(normalise_pixels_range(pixels), copy=False)
            else:
                return Image(pixels, copy=False)
Ejemplo n.º 5
0
        def imageio_to_menpo(imio_reader, index):
            pixels = imio_reader.get_data(index)
            pixels = channels_to_front(pixels)

            if pixels.shape[0] == 4:
                # If normalise is False, then we return the alpha as an extra
                # channel, which can be useful if the alpha channel has semantic
                # meanings!
                if self.normalise:
                    p = normalise_pixels_range(pixels[:3])
                    return MaskedImage(p, mask=pixels[-1].astype(np.bool),
                                       copy=False)
                else:
                    return Image(pixels, copy=False)

            # Assumed not to have an Alpha channel
            if self.normalise:
                return Image(normalise_pixels_range(pixels), copy=False)
            else:
                return Image(pixels, copy=False)
Ejemplo n.º 6
0
    def build(self):
        import imageio

        pixels = imageio.imread(self.filepath)
        pixels = channels_to_front(pixels)

        transparent_types = {'.png'}
        filepath = Path(self.filepath)
        if pixels.shape[0] == 4 and filepath.suffix in transparent_types:
            # If normalise is False, then we return the alpha as an extra
            # channel, which can be useful if the alpha channel has semantic
            # meanings!
            if self.normalise:
                p = normalise_pixels_range(pixels[:3])
                return MaskedImage(p, mask=pixels[-1].astype(np.bool),
                                   copy=False)
            else:
                return Image(pixels, copy=False)

        # Assumed not to have an Alpha channel
        if self.normalise:
            return Image(normalise_pixels_range(pixels), copy=False)
        else:
            return Image(pixels, copy=False)
Ejemplo n.º 7
0
def vector_128_dsift(x, dtype=np.float32):
    r"""
    Computes a SIFT feature vector from a square patch (or image). Patch
    **must** be square and the output vector will *always* be a ``(128,)``
    vector. Please see :func:`dsift` for more information.

    Parameters
    ----------
    x : :map:`Image` or subclass or ``(C, Y, Y)`` `ndarray`
        Either the image object itself or an array with the pixels. The first
        dimension is interpreted as channels. Must be square i.e.
        ``height == width``.
    dtype : ``np.dtype``, optional
        The dtype of the returned vector.

    Raises
    ------
    ValueError
        Only square images are supported.
    """
    if not isinstance(x, np.ndarray):
        x = x.pixels
    if x.shape[-1] != x.shape[-2]:
        raise ValueError('This feature only works with square images '
                         'i.e. width == height')
    patch_shape = x.shape[-1]
    n_bins = 4
    c_size = patch_shape // n_bins
    x = normalise_pixels_range(x, error_on_unknown_type=False)
    return dsift(x,
                 window_step_horizontal=patch_shape,
                 window_step_vertical=patch_shape,
                 num_bins_horizontal=n_bins,
                 num_bins_vertical=n_bins,
                 cell_size_horizontal=c_size,
                 cell_size_vertical=c_size,
                 num_or_bins=8,
                 fast=True).astype(dtype)