File size: 2,623 Bytes
205273a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
import numpy as np
import cv2
from config import RESIZE_TO, MASKIMAGE

# image mask

# free form mask
# bbox mask


def create_ff_mask():
    config = {
        "img_shape": list(RESIZE_TO),
        "mv": 15,
        "ma": 4.0,
        "ml": 40,
        "mbw": 5,
    }

    h, w = config["img_shape"]
    mask = np.zeros((h, w))
    num_v = np.random.randint(config["mv"])

    for i in range(num_v):
        start_x = np.random.randint(w)
        start_y = np.random.randint(h)
        for j in range(1 + np.random.randint(5)):
            angle = 0.01 + np.random.randint(config["ma"])
            if i % 2 == 0:
                angle = 2 * 3.1415926 - angle
            length = 10 + np.random.randint(config["ml"])
            brush_w = 5 + np.random.randint(config["mbw"])
            end_x = (start_x + length * np.sin(angle)).astype(np.int32)
            end_y = (start_y + length * np.cos(angle)).astype(np.int32)

            cv2.line(mask, (start_y, start_x), (end_y, end_x), 255.0, brush_w)
            start_x, start_y = end_x, end_y

    mask = mask.astype(np.uint8)
    cv2.imwrite(MASKIMAGE, mask)


def create_bbox_mask():
    shape = list(RESIZE_TO)
    margin = [10, 10]
    bbox_shape = [30, 30]

    def random_bbox(shape, margin, bbox_shape):
        """Generate a random tlhw with configuration.

        Args:

            config: Config should have configuration including IMG_SHAPES,

                    VERTICAL_MARGIN, HEIGHT, HORIZONTAL_MARGIN, WIDTH.

        Returns:

            tuple: (top, left, height, width)

        """
        img_height, img_width = shape
        height, width = bbox_shape
        ver_margin, hor_margin = margin
        maxt = img_height - ver_margin - height
        maxl = img_width - hor_margin - width
        t = np.random.randint(low=ver_margin, high=maxt)
        l = np.random.randint(low=hor_margin, high=maxl)
        h = height
        w = width
        return (t, l, h, w)

    bboxs = []
    for i in range(20):
        bbox = random_bbox(shape, margin, bbox_shape)
        bboxs.append(bbox)

    height, width = shape
    mask = np.zeros((height, width), np.float32)
    # print(mask.shape)
    for bbox in bboxs:
        h = int(bbox[2] * 0.1) + np.random.randint(int(bbox[2] * 0.2 + 1))
        w = int(bbox[3] * 0.1) + np.random.randint(int(bbox[3] * 0.2) + 1)
        mask[
            (bbox[0] + h) : (bbox[0] + bbox[2] - h),
            (bbox[1] + w) : (bbox[1] + bbox[3] - w),
        ] = 255.0

    mask = mask.astype(np.uint8)
    cv2.imwrite(MASKIMAGE, mask)