Diff of /data/data_utils.py [000000] .. [bd7f9c]

Switch to unified view

a b/data/data_utils.py
1
import random
2
import numpy as np
3
from PIL import Image
4
5
def read_image(path, dtype=np.float32, color=True):
6
    """
7
    Read an image from a given path, the image is in (C, H, W) format and the range of its value is between [0, 255]
8
    :param path: The path of an image file
9
    :param dtype: data type of an image, default is float32
10
    :param color: If 'True', the number of channels is three, in this case, RGB
11
        If 'False', this function returns a gray scale image
12
    :return:
13
    """
14
    f = Image.open(path)
15
    try:
16
        if color:
17
            img = f.convert('RGB')
18
        else:
19
            img = f.convert('P')
20
        img = np.asarray(img, dtype=dtype)
21
    finally:
22
        if hasattr(f, 'close'):
23
            f.close()
24
25
    if img.ndim == 2:
26
        # reshape (H, W) -> (1, H, W)
27
        return img[np.newaxis]
28
    else:
29
        # transpose (H, W, C) -> (C, H, W)
30
        return img.transpose((2, 0, 1))
31
32
33
def flip_masks(masks, y_flip=False, x_flip=False):
34
    masks = masks.copy()
35
    for i in range(masks.shape[0]):
36
        if y_flip:
37
            masks[i] = np.flipud(masks[i]).copy()
38
        if x_flip:
39
            masks[i] = np.fliplr(masks[i]).copy()
40
    return masks
41
42
def random_flip(img, y_random=False, x_random=False,
43
                return_param=False, copy=False):
44
    """
45
    Randomly flip an image in vertical or horizontal direction.
46
    :param img (numpy.ndarray): An array that gets flipped.
47
        This is in CHW format.
48
    :param y_random (bool): Randomly flip in vertical direction.
49
    :param x_random (bool): Randomly flip in horizontal direction.
50
    :param return_param (bool): Returns information of flip.
51
    :param copy (bool): If False, a view of :obj:`img` will be returned.
52
    :return (numpy.ndarray or (numpy.ndarray, dict)):
53
        If :`return_param = False`,
54
        returns an array :obj:`out_img` that is the result of flipping.
55
        If :obj:`return_param = True`,
56
        returns a tuple whose elements are :obj:`out_img, param`.
57
        :obj:`param` is a dictionary of intermediate parameters whose
58
        contents are listed below with key, value-type and the description
59
        of the value.
60
        * **y_flip** (*bool*): Whether the image was flipped in the\
61
            vertical direction or not.
62
        * **x_flip** (*bool*): Whether the image was flipped in the\
63
            horizontal direction or not.
64
    """
65
    y_flip, x_flip = False, False
66
    if y_random:
67
        y_flip = random.choice([True, False])
68
    if x_random:
69
        x_flip = random.choice([True, False])
70
71
    if y_flip:
72
        img = img[:, ::-1, :]
73
    if x_flip:
74
        img = img[:, :, ::-1]
75
76
    if copy:
77
        img = img.copy()
78
79
    if return_param:
80
        return img, {'y_flip': y_flip, 'x_flip': x_flip}
81
    else:
82
        return img
83
84
85
if __name__ == '__main__':
86
    pass