pyosv.utils.mapper

 1import numpy as np
 2
 3def mapFromTo(x : np.ndarray , a : int, b : int, c : int, d : int) -> np.ndarray:
 4    '''
 5        Map x from range a,b to range c,d
 6        
 7        Parameters:
 8        -----------
 9            - x : np.ndarray
10                input to be mapped
11            - a : int 
12                min range from, if None it will inputed using x
13            - b : int
14                max range from, if None it will inputed using x
15            - c : int
16                min range to
17            - d : int
18                max range to
19        
20        Returns:
21        --------
22            - y : np.ndarray
23                mapped values
24        
25        Usage:
26        ------
27
28        ```python
29        import numpy as np
30
31        img         = np.array(  
32            [[
33                [0.1, 0.2, 0.3],  
34                [0.4, 0.5, 0.6],  
35                [0.7, 0.8, 0.9]
36             ],
37             [
38                [1.1, 1.2, 1.3],  
39                [1.4, 1.5, 1.6],  
40                [1.7, 1.8, 1.9]
41             ]
42            ]  
43        )
44
45        img_m = mapFromTo(img, None, None, 0, 255)
46        ```
47
48        Output:
49        -------
50
51        ```
52        [[[  0.          14.16666667  28.33333333]  
53        [ 42.5         56.66666667  70.83333333]  
54        [ 85.          99.16666667 113.33333333]]  
55        [[141.66666667 155.83333333 170.        ]  
56        [184.16666667 198.33333333 212.5       ]  
57        [226.66666667 240.83333333 255.        ]]]  
58        ```
59    '''
60
61    if a is None: a = np.min(x)
62    if b is None: b = np.max(x)
63
64    # Avoid zero division
65    if b-a == 0: b = 0.0001 
66    if d-c == 0: d = 0.0001
67    
68    y = (x-a)/(b-a)*(d-c)+c
69    
70    return y
def mapFromTo(x: numpy.ndarray, a: int, b: int, c: int, d: int) -> numpy.ndarray:
 4def mapFromTo(x : np.ndarray , a : int, b : int, c : int, d : int) -> np.ndarray:
 5    '''
 6        Map x from range a,b to range c,d
 7        
 8        Parameters:
 9        -----------
10            - x : np.ndarray
11                input to be mapped
12            - a : int 
13                min range from, if None it will inputed using x
14            - b : int
15                max range from, if None it will inputed using x
16            - c : int
17                min range to
18            - d : int
19                max range to
20        
21        Returns:
22        --------
23            - y : np.ndarray
24                mapped values
25        
26        Usage:
27        ------
28
29        ```python
30        import numpy as np
31
32        img         = np.array(  
33            [[
34                [0.1, 0.2, 0.3],  
35                [0.4, 0.5, 0.6],  
36                [0.7, 0.8, 0.9]
37             ],
38             [
39                [1.1, 1.2, 1.3],  
40                [1.4, 1.5, 1.6],  
41                [1.7, 1.8, 1.9]
42             ]
43            ]  
44        )
45
46        img_m = mapFromTo(img, None, None, 0, 255)
47        ```
48
49        Output:
50        -------
51
52        ```
53        [[[  0.          14.16666667  28.33333333]  
54        [ 42.5         56.66666667  70.83333333]  
55        [ 85.          99.16666667 113.33333333]]  
56        [[141.66666667 155.83333333 170.        ]  
57        [184.16666667 198.33333333 212.5       ]  
58        [226.66666667 240.83333333 255.        ]]]  
59        ```
60    '''
61
62    if a is None: a = np.min(x)
63    if b is None: b = np.max(x)
64
65    # Avoid zero division
66    if b-a == 0: b = 0.0001 
67    if d-c == 0: d = 0.0001
68    
69    y = (x-a)/(b-a)*(d-c)+c
70    
71    return y

Map x from range a,b to range c,d

Parameters:

- x : np.ndarray
    input to be mapped
- a : int 
    min range from, if None it will inputed using x
- b : int
    max range from, if None it will inputed using x
- c : int
    min range to
- d : int
    max range to

Returns:

- y : np.ndarray
    mapped values

Usage:

import numpy as np

img         = np.array(  
    [[
        [0.1, 0.2, 0.3],  
        [0.4, 0.5, 0.6],  
        [0.7, 0.8, 0.9]
     ],
     [
        [1.1, 1.2, 1.3],  
        [1.4, 1.5, 1.6],  
        [1.7, 1.8, 1.9]
     ]
    ]  
)

img_m = mapFromTo(img, None, None, 0, 255)

Output:

[[[  0.          14.16666667  28.33333333]  
[ 42.5         56.66666667  70.83333333]  
[ 85.          99.16666667 113.33333333]]  
[[141.66666667 155.83333333 170.        ]  
[184.16666667 198.33333333 212.5       ]  
[226.66666667 240.83333333 255.        ]]]