-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate_transforms.py
executable file
·78 lines (58 loc) · 2.11 KB
/
generate_transforms.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
#!/usr/bin/env python3
import os
from glob import glob
import random
import cv2
import numpy as np
from constants import CHESSBOARDS_DIR, TRANSFORM_DIR, USE_GRAYSCALE
OVERWRITE = False
MIN_ROT = 1 # minimum rotation magnitude in degrees
MAX_ROT = 3
def rotate(img):
h, w = img.shape[:2]
cx, cy = w//2, h//2
scale = 1.0
angle = random.choice([-1, 1]) * random.uniform(MIN_ROT, MAX_ROT)
M = cv2.getRotationMatrix2D((cx, cy), angle, scale)
return cv2.warpAffine(img, M, (w,h))
def transform(img, method):
if (method == "rotate"):
return rotate(img)
# Might consider other transforms such as
# - shear
# - translation
# - fractional scaling (effectively creating a border on one or more sides)
# - convex/concave deformations or warping
# - pincushion/barrel distortion
raise f"Unknown transform method: {method}"
if __name__ == '__main__':
if not os.path.exists(TRANSFORM_DIR):
os.makedirs(TRANSFORM_DIR)
chessboard_paths = glob("{}/*/*.png".format(CHESSBOARDS_DIR))
num_chessboards = len(chessboard_paths)
success = 0
skipped = 0
failed = 0
method = "rotate"
for i, path in enumerate(chessboard_paths):
print("%3d/%d %s" % (i + 1, num_chessboards, path))
path_split = path.split('/')
chessboard_dir = path_split[-2]
filename = path_split[-1]
save_folder = os.path.join(TRANSFORM_DIR, method + "_" + chessboard_dir)
full_save_path = os.path.join(save_folder, filename)
if os.path.exists(full_save_path) and not OVERWRITE:
print("\tIgnoring existing {}\n".format(full_save_path))
skipped += 1
continue
else:
if not os.path.exists(save_folder):
os.makedirs(save_folder)
img = cv2.imread(path)
tx = transform(img, method)
if cv2.imwrite(os.path.abspath(full_save_path), tx):
success += 1
else:
failed += 1
print('Processed {} chessboard images ({} generated, {} skipped, {} write failed)'
.format(num_chessboards, success, skipped, failed))