-
Notifications
You must be signed in to change notification settings - Fork 9
/
figures.py
203 lines (163 loc) · 6.55 KB
/
figures.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
"""
Python module for use with openCV to display keypoints
and their links between images
Jonas Toft Arnfred, 2013-04-24
"""
####################################
# #
# Imports #
# #
####################################
import pylab
import numpy
import imaging
####################################
# #
# Images #
# #
####################################
def append_images(im1, im2, seperator = 0) :
""" return a new image that appends the two images side-by-side.
"""
barrier = numpy.ones((im1.shape[0],seperator, 3), dtype=numpy.float)
im1_float = im1 / 255.0
im2_float = im2 / 255.0
if (im1.shape[0] == im2.shape[0]) :
tmp = im2_float
elif (im1.shape[0] > im2.shape[0]) :
tmp = numpy.ones((im1.shape[0], im2.shape[1], 3), dtype=numpy.float)
tmp[0:im2.shape[0], :, :] = im2_float
elif (im1.shape[0] < im2.shape[0]) :
tmp = numpy.ones((im1.shape[0], im2.shape[1], 3), dtype=numpy.float)
tmp[0:im1.shape[0], :, :] = im2_float[0:im1.shape[0], :, :]
else :
print("Detonating thermo-nuclear devices")
return numpy.concatenate((im1_float, barrier, tmp), axis=1)
def keypoints(im, pos) :
""" show image with features. input: im (image as array),
locs (row, col, scale, orientation of each feature)
"""
# Plot all keypoints
pylab.imshow(im)
for i, (x,y) in enumerate(pos) :
pylab.plot(x, y, marker='.', color = getRedGreen(float(i+1) / len(pos)))
pylab.axis('off')
pylab.show()
def compare_keypoints(im1, im2, pos1, pos2, filename = None, separation = 0) :
""" Show two images next to each other with the keypoints marked
"""
# Construct unified image
im3 = append_images(im1,im2, separation)
# Find the offset and add it
offset = im1.shape[1]
pos2_o = [(x+offset + separation,y) for (x,y) in pos2]
# Create figure
fig = pylab.figure(frameon=False, figsize=(12.0, 8.0))
#ax = pylab.Axes(fig, [0., 0., 1., 1.])
# Show images
pylab.gray()
pylab.imshow(im3)
pylab.plot([x for x,y in pos1], [y for x,y in pos1], marker='o', color = '#00aaff', lw=0)
pylab.plot([x for x,y in pos2_o], [y for x,y in pos2_o], marker='o', color = '#00aaff', lw=0)
pylab.axis('off')
pylab.xlim(0,im3.shape[1])
pylab.ylim(im3.shape[0],0)
if filename != None :
fig.savefig(filename, bbox_inches='tight', dpi=300)
def matches(im1, im2, matches, dist = None, options = {}) :
""" show a figure with lines joining the accepted matches in im1 and im2
input: im1,im2 (images as arrays), locs1,locs2 (location of features),
matchscores (as output from 'match').
"""
scale = options.get("scale", 1)
filename = options.get("filename", None)
max_dist = options.get("max_dist", 100)
line_width = options.get("line_width", 0.8)
size = options.get("size", (12, 8))
separation = options.get("separation", 20)
if scale != 1 :
s = numpy.array([scale, scale])
im1_size = numpy.array(im1.shape[1::-1] * s, dtype=numpy.uint16)
im2_size = numpy.array(im2.shape[1::-1] * s, dtype=numpy.uint16)
if scale < 0.5 :
im1_scaled = imaging.get_thumbnail(im1, size = im1_size)
im2_scaled = imaging.get_thumbnail(im2, size = im2_size)
else :
im1_scaled = imaging.open_img(im1, size = im1_size)
im2_scaled = imaging.open_img(im2, size = im2_size)
im3 = append_images(im1_scaled, im2_scaled, separation)
matches = [(m[0] * s, m[1] * s) for m in matches]
else :
im3 = append_images(im1,im2, separation)
im1_scaled = im1
# Create figure
fig = pylab.figure(frameon=False, figsize=size)
ax = pylab.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
# Display image
ax.imshow(im3)
# Get colors
if dist != None and len(dist) == len(matches) :
cs = [getRedGreen(numpy.log(d+1)/numpy.log(max_dist)) for d in dist]
else :
cs = ['#00aaff' for m in matches]
# Plot all lines
offset_x = im1_scaled.shape[1]
for i,((x1,y1),(x2,y2)) in enumerate(matches) :
ax.plot([x1, x2+offset_x + separation], [y1,y2], color=cs[i], lw=line_width)
pylab.xlim(0,im3.shape[1])
pylab.ylim(im3.shape[0],0)
if filename != None :
fig.savefig(filename, bbox_inches='tight', dpi=72)
def getRedGreen(f) :
if f > 1 : f = 1
elif f < 0 : f = 0
c = '#%02x%02x11' % (int(f*255), int((1-f)*255))
return c
def visualize_log(log, im1, im2, stop_at = None, scale = None, size = (14, 8)) :
# Prepare images
if scale == None :
scale = min(1.0, 600 / float(im1.shape[1]))
s = numpy.array([scale, scale])
im1_size = numpy.array(im1.shape[1::-1] * s, dtype=numpy.uint16)
im2_size = numpy.array(im2.shape[1::-1] * s, dtype=numpy.uint16)
im1_scaled = imaging.get_thumbnail(im1, size = im1_size)
im2_scaled = imaging.get_thumbnail(im2, size = im2_size)
separation = 20
offset_x = im1_size[0] + separation
im3 = append_images(im1_scaled, im2_scaled, separation)
def translate_point(point, image = "im1") :
x, y = numpy.array(point) * s
if image == "im1" :
return x, y
else :
return (x + offset_x, y)
# Create figure
fig = pylab.figure(frameon=False, figsize=size)
ax = pylab.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)
# Display image
ax.imshow(im3)
# Plot log data
for i, d in enumerate(log) :
# Enough?
if stop_at != None and i > stop_at :
break
# Plot target grid
((x_min, x_max), (y_min, y_max)) = d["target_grid"]
margin = d["margin"]
# Add square
ax.add_patch(pylab.Rectangle( translate_point((x_min + margin, y_min + margin), "im2"),
(x_max-x_min - 2*margin) * scale, (y_max-y_min - 2*margin) * scale, fill = False))
# Add circle
ax.add_patch(pylab.Circle( translate_point(d["query_pos"], "im1"), d["radius"] * scale, fill = False, linewidth = 0.5))
# Add matches
for pos_q, pos_t in d["matches"] :
x1, x2 = numpy.array([pos_q[0], pos_t[0]]) * s
y1, y2 = numpy.array([pos_q[1], pos_t[1]]) * s
ax.plot([x1, x2+offset_x], [y1,y2], color="#2595e3", lw=1)
# Limit figure to area around im3
pylab.xlim(0,im3.shape[1])
pylab.ylim(im3.shape[0],0)