-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_3d.py
96 lines (86 loc) · 3.08 KB
/
plot_3d.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
#!/usr/bin/env python3
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation
from util import norm_rgb
from kmeans import *
import matplotlib.pyplot as plt
import numpy as np
import pickle
FREQ_BY_PIXEL_COUNT_PKL = 'data/freq_by_pixel_count.pkl'
FREQUENCY_THRESHOLD = 20000
MAX_POINT_SIZE = 200
def clamp(x, low, high):
return max(min(x, high), low)
def get_data():
with open(FREQ_BY_PIXEL_COUNT_PKL, 'rb') as f:
data = pickle.load(f)
colors, frequencies = [], []
for key in data:
if data[key] > FREQUENCY_THRESHOLD:
colors.append(key)
frequencies.append(data[key])
return colors, frequencies
def plot_color_frequencies(colors, frequencies):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r, g, b = zip(*colors)
m = sorted(frequencies)[-10]
sizes = list(map(lambda x: clamp(x / m, 0.05, 1) * MAX_POINT_SIZE, frequencies))
ax.scatter(r, g, b, c=[norm_rgb(color) for color in colors], s=sizes)
ax.set_xlabel('R')
ax.xaxis.label.set_color('red')
ax.set_ylabel('G')
ax.yaxis.label.set_color('green')
ax.set_zlabel('B')
ax.zaxis.label.set_color('blue')
ax.set_xlim(0, 256)
ax.set_xticks(range(0, 257, 32))
ax.tick_params(axis='x', colors='red')
ax.set_ylim(0, 256)
ax.set_yticks(range(0, 257, 32))
ax.tick_params(axis='y', colors='green')
ax.set_zlim(0, 256)
ax.set_zticks(range(0, 257, 32))
ax.tick_params(axis='z', colors='blue')
return fig, ax
def plot_color_clusters(colors, frequencies):
centroids, clusters = kmeans(8, colors, frequencies, 1)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
r, g, b = zip(*centroids)
colors = [norm_rgb(color) for color in centroids]
ax.scatter(r, g, b, c=colors, s=100)
for color, cluster in zip(colors, clusters):
r, g, b = zip(*cluster)
ax.scatter(r, g, b, c=color, s=10)
ax.set_xlabel('R')
ax.xaxis.label.set_color('red')
ax.set_ylabel('G')
ax.yaxis.label.set_color('green')
ax.set_zlabel('B')
ax.zaxis.label.set_color('blue')
ax.set_xlim(0, 256)
ax.set_xticks(range(0, 257, 32))
ax.tick_params(axis='x', colors='red')
ax.set_ylim(0, 256)
ax.set_yticks(range(0, 257, 32))
ax.tick_params(axis='y', colors='green')
ax.set_zlim(0, 256)
ax.set_zticks(range(0, 257, 32))
ax.tick_params(axis='z', colors='blue')
return fig, ax
if __name__ == '__main__':
colors, frequencies = get_data()
fig, ax = plot_color_clusters(colors, frequencies)
ax.view_init(30, 0)
def update(i):
ax.view_init(30, i)
anim = FuncAnimation(fig, update, frames=np.arange(0, 360), interval=5)
anim.save('render/output/plot_3d_clusters.gif',
dpi=80, writer='imagemagick')
# fig, ax = plot_color_frequencies(colors, frequencies)
# ax.view_init(30, 0)
# def update(i):
# ax.view_init(30, i)
# anim = FuncAnimation(fig, update, frames=np.arange(0, 360), interval=5)
# anim.save('render/output/plot_3d.gif', dpi=80, writer='imagemagick')