[e698c9]: / Segmentation / utils.py

Download this file

79 lines (74 with data), 2.5 kB

 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
import random
import cv2
import numpy as np
def gaussian_noise(img, mean=0, sigma=0.03):
img = img.copy()
noise = np.random.normal(mean, sigma, img.shape)
mask_overflow_upper = img+noise >= 1.0
mask_overflow_lower = img+noise < 0
noise[mask_overflow_upper] = 1.0
noise[mask_overflow_lower] = 0
img += noise
return img
def brightness(img, low, high):
value = random.uniform(low, high)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
hsv = np.array(hsv, dtype = np.float64)
hsv[:,:,1] = hsv[:,:,1]*value
hsv[:,:,1][hsv[:,:,1]>255] = 255
hsv[:,:,2] = hsv[:,:,2]*value
hsv[:,:,2][hsv[:,:,2]>255] = 255
hsv = np.array(hsv, dtype = np.uint8)
img = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
return img
def fill(img, h, w):
img = img.astype('float32')
img = cv2.resize(img, (h, w), cv2.INTER_CUBIC)
return img
def zoom(img, mask, value):
if value > 1 or value < 0:
print('Value for zoom should be less than 1 and greater than 0')
return img, mask
value = random.uniform(value, 1)
h, w = img.shape[:2]
h_taken = int(value*h)
w_taken = int(value*w)
h_start = random.randint(0, h-h_taken)
w_start = random.randint(0, w-w_taken)
img = img[h_start:h_start+h_taken, w_start:w_start+w_taken, :]
img = fill(img, h, w)
mask = mask[h_start:h_start+h_taken, w_start:w_start+w_taken, :]
mask = fill(mask, h, w)
return img, mask
def vertical_shift(img, mask, ratio=0.0):
if ratio > 1 or ratio < 0:
print('Value should be less than 1 and greater than 0')
return img, mask
ratio = random.uniform(-ratio, ratio)
h, w = img.shape[:2]
to_shift = h*ratio
if ratio > 0:
img = img[:, :int(w-to_shift), :]
mask = mask[:, :int(w-to_shift), :]
if ratio < 0:
img = img[:, int(-1*to_shift):, :]
mask = mask[:, int(-1*to_shift):, :]
img = fill(img, h, w)
mask = fill(mask, h, w)
return img, mask
def horizontal_shift(img, mask, ratio=0.0):
if ratio > 1 or ratio < 0:
print('Value should be less than 1 and greater than 0')
return img, mask
ratio = random.uniform(-ratio, ratio)
h, w = img.shape[:2]
to_shift = w*ratio
if ratio > 0:
img = img[:, :int(w-to_shift), :]
mask = mask[:, :int(w-to_shift), :]
if ratio < 0:
img = img[:, int(-1*to_shift):, :]
mask = mask[:, int(-1*to_shift):, :]
img = fill(img, h, w)
mask = fill(mask, h, w)
return img,mask