dippykit.sampling module

Module of various image sampling functions

This module contains an assortment of functions that sample images in various useful manners.

dippykit.sampling.resample(image: numpy.ndarray, rs_matrix: numpy.ndarray, **kwargs) → numpy.ndarray[source]

Resamples an image based on a given resampling matrix.

Resamples an image by the given resampling matrix argument, rs_matrix. For an input domain vector mn = [n, m] and an input image f (mn), the output image g (.) is defined by g (mn) = f (M @ mn) where M is the resampling matrix (rs_matrix) and the @ symbol denotes matrix multiplication.

Parameters:
  • image (numpy.ndarray) – An image to be resampled.
  • rs_matrix (numpy.ndarray) – The resampling matrix M to be used.
Return type:

numpy.ndarray

Returns:

The resampled image.

Keyword Arguments:
 
  • interpolation (str) – (default=None) The interpolation method used during the resampling. The lack of an interpolation method will lead to a resampling where only pixels that perfectly map across the input and output domains are included. This typically only works well for resampling matrices that contain only integer entries (which will downsample), or the inverses of such matrices (which will upsample).

    The interpolation keyword argument can take on any of the following values (For more information, see cv2.warpAffine):

    • nearest’ (See cv2.INTER_NEAREST)
    • linear’ or ‘bilinear’ (See cv2.INTER_LINEAR)
    • area’ (See cv2.INTER_AREA)
    • cubic’ or ‘bicubic’ (See cv2.INTER_CUBIC)
    • lanczos4’ (See cv2.INTER_LANCZOS4)
  • crop (bool) – (default=False) Whether or not to crop the output image. If this keyword argument is specified to be True without specifying a crop_size keyword argument, then the crop_size will be set to the original size of the input image by default.

  • crop_size (ShapeType) – (default=image.shape) The dimensions of the output image after cropping. An integer argument signifies dimension in pixels, a floating point argument signifies dimension in proportion to the output image size (before cropping). A single value will yield a square output image.

Examples:

>>> import numpy as np
>>> image = np.array([[ 0,  1,  2,  3],
...                   [10, 11, 12, 13],
...                   [20, 21, 22, 23],
...                   [30, 31, 32, 33]])
>>> M = np.array([[1/2, 0], [0, 1/2]])
>>> resample(image, M)
array([[ 0,  0,  1,  0,  2,  0,  3],
       [ 0,  0,  0,  0,  0,  0,  0],
       [10,  0, 11,  0, 12,  0, 13],
       [ 0,  0,  0,  0,  0,  0,  0],
       [20,  0, 21,  0, 22,  0, 23],
       [ 0,  0,  0,  0,  0,  0,  0],
       [30,  0, 31,  0, 32,  0, 33]])
>>> resample(image, M, interp='lin')
array([[ 0,  0,  1,  1,  2,  2,  3],
       [ 5,  5,  6,  6,  7,  7,  8],
       [10, 10, 11, 11, 12, 12, 13],
       [15, 15, 16, 16, 17, 17, 18],
       [20, 20, 21, 21, 22, 22, 23],
       [25, 25, 26, 26, 27, 27, 28],
       [30, 30, 31, 31, 32, 32, 33]])
>>> image_float = image.astype(float)
>>> resample(image_float, M, interp='lin')
array([[ 0. ,  0.5,  1. ,  1.5,  2. ,  2.5,  3. ],
       [ 5. ,  5.5,  6. ,  6.5,  7. ,  7.5,  8. ],
       [10. , 10.5, 11. , 11.5, 12. , 12.5, 13. ],
       [15. , 15.5, 16. , 16.5, 17. , 17.5, 18. ],
       [20. , 20.5, 21. , 21.5, 22. , 22.5, 23. ],
       [25. , 25.5, 26. , 26.5, 27. , 27.5, 28. ],
       [30. , 30.5, 31. , 31.5, 32. , 32.5, 33. ]])
>>> resample(image_float, M, interp='lin', crop=True, crop_size=(4,3))
array([[ 6. ,  6.5,  7. ],
       [11. , 11.5, 12. ],
       [16. , 16.5, 17. ],
       [21. , 21.5, 22. ]])
>>> M2 = np.array([[2, 0], [0, 2]])
>>> resample(image, M2)
array([[ 0,  2],
       [20, 22]])
>>> M3 = np.array([[-1, 0], [0, 1]])
>>> resample(image, M3)
array([[ 3,  2,  1,  0],
       [13, 12, 11, 10],
       [23, 22, 21, 20],
       [33, 32, 31, 30]])
>>> theta = np.pi / 2
>>> M4 = np.array([[ np.cos(theta), -np.sin(theta)],
...                [ np.sin(theta),  np.cos(theta)]])
>>> resample(image, M4)
array([[ 3, 13, 23, 33],
       [ 2, 12, 22, 32],
       [ 1, 11, 21, 31],
       [ 0, 10, 20, 30]])
>>> theta = np.pi / 4
>>> M5 = np.array([[ np.cos(theta), -np.sin(theta)],
...                [ np.sin(theta),  np.cos(theta)]])
>>> resample(image, M5, interp='nearest')
array([[ 0,  0,  3,  0,  0],
       [ 0,  1, 12, 13,  0],
       [ 0, 11, 11, 22, 33],
       [ 0, 10, 21, 31,  0],
       [ 0,  0, 30,  0,  0]])