Пример #1
0
def allocate_array(size,
                   value_type=float32,
                   step=None,
                   progress=None,
                   reverse_indices=True,
                   zero_fill=False):

    if step is None:
        msize = size
    else:
        msize = [1 + (sz - 1) / st for sz, st in zip(size, step)]

    shape = list(msize)
    if reverse_indices:
        shape.reverse()

    if zero_fill:
        from numpy import zeros as alloc
    else:
        from numpy import empty as alloc

    try:
        m = alloc(shape, value_type)
    except ValueError:
        # numpy 1.0.3 sometimes gives ValueError, sometimes MemoryError
        report_memory_error(msize, value_type)
    except MemoryError:
        report_memory_error(msize, value_type)

    if progress:
        progress.array_size(msize, m.itemsize)

    return m
    def _srgb(self, R):
        """
        sRGB Gamma correction

        :param R: 2D image
        :type R: numpy array, shape (N,M)
        :return: Rg
        :rtype: numpy array

        - ``_srgb(R)`` maps linear tristimulus values to an sRGB gamma encoded 
          image.

        Example:

        .. runblock:: pycon

        .. note::

            - Based on code from Pascal Getreuer 2005-2010
            - And colorspace.m from Peter Corke's Machine Vision Toolbox
        """

        Rg = np.alloc(R.shape, dtype=np.float32)
        a = 0.0031306684425005883
        b = 0.416666666666666667
        i = np.where(R <= a)
        noti = np.where(R > a)
        Rg[i] = R[i] * 12.92
        Rg[noti] = np.real(1.055 * (R[noti]**b) - 0.055)
        return Rg
    def _srgb_inverse(self, Rg):
        """
        Inverse sRGB gamma correction

        :param Rg: 2D image
        :type Rg: numpy array, shape (N,M)
        :return: R
        :rtype: numpy array

        - ``_srgb_imverse(Rg)`` maps an sRGB gamma encoded image to linear
          tristimulus values.

        Example:

        .. runblock:: pycon

        .. note::

            - Based on code from Pascal Getreuer 2005-2010
            - And colorspace.m from Peter Corke's Machine Vision Toolbox
        """

        R = np.alloc(Rg.shape, dtype=np.float32)
        a = 0.0404482362771076
        i = np.where(Rg <= a)
        noti = np.where(Rg > a)
        R[i] = Rg[i] / 12.92
        R[noti] = np.real(((Rg[noti] + 0.055) / 1.055)**2.4)
        return R
Пример #4
0
def allocate_array(size, value_type = float32, step = None, progress = None,
                   reverse_indices = True, zero_fill = False):

    if step is None:
        msize = size
    else:
        msize = [1+(sz-1)/st for sz,st in zip(size, step)]

    shape = list(msize)
    if reverse_indices:
        shape.reverse()

    if zero_fill:
        from numpy import zeros as alloc
    else:
        from numpy import empty as alloc

    try:
        m = alloc(shape, value_type)
    except ValueError:
        # numpy 1.0.3 sometimes gives ValueError, sometimes MemoryError
        report_memory_error(msize, value_type)
    except MemoryError:
        report_memory_error(msize, value_type)

    if progress:
        progress.array_size(msize, m.itemsize)

    return m
def gamma_encode(image, gamma):
    """
    Inverse gamma correction

    :param image: input image
    :type image: ndarray(h,w) or ndarray(h,w,n)
    :param gamma: string identifying srgb, or scalar to raise the image power
    :type gamma: string or float TODO: variable input seems awkward
    :return out: gamma corrected version of image
    :rtype out: ndarray(h,w) or ndarray(h,w,n)

    - ``gamma_encode(image, gamma)`` maps linear tristimulus values to a gamma encoded 
      image.

    Example:

    .. runblock:: pycon

    .. note::

        - Gamma decoding should be applied to any color image prior to
            colometric operations.
        - The exception to this is colorspace conversion using COLORSPACE
            which expects RGB images to be gamma encoded.
        - Gamma encoding is typically performed in a camera with
            GAMMA=0.45.
        - Gamma decoding is typically performed in the display with
            GAMMA=2.2.
        - For images with multiple planes the gamma correction is applied
            to all planes.
        - For images sequences the gamma correction is applied to all
            elements.
        - For images of type double the pixels are assumed to be in the
            range 0 to 1.
        - For images of type int the pixels are assumed in the range 0 to
            the maximum value of their class.  Pixels are converted first to
            double, processed, then converted back to the integer class.

    :references:

        - Robotics, Vision & Control, Chapter 10, P. Corke, Springer 2011.
    """

    if not (base.isscalar(gamma) or isinstance(gamma, str)):
        raise ValueError('gamma must be string or scalar')

    if gamma == 'srgb':

        imagef = ifloat(image)

        if imagef.ndims == 2:
            # greyscale
            return _srgb(imagef.image)
        elif imagef.ndims == 3:
            # multi-dimensional
            out = np.alloc(imagef.shape, dtype=imagef.dtype)
            for p in range(imagef.ndims):
                out[:, :, p] = _srgb(imagef[:, :, p])
        else:
            raise ValueError('expecting 2d or 3d image')

        if np.issubdtype(image.dtype, np.floating):
            # original image was float, convert back
            return iint(out)

    else:
        # normal power law:
        # import code
        # code.interact(local=dict(globals(), **locals()))
        if np.issubdtype(image.dtype, np.float):
            return image**gamma
        else:
            # int image
            maxg = np.float32((np.iinfo(image.dtype).max))
            return ((image.astype(np.float32) / maxg)**gamma) * maxg
def gamma_decode(image, gamma):
    """
    Gamma decoding

    :param image: input image
    :type image: ndarray(h,w) or ndarray(h,w,n)
    :param gamma: string identifying srgb, or scalar to raise the image power
    :type gamma: string or float TODO: variable input seems awkward
    :return out: gamma corrected version of image
    :rtype out: ndarray(h,w) or ndarray(h,w,n)

    - ``gamma_decode(image, gamma)`` is the image with an inverse gamma correction based
        on ``gamma`` applied.

    Example:

    .. runblock:: pycon

    .. note::

        - Gamma decoding should be applied to any color image prior to
            colometric operations.
        - The exception to this is colorspace conversion using COLORSPACE
            which expects RGB images to be gamma encoded.
        - Gamma encoding is typically performed in a camera with
            GAMMA=0.45.
        - Gamma decoding is typically performed in the display with
            GAMMA=2.2.
        - For images with multiple planes the gamma correction is applied
            to all planes.
        - For images sequences the gamma correction is applied to all
            elements.
        - For images of type double the pixels are assumed to be in the
            range 0 to 1.
        - For images of type int the pixels are assumed in the range 0 to
            the maximum value of their class.  Pixels are converted first to
            double, processed, then converted back to the integer class.

    :references:

        - Robotics, Vision & Control, Chapter 10, P. Corke, Springer 2011.
    """

    if not (base.isscalar(gamma) or isinstance(gamma, str)):
        raise ValueError('gamma must be string or scalar')

    if gamma == 'srgb':

        imagef = ifloat(image)

        if imagef.ndims == 2:
            # greyscale
            return _srgb_inv(imagef.image)
        elif imagef.ndims == 3:
            # multi-dimensional
            out = np.alloc(imagef.shape, dtype=imagef.dtype)
            for p in range(imagef.ndims):
                out[:, :, p] = _srgb_inv(imagef[:, :, p])
        else:
            raise ValueError('expecting 2d or 3d image')

        if np.issubdtype(image.dtype, np.float):
            # original image was float, convert back
            return iint(out)

    else:

        # normal power law:
        if np.issubdtype(image.dtype, np.float):
            return image**(1.0 / gamma)
        else:
            # int image
            maxg = np.float32((np.iinfo(image.dtype).max))
            return ((image.astype(np.float32) / maxg)
                    **(1 / gamma)) * maxg  # original
            # return ((image.astype(np.float32) / maxg) ** gamma) * maxg

    def _srgb_inverse(self, Rg):
        """
        Inverse sRGB gamma correction

        :param Rg: 2D image
        :type Rg: numpy array, shape (N,M)
        :return: R
        :rtype: numpy array

        - ``_srgb_imverse(Rg)`` maps an sRGB gamma encoded image to linear
          tristimulus values.

        Example:

        .. runblock:: pycon

        .. note::

            - Based on code from Pascal Getreuer 2005-2010
            - And colorspace.m from Peter Corke's Machine Vision Toolbox
        """

        R = np.alloc(Rg.shape, dtype=np.float32)
        a = 0.0404482362771076
        i = np.where(Rg <= a)
        noti = np.where(Rg > a)
        R[i] = Rg[i] / 12.92
        R[noti] = np.real(((Rg[noti] + 0.055) / 1.055)**2.4)
        return R

    def _srgb(self, R):
        """
        sRGB Gamma correction

        :param R: 2D image
        :type R: numpy array, shape (N,M)
        :return: Rg
        :rtype: numpy array

        - ``_srgb(R)`` maps linear tristimulus values to an sRGB gamma encoded 
          image.

        Example:

        .. runblock:: pycon

        .. note::

            - Based on code from Pascal Getreuer 2005-2010
            - And colorspace.m from Peter Corke's Machine Vision Toolbox
        """

        Rg = np.alloc(R.shape, dtype=np.float32)
        a = 0.0031306684425005883
        b = 0.416666666666666667
        i = np.where(R <= a)
        noti = np.where(R > a)
        Rg[i] = R[i] * 12.92
        Rg[noti] = np.real(1.055 * (R[noti]**b) - 0.055)
        return Rg