-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.py
267 lines (230 loc) · 11.4 KB
/
algorithm.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import albumentations as A
import cv2
import numpy as np
class Algorithm:
def rotate(self, limit, border):
if border == "Constant":
border_mode = 1
elif border == "Replicate":
border_mode = 2
elif border == "Reflect":
border_mode = 3
transforms = A.Compose([
A.augmentations.geometric.rotate.Rotate(limit=limit, border_mode=border_mode, p=1),
])
return transforms
def rotateOD(self, limit, border):
if border == "Constant":
border_mode = 1
elif border == "Replicate":
border_mode = 2
elif border == "Reflect":
border_mode = 3
transforms = A.Compose([
A.augmentations.geometric.rotate.Rotate(limit=limit, border_mode=border_mode, p=1),
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def Rcrop(self, width, height):
transforms = A.Compose([
A.RandomCrop(width=width, height=height)
])
return transforms
def RcropOD(self, width, height):
transforms = A.Compose([
A.RandomCrop(width=width, height=height)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def Mcrop(self, xmin, ymin, xmax, ymax):
transforms = A.Compose([
A.Crop(x_min=int(xmin), y_min=int(ymin), x_max=int(xmax), y_max=int(ymax))
])
return transforms
def McropOD(self, xmin, ymin, xmax, ymax):
transforms = A.Compose([
A.Crop(x_min=int(xmin), y_min=int(ymin), x_max=int(xmax), y_max=int(ymax))
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def resize(self, width, height):
transforms = A.Compose([
A.Resize(p=1, height=height, width=width)
])
return transforms
def resizeOD(self, width, height):
transforms = A.Compose([
A.Resize(p=1, height=height, width=width)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def hflip(self):
transforms = A.Compose([
A.HorizontalFlip(p=1)
])
return transforms
def hflipOD(self):
transforms = A.Compose([
A.HorizontalFlip(p=1)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def vflip(self):
transforms = A.Compose([
A.VerticalFlip(p=1)
])
return transforms
def vflipOD(self):
transforms = A.Compose([
A.VerticalFlip(p=1)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def randomScale(self, scale_limit, interpolation):
transforms = A.Compose([
A.RandomScale(p=1, scale_limit=scale_limit, interpolation=interpolation)
])
return transforms
def randomScaleOD(self, scale_limit, interpolation):
transforms = A.Compose([
A.RandomScale(p=1, scale_limit=scale_limit, interpolation=interpolation)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def Blur(self, blur_limit):
transforms = A.Compose([
A.Blur(always_apply=False, p=1)
])
return transforms
def BlurOD(self, blur_limit):
transforms = A.Compose([
A.Blur(p=1, blur_limit=blur_limit)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def HueSaturationValue(self,hue_shift_limit,sat_shift_limit,val_shift_limit):
transforms = A.Compose([
A.HueSaturationValue(p=1, hue_shift_limit=hue_shift_limit, sat_shift_limit=sat_shift_limit, val_shift_limit=val_shift_limit)
])
return transforms
def HueSaturationValueOD(self,hue_shift_limit,sat_shift_limit,val_shift_limit):
transforms = A.Compose([
A.HueSaturationValue(p=1, hue_shift_limit=hue_shift_limit, sat_shift_limit=sat_shift_limit, val_shift_limit=val_shift_limit)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def RandomBrightnessContrast(self,brightness_limit,contrast_limit ):
transforms = A.Compose([
A.RandomBrightnessContrast(p=1, brightness_limit=brightness_limit, contrast_limit=contrast_limit)
])
return transforms
def RandomBrightnessContrastOD(self,brightness_limit,contrast_limit ):
transforms = A.Compose([
A.RandomBrightnessContrast(p=1, brightness_limit=brightness_limit, contrast_limit=contrast_limit)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def Solarize(self, threshold):
transforms = A.Compose([
A.Solarize(p=1, threshold=threshold)
])
return transforms
def SolarizeOD(self, threshold):
transforms = A.Compose([
A.Solarize(p=1, threshold=threshold)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def RandomGamma(self, gamma_limit):
transforms = A.Compose([
A.RandomGamma(p=1, gamma_limit=gamma_limit)
])
return transforms
def RandomGammaOD(self, gamma_limit):
transforms = A.Compose([
A.RandomGamma(p=1, gamma_limit=gamma_limit)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def GaussNoise(self, var_limit, mean):
transforms = A.Compose([
A.GaussNoise(p=1, var_limit=var_limit, mean=mean)
])
return transforms
def GaussNoiseOD(self, var_limit, mean):
transforms = A.Compose([
A.GaussNoise(p=1, var_limit=var_limit, mean=mean)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def ChannelShuffle(self):
transforms = A.Compose([
A.ChannelShuffle(p=1)
])
return transforms
def ChannelShuffleOD(self):
transforms = A.Compose([
A.ChannelShuffle(p=1)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def CoarseDropout(self, min_holes, max_holes, min_height, max_height, min_width, max_width, fill_value):
transforms = A.Compose([
A.CoarseDropout(p=1, min_holes=int(min_holes), max_holes=int(max_holes), min_height=int(min_height),
max_height=int(max_height), min_width=int(min_width), max_width=int(max_width), fill_value=fill_value)
])
return transforms
def CoarseDropoutOD(self, min_holes, max_holes, min_height, max_height, min_width, max_width, fill_value):
transforms = A.Compose([
A.CoarseDropout(p=1, min_holes=int(min_holes), max_holes=int(max_holes), min_height=int(min_height),
max_height=int(max_height), min_width=int(min_width), max_width=int(max_width), fill_value=fill_value)
], bbox_params=A.BboxParams(format='pascal_voc'))
return transforms
def multiTransform(self, param_array):
transforms = A.Compose([
A.augmentations.geometric.rotate.Rotate(limit=param_array["rotate_limit"], border_mode=param_array["border_mode"], p=1),
A.RandomCrop(width=param_array["width_crop"], height=param_array["height_crop"]),
A.Resize(p=1, height=param_array["height_resize"], width=param_array["width_resize"]),
A.Solarize(p=1, threshold=param_array["threshold"])
])
return transforms
def multiTransformF(self, param_array):
transforms = A.Compose([
A.augmentations.geometric.rotate.Rotate(limit=param_array["rotate_limit"],
border_mode=param_array["border_mode"], p=1),
A.RandomCrop(width=param_array["width_crop"], height=param_array["height_crop"]),
A.Crop(x_min=int(param_array["xmin"]), y_min=int(param_array["ymin"]),
x_max=int(param_array["xmax"]), y_max=int(param_array["ymax"])),
A.Resize(p=1, height=param_array["height_resize"], width=param_array["width_resize"]),
A.HorizontalFlip(p=1),
A.VerticalFlip(p=1),
A.RandomScale(p=1, scale_limit=param_array["scale_limit"], interpolation=param_array["interpolation"]),
A.ZoomBlur(p=1, max_factor=param_array["max_factor"], step_factor=param_array["step_factor"]),
A.HueSaturationValue(p=1, hue_shift_limit=param_array["hue_shift_limit"],
sat_shift_limit=param_array["sat_shift_limit"], val_shift_limit=param_array["val_shift_limit"]),
A.RandomBrightnessContrast(p=1, brightness_limit=param_array["brightness_limit"], contrast_limit=param_array["contrast_limit"]),
A.Solarize(p=1, threshold=param_array["threshold"])
])
return transforms
def scale_contrast(self, mean_shift, contrast_scaling, img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
mean_val = np.mean(gray_img)
std_dev = np.std(gray_img)
normalized_img = (gray_img - mean_val) / std_dev
# Modify the constants for contrast scaling and mean shift
scaled_contrast_img = mean_shift + contrast_scaling * normalized_img
scaled_contrast_img = np.clip(scaled_contrast_img, 0, 255).astype(np.uint8)
scaled_contrast_img = cv2.cvtColor(scaled_contrast_img, cv2.COLOR_GRAY2RGB)
return scaled_contrast_img
def log(self, c, gamma, img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply the log transformation with adjustable parameters
log_transformed = c * (np.log1p(gray_img) ** gamma)
# Scale the pixel values to the valid range (0-255)
log_transformed = ((log_transformed - np.min(log_transformed)) / (np.max(log_transformed) - np.min(log_transformed))) * 255
log_transformed = log_transformed.astype(np.uint8)
log_transformed = cv2.cvtColor(log_transformed, cv2.COLOR_GRAY2RGB)
return log_transformed
def normalization(self, alpha, beta, img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
normalized_img = cv2.convertScaleAbs(gray_img, alpha=alpha, beta=beta)
normalized_img = cv2.cvtColor(normalized_img, cv2.COLOR_GRAY2RGB)
return normalized_img
def absolute(self, img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
absolute_transformed = np.abs(gray_img)
absolute_transformed = cv2.cvtColor(absolute_transformed, cv2.COLOR_GRAY2RGB)
return absolute_transformed
def decay(self, decay_factor, img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
decayed_log_transformed = np.log(1 + decay_factor * gray_img)
decayed_log_transformed = ((decayed_log_transformed - np.min(decayed_log_transformed)) / (np.max(decayed_log_transformed) - np.min(decayed_log_transformed))) * 255
decayed_log_transformed = decayed_log_transformed.astype(np.uint8)
decayed_log_transformed = cv2.cvtColor(decayed_log_transformed, cv2.COLOR_GRAY2RGB)
return decayed_log_transformed