-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualize_primitive.py
236 lines (206 loc) · 6.79 KB
/
visualize_primitive.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
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import torch
from field_geometry import *
from typing import Dict, Tuple
def rectangle_on_img(prim_dict: Dict, img: torch.tensor, rect: torch.tensor, mode: str="gray", sample_size: int=4):
# print("img:", img.shape)
# print("rect:", rect.shape)
# print("first img", img[0, 0, 0].shape)
# print("first rect", rect[0, 0].shape)
# create the figure
fig, ax = plt.subplots(1, sample_size, figsize=(20,20))
print(img.shape)
# display the image
edgecolors = ["b", "r", "g", "b", "r", "g","b", "r", "g", "b", "r", "g"]
# create the rectangle values needed
for j, axis in enumerate(ax):
axis.imshow(torch.permute(img[j],(1, 2, 0)))
for i in range(prim_dict["Rectangles"]):
rect_length = [
abs(rect[i, j, 2] - rect[i, j, 0]),
abs(rect[i, j, 3] - rect[i, j, 1]),
] * 255
# create a rectangle patch
fig_rect = patches.Rectangle(
(rect[i, j, 0:2])*255,
rect_length[0],
rect_length[1],
linewidth=1,
edgecolor=edgecolors[i],
facecolor="none"
)
axis.add_patch(fig_rect)
# axis.scatter(x=rect[i, j, 0] * 255, y= rect[i, j, 1] * 255)
# axis.scatter(x=rect[i, j, 2] * 255, y= rect[i, j, 3] * 255)
plt.show()
def plot_rectangles(
ax: torch.tensor,
r: torch.tensor,
img_size:Tuple[int, int]
):
# compute rectangle
r = r.sort(dim=-1).values
r = r * torch.FloatTensor(tuple(img_size))
wh = r[:, :, 1] - r[:, :, 0]
# display the image
color = ["b", "r", "g"]
for i in range(r.size(0)):
# create a rectangle patch
fig_rect = patches.Rectangle(
r[i, (1, 0), 0],
wh[i, 1],
wh[i, 0],
linewidth=1,
edgecolor=color[i % len(color)],
facecolor=color[i % len(color)],
alpha=0.3
)
ax.add_patch(fig_rect)
# create a rectangle patch
fig_rect = patches.Rectangle(
r[i, (1, 0), 0],
wh[i, 1],
wh[i, 0],
linewidth=1,
edgecolor=color[i % len(color)],
facecolor='none',
alpha=1
)
ax.add_patch(fig_rect)
return ax
def plot_rotated_rectangles(
ax: torch.tensor,
r: torch.tensor,
t: torch.tensor,
img_size:Tuple[int, int]
):
r = r * torch.FloatTensor(tuple(img_size))
# rotate rectangle
rec_all_anchors = compute_axis_aligned_rectagle_anchor_points(r)
rec_rot = rotate_rectangle(rec_all_anchors, t)
# display the image
color = ["b", "r", "g"]
for i in range(r.size(0)):
# create a rectangle patch
fig_rect = patches.Polygon(
rec_rot[i, :, :].t(),
linewidth=1,
edgecolor=color[i % len(color)],
facecolor=color[i % len(color)],
alpha=0.3
)
ax.add_patch(fig_rect)
# create a rectangle patch
fig_rect = patches.Polygon(
rec_rot[i, :, :].t(),
linewidth=1,
edgecolor=color[i % len(color)],
facecolor='none',
alpha=1
)
ax.add_patch(fig_rect)
return ax
def plot_circles(
ax: torch.tensor,
c: torch.tensor,
img_size:Tuple[int, int]
):
# compute circle
c = c * img_size[0]
# display the image
color = ["b", "r", "g"]
for i in range(c.size(0)):
# create a circle patch
fig_rect = patches.Circle(
c[i, (1, 0)],
c[i, 2],
linewidth=1,
edgecolor=color[i % len(color)],
facecolor=color[i % len(color)],
alpha=0.3
)
ax.add_patch(fig_rect)
# create a circle patch
fig_rect = patches.Circle(
c[i, (1, 0)],
c[i, 2],
linewidth=1,
edgecolor=color[i % len(color)],
facecolor='none',
alpha=1
)
ax.add_patch(fig_rect)
return ax
def compute_rotation_matrix(theta):
# compute sine and cosine
sin_theta = torch.sin(theta)
cos_theta = torch.cos(theta)
# build flat rotation matrix and unflatten
R = torch.stack([cos_theta, sin_theta, -sin_theta, cos_theta], dim=-1)
# R = torch.stack([-sin_theta, cos_theta, cos_theta, sin_theta], dim=-1)
# R = torch.stack([sin_theta, cos_theta, cos_theta, -sin_theta], dim=-1)
R = R.reshape(-1, 2, 2)
return R[:, :, (1, 0)]
def compute_center_points(rec):
return rec.mean(dim=-1, keepdims=True)
def rotate_points(points, R, p0):
# check dimensions
assert points.size(0) == R.size(0) == p0.size(0)
assert points.ndim == p0.ndim
# rotate points according to formula
# return (R @ (points - p0).unsqueeze(-1)).reshape(p0.size())
return p0[:, (1, 0)] + (R @ (points - p0).unsqueeze(-1)).reshape(p0.size())
def rotate_rectangle(rec, theta):
n = rec.size(-1)
# compute rotation matrix and centers of rotation
# apply deg2rad to get correct theta
R = compute_rotation_matrix(torch.deg2rad(theta))
p0 = compute_center_points(rec)
# repeat
R = R.repeat_interleave(n, dim=0).reshape(-1, 2, 2)
p0 = p0.repeat_interleave(n, dim=0).reshape(-1, 2)
# rotate points
p_rot = rotate_points(
points=rec.permute(0, 2, 1).reshape(-1, 2),
R=R,
p0=p0
)
# unravel rotated points back to rectangles
return p_rot.reshape(-1, n, 2).permute(0, 2, 1)
def compute_axis_aligned_rectagle_anchor_points(rec):
assert rec.size(-1) == 2
assert rec.size(-2) == 2
rec = rec.sort(dim=-1).values
rec_swap = rec.clone()
rec_swap[:, 0, :] = rec_swap[:, 0, (1, 0)]
return torch.stack((
rec[:, :, 0],
rec_swap[:, :, 0],
rec[:, :, 1],
rec_swap[:, :, 1]
), dim=-1)
def plot_rotated_rectangles_field(
ax: torch.tensor,
r: torch.tensor,
r_rot: torch.tensor,
img_size:Tuple[int,int]
):
colors = ["red", "green", "blue"]
rect_field = compute_rotated_rectangle_distance_field(r, r_rot, torch.tensor(img_size))
mask_rect_field = (rect_field == 0) * 1
for i in range(r.size(0)):
gray_mask = mask_rect_field[i].squeeze()
rgb_mask = gray_to_rgb(gray_mask, colors[i % len(colors)])
ax.imshow(rgb_mask, alpha= 0.2)
def gray_to_rgb(mask, color):
zeros = torch.zeros_like(mask)
if color == "red":
red = torch.stack([mask * 255, zeros, zeros]).permute(1, 2, 0)
return red
elif color == "green":
green = torch.stack([zeros, mask * 255, zeros]).permute(1, 2, 0)
return green
elif color == "blue":
blue = torch.stack([zeros, zeros, mask * 255]).permute(1, 2, 0)
return blue