a b/utils/image_utils.py
1
import numpy as np
2
3
4
def crop_center(img, cropx, cropy):
5
    y = img.shape[0]
6
    x = img.shape[1]
7
    startx = x // 2 - (cropx // 2)
8
    starty = y // 2 - (cropy // 2)
9
    if len(img.shape) > 2:
10
        return img[starty:starty + cropy, startx:startx + cropx, :]
11
    else:
12
        return img[starty:starty + cropy, startx:startx + cropx]
13
14
15
def crop(img, y, x, height, width):
16
    return img[y:y + height, x:x + width]
17
18
19
# TP are green
20
# FP are orange
21
# FN are red
22
def augment_prediction_and_groundtruth_to_image(image, p, g):
23
    if image.ndim < 3:
24
        image = np.expand_dims(image, 2)
25
    tmp = np.repeat(image, 3, 2)
26
    p = np.squeeze(p.astype(bool))
27
    g = np.squeeze(g.astype(bool))
28
29
    tp_map = np.zeros(tmp.shape)
30
    tp_channel = np.multiply(p, g)
31
    tp_map[:, :, 1] = tp_channel
32
    fp_map = np.zeros(tmp.shape)
33
    fp_channel = np.multiply(p, np.invert(g))
34
    fp_map[:, :, 0] = fp_channel
35
    fp_map[:, :, 1] = 0.5 * fp_channel
36
    fn_map = np.zeros(tmp.shape)
37
    fn_channel = np.multiply(np.invert(p), g)
38
    fn_map[:, :, 0] = fn_channel
39
    map = tp_map + fp_map + fn_map
40
    mask = np.repeat(np.expand_dims(np.logical_or(np.logical_or(tp_channel, fp_channel), fn_channel), axis=2), 3, 2)
41
42
    tmp[tmp < 0] = 0
43
    tmp[mask] = map[mask]
44
45
    return tmp