File size: 1,792 Bytes
67a9b5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import khandy
import numpy as np


def flip_image(image, direction='h', copy=True):
    """
    References:
        np.flipud, np.fliplr, np.flip
        cv2.flip
        tf.image.flip_up_down
        tf.image.flip_left_right
    """
    assert khandy.is_numpy_image(image)
    assert direction in ['x', 'h', 'horizontal',
                         'y', 'v', 'vertical', 
                         'o', 'b', 'both']
    if copy:
        image = image.copy()
    if direction in ['o', 'b', 'both', 'x', 'h', 'horizontal']:
        image = np.fliplr(image)
    if direction in ['o', 'b', 'both', 'y', 'v', 'vertical']:
        image = np.flipud(image)
    return image
    
    
def transpose_image(image, copy=True):
    """Transpose image.
    
    References:
        np.transpose
        cv2.transpose
        tf.image.transpose
    """
    assert khandy.is_numpy_image(image)
    if copy:
        image = image.copy()
    if image.ndim == 2:
        transpose_axes = (1, 0)
    else:
        transpose_axes = (1, 0, 2)
    image = np.transpose(image, transpose_axes)
    return image

    
def rot90_image(image, n=1, copy=True):
    """Rotate image counter-clockwise by 90 degrees.
    
    References:
        np.rot90
        cv2.rotate
        tf.image.rot90
    """
    assert khandy.is_numpy_image(image)
    if copy:
        image = image.copy()
    if image.ndim == 2:
        transpose_axes = (1, 0)
    else:
        transpose_axes = (1, 0, 2)
        
    n = n % 4
    if n == 0:
        return image[:]
    elif n == 1:
        image = np.transpose(image, transpose_axes)
        image = np.flipud(image)
    elif n == 2:
        image = np.fliplr(np.flipud(image))
    else:
        image = np.transpose(image, transpose_axes)
        image = np.fliplr(image)
    return image