-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvtransforms.py
More file actions
41 lines (33 loc) · 1.11 KB
/
cvtransforms.py
File metadata and controls
41 lines (33 loc) · 1.11 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
# coding: utf-8
import random
import cv2
import numpy as np
def CenterCrop(batch_img, size):
w, h = batch_img[0][0].shape[1], batch_img[0][0].shape[0]
th, tw = size
img = np.zeros((len(batch_img), len(batch_img[0]), th, tw))
for i in range(len(batch_img)):
x1 = int(round((w - tw))/2.)
y1 = int(round((h - th))/2.)
img[i] = batch_img[i, :, y1:y1+th, x1:x1+tw]
return img
def RandomCrop(batch_img, size):
w, h = batch_img[0][0].shape[1], batch_img[0][0].shape[0]
th, tw = size
img = np.zeros((len(batch_img), len(batch_img[0]), th, tw))
for i in range(len(batch_img)):
x1 = random.randint(0, 8)
y1 = random.randint(0, 8)
img[i] = batch_img[i, :, y1:y1+th, x1:x1+tw]
return img
def HorizontalFlip(batch_img):
for i in range(len(batch_img)):
if random.random() > 0.5:
for j in range(len(batch_img[i])):
batch_img[i][j] = cv2.flip(batch_img[i][j], 1)
return batch_img
def ColorNormalize(batch_img):
mean = 0.413621
std = 0.1700239
batch_img = (batch_img - mean) / std
return batch_img