scikit-image

Форк
0
74 строки · 2.5 Кб
1
import numpy as np
2
from .dtype import dtype_limits
3

4

5
def invert(image, signed_float=False):
6
    """Invert an image.
7

8
    Invert the intensity range of the input image, so that the dtype maximum
9
    is now the dtype minimum, and vice-versa. This operation is
10
    slightly different depending on the input dtype:
11

12
    - unsigned integers: subtract the image from the dtype maximum
13
    - signed integers: subtract the image from -1 (see Notes)
14
    - floats: subtract the image from 1 (if signed_float is False, so we
15
      assume the image is unsigned), or from 0 (if signed_float is True).
16

17
    See the examples for clarification.
18

19
    Parameters
20
    ----------
21
    image : ndarray
22
        Input image.
23
    signed_float : bool, optional
24
        If True and the image is of type float, the range is assumed to
25
        be [-1, 1]. If False and the image is of type float, the range is
26
        assumed to be [0, 1].
27

28
    Returns
29
    -------
30
    inverted : ndarray
31
        Inverted image.
32

33
    Notes
34
    -----
35
    Ideally, for signed integers we would simply multiply by -1. However,
36
    signed integer ranges are asymmetric. For example, for np.int8, the range
37
    of possible values is [-128, 127], so that -128 * -1 equals -128! By
38
    subtracting from -1, we correctly map the maximum dtype value to the
39
    minimum.
40

41
    Examples
42
    --------
43
    >>> img = np.array([[100,  0, 200],
44
    ...                 [  0, 50,   0],
45
    ...                 [ 30,  0, 255]], np.uint8)
46
    >>> invert(img)
47
    array([[155, 255,  55],
48
           [255, 205, 255],
49
           [225, 255,   0]], dtype=uint8)
50
    >>> img2 = np.array([[ -2, 0, -128],
51
    ...                  [127, 0,    5]], np.int8)
52
    >>> invert(img2)
53
    array([[   1,   -1,  127],
54
           [-128,   -1,   -6]], dtype=int8)
55
    >>> img3 = np.array([[ 0., 1., 0.5, 0.75]])
56
    >>> invert(img3)
57
    array([[1.  , 0.  , 0.5 , 0.25]])
58
    >>> img4 = np.array([[ 0., 1., -1., -0.25]])
59
    >>> invert(img4, signed_float=True)
60
    array([[-0.  , -1.  ,  1.  ,  0.25]])
61
    """
62
    if image.dtype == 'bool':
63
        inverted = ~image
64
    elif np.issubdtype(image.dtype, np.unsignedinteger):
65
        max_val = dtype_limits(image, clip_negative=False)[1]
66
        inverted = np.subtract(max_val, image, dtype=image.dtype)
67
    elif np.issubdtype(image.dtype, np.signedinteger):
68
        inverted = np.subtract(-1, image, dtype=image.dtype)
69
    else:  # float dtype
70
        if signed_float:
71
            inverted = -image
72
        else:
73
            inverted = np.subtract(1, image, dtype=image.dtype)
74
    return inverted
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.