-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.py
executable file
·67 lines (56 loc) · 2.08 KB
/
utilities.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
#!/usr/bin/python
import scipy.misc
def crop(image, new_shape):
"""
Returns a cropped version of numpy array image with shape specified.
Will resize and crop the image such that the minimum part of the image is
removed and the cropped image contains the middle of the original one.
"""
shape = image.shape
if shape == new_shape:
return image
# get ratios
ratio = float(shape[0]) / shape[1]
new_ratio = float(new_shape[0]) / new_shape[1]
# index of the dimension to be set to the corresponding one in new_shape,
# the other one is scaled
dim_compare = ratio > new_ratio
scale_factor = float(new_shape[dim_compare]) / shape[dim_compare]
shape = list(shape) # enable editing
# set new shape
shape[not dim_compare] = int(scale_factor * shape[not dim_compare])
shape[dim_compare] = new_shape[dim_compare]
image = scipy.misc.imresize(image, shape)
# add padding such that the middle of the image is retrieved
vertical_padding = int((image.shape[0] - new_shape[0]) / 2)
horizontal_padding = int((image.shape[1] - new_shape[1]) / 2)
# calulate edges
left = horizontal_padding
top = vertical_padding
right = horizontal_padding + new_shape[1]
bottom = vertical_padding + new_shape[0]
# crop image
return image[top:bottom, left:right]
def plot_3d_scatter(axis, data, color='b'):
"""
Simple helper for plotting 3d scatter data.
Plots the first three columns of data.
"""
xs = data[:, 0]
ys = data[:, 1]
zs = data[:, 2]
axis.scatter(xs, ys, zs, c=color)
def visual_compare(data1, data2):
"""
Plots a visual comparision of data1 and data2.
Produces a 3D scatterplot showing data1 (blue) and the data2 (red).
Imports matplotlib.pyplot and mpl_toolkits.mplot3d when called.
"""
import matplotlib.pyplot
import mpl_toolkits.mplot3d
# plot
figure = matplotlib.pyplot.figure()
axis = figure.add_subplot(111, projection='3d')
plot_3d_scatter(axis, data1, color='b')
plot_3d_scatter(axis, data2, color='r')
matplotlib.pyplot.show()