-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcolorized_voxels_demo.py
183 lines (141 loc) · 5.06 KB
/
colorized_voxels_demo.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
from DataManager.manager import get_visualization_batch, get_batch
from PIL import Image
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.image as im
import scipy.io as sio
import numpy as np
#######################
### Scaling Imports ###
#######################
# from mpl_toolkits.mplot3d.axes3d import Axes3D
STRIDE = 1 # Higher stride means more subsampling (faster rendering, worse quality)
########################
### Helper Functions ###
########################
"""Displays a single image retrieved using get_batch()."""
def demo_cropped():
images, voxel_lst = get_batch(1)
image = Image.fromarray(images[0], 'RGB')
voxels = voxel_lst[0]
visualize_voxels_cropped(image, voxels, save=True)
def demo(save=False):
images, voxel_lst, transforms = get_visualization_batch(1)
image = Image.fromarray(images[0], 'RGB')
voxels = voxel_lst[0]
transform = transforms[0]
visualize_voxels_original(image, voxels, transform, save)
'''Visualizes the voxels on top of the cropped image.
Input: The cropped image and the voxels.
Output: If save flag set, saves different views of the
visualization in root. Otherwise, simply opens a window with
the visualization from a side-front view.
'''
def visualize_voxels_cropped(cropped_image, voxels, save=False, file_prefix="voxels"):
fig = plt.figure()
fig.subplots_adjust(top=1, bottom=0, left=0, right=1)
ax = fig.add_subplot(111, projection='3d')
ax.set_ylim(0,100)
xs, ys, zs, colors = [], [], [], []
base_xs, base_ys, base_zs, base_colors = [], [], [], []
for x in range(0, 192, STRIDE):
for y in range(0, 192, STRIDE):
# color = np.array(list(cropped_image.getpixel((x, y)))) / 255.0
color = (0,0,0)
base_xs.insert(0, x)
base_ys.insert(0, 192-y)
base_zs.insert(0, 0)
base_colors.insert(0, (1,1,1))
for z in np.argwhere(voxels[x][y] == 1).T[0]:
xs.append(x)
ys.append(192-y)
zs.append(z)
colors.append(color)
# Shift z-coordinates of voxels to have 0 mean
avg_z = sum(zs) / float(len(zs)) if float(len(zs)) > 0 else 0
# Clip negative z-coordinates to 0
zs = [max(z - avg_z, 0) for z in zs]
xs = np.concatenate((base_xs, xs))
ys = np.concatenate((base_ys, ys))
zs = np.concatenate((base_zs, zs))
colors = base_colors + colors
# colors = np.concatenate((base_colors, colors))
ax.scatter(xs=xs, ys=zs, zs=ys, color=colors, s=8)
if save:
ax.view_init(elev=0, azim=90)
plt.savefig(file_prefix + "_front.jpg")
ax.view_init(elev=25, azim=50)
plt.savefig(file_prefix + "_corner.jpg")
ax.view_init(elev=0, azim=0)
plt.savefig(file_prefix + "_side.jpg")
ax.view_init(elev=0, azim=50)
plt.savefig(file_prefix + "_side_front.jpg")
else:
ax.view_init(elev=0, azim=50)
plt.show()
'''Visualizes the voxels on top of the original image.
Input: The original image, the voxels, and the transform
performed on the voxels during preprocessing.
Output: If save flag set, saves different views of the
visualization in root. Otherwise, simply opens a window with
the visualization from a corner view.
'''
def visualize_voxels_original(image, voxels, transform, save=False):
fig = plt.figure(figsize=(8,8))
fig.subplots_adjust(top=1, bottom=0, left=0, right=1)
ax = fig.add_subplot(111, projection='3d')
###### Scaling Section #######
x_scale=4.0
y_scale=1.0
z_scale=4.0
scale=np.diag([x_scale, y_scale, z_scale, 1.0])
scale=scale*(1.0/scale.max())
scale[3,3]=1.0
ax.get_proj = lambda: np.dot(Axes3D.get_proj(ax), scale)
##############################
Ainv = np.linalg.inv(transform['A'])
b = transform['b']
xs, ys, zs, colors = [], [], [], []
for x in range(0, 192, STRIDE):
for y in range(0, 192, STRIDE):
for z in np.argwhere(voxels[x][y] == 1).T[0]:
inv_coords = Ainv.dot(np.subtract(np.array([x, y, z]), b.T)[0])
xt, yt = int(inv_coords[0]), int(inv_coords[1])
color = np.array(list(image.getpixel((xt, yt)))) / 255.0
xs.append(xt)
ys.append(image.size[1] - yt)
zs.append(z)
colors.append(color)
# Shift z-coordinates of voxels to have 0 mean
avg_z = sum(zs) / float(len(zs))
# Clip negative z-coordinates to 0
zs = [max(z - avg_z, 0) for z in zs]
# Add base layer using original image
for x in range(0, image.size[0], STRIDE):
for y in range(0, image.size[1], STRIDE):
color = np.array(list(image.getpixel((x, y)))) / 255.0
xs.insert(0, x)
ys.insert(0, image.size[1] - y)
zs.insert(0, 0)
colors.insert(0, color)
ax.scatter(xs=xs, ys=zs, zs=ys, color=colors, s=5)
ax.set_ylim(0,100)
plt.axis('off')
if save:
# Used to crop whitespace
bbox = fig.bbox_inches.from_bounds(0, 1, 5, 7)
bbox_front = fig.bbox_inches.from_bounds(0, 0, 8, 8)
# ax.view_init(elev=0, azim=90)
# plt.savefig("voxels_front.jpg")
ax.view_init(elev=20, azim=40)
plt.savefig("voxels_corner.jpg", bbox_inches=bbox)
ax.view_init(elev=0, azim=0)
plt.savefig("voxels_side.jpg", bbox_inches=bbox)
# ax.view_init(elev=0, azim=40)
# plt.savefig("voxels_side_front.jpg", bbox_inches=bbox)
else:
ax.view_init(elev=20, azim=40)
plt.show()
if __name__ == '__main__':
demo(save=True)