-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_creator.py
81 lines (65 loc) · 2.53 KB
/
image_creator.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
import numpy
import numpy as np
import cv2
from random import randint
from rl_project.config import *
# noinspection DuplicatedCode
def create_img(x_cube_in_img, y_cube_in_img):
cube = cubes[randint(0, len(cubes) - 1)]
new_cube = np.zeros(background.shape)
x_offset = int(x_cube_in_img - (cube.shape[0] / 2))
y_offset = int(y_cube_in_img - (cube.shape[1] / 2))
new_cube[y_offset:y_offset + cube.shape[0], x_offset:x_offset + cube.shape[1]] = cube
# create overlay img
final = np.zeros(background.shape)
w, h, c = background.shape
for iw in range(w):
for ih in range(h):
if not new_cube[iw][ih].any():
final[iw][ih] = background[iw][ih]
else:
final[iw][ih] = new_cube[iw][ih]
if debug:
cv2.imwrite('images/overlay.png', final)
return final
def create_binary_img(x_cube_in_img, y_cube_in_img):
cube = cubes[randint(0, len(cubes) - 1)]
new_cube = np.zeros(background.shape)
x_offset = int(x_cube_in_img - (cube.shape[0] / 2))
y_offset = int(y_cube_in_img - (cube.shape[1] / 2))
new_cube[y_offset:y_offset + cube.shape[0], x_offset:x_offset + cube.shape[1]] = cube
# create overlay image
w, h, c = background.shape
final = np.zeros((w, h, 3), )
for iw in range(w):
for ih in range(h):
for iz in range(3):
if not new_cube[iw][ih].any():
final[iw][ih][iz] = 0
else:
final[iw][ih][iz] = 255
if debug:
cv2.imwrite('images/overlay.png', final)
return final
def create_multiimage(locations):
new_cube = np.zeros(background.shape)
for loc in locations:
cube = cubes[randint(0, len(cubes) - 1)]
x_middle, y_middle = loc
x_offset = int(x_middle - (cube.shape[0] / 2))
y_offset = int(y_middle - (cube.shape[1] / 2))
new_cube[y_offset:y_offset + cube.shape[0], x_offset:x_offset + cube.shape[1]] = cube
# create overlay img
# noinspection DuplicatedCode
final = np.zeros(background.shape)
w, h, c = background.shape
for iw in range(w):
for ih in range(h):
if not new_cube[iw][ih].any():
final[iw][ih] = background[iw][ih]
else:
final[iw][ih] = new_cube[iw][ih]
if debug:
cv2.imwrite('new_overlay.png', final)
final = cv2.resize(final, (int(h / resize_factor), int(w / resize_factor)))
return final