-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_site_points.py
60 lines (43 loc) · 1.91 KB
/
generate_site_points.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
import numpy as np
import sys
# from skimage.viewer import ImageViewer
# from skimage.data import astronaut
from skimage.segmentation import slic
from skimage.util import img_as_float
from skimage.io import imread
def read_image(file_name):
img = img_as_float(imread(file_name)) # height x width x channels
img = np.swapaxes(img, 0, 1) # width x height x channels
# print(type(img), img.shape)
return img
def get_representative_points(img, number_of_site_points):
segments_slic = slic(img, n_segments=number_of_site_points, compactness=10, sigma=1)
# print("SLIC number of segments: ", len(np.unique(segments_slic)))
clusters = np.unique(segments_slic)
cluster_centers, cluster_rgb_means = [], []
# cluster_image = np.zeros(img.shape)
for cluster in clusters:
pixel_indices = np.where(segments_slic == cluster)
pixel_rgbs = img[pixel_indices]
pixel_coords = np.array(list(zip(pixel_indices[0], pixel_indices[1])))
cluster_center = pixel_coords.mean(axis=0)
cluster_rgb_mean = pixel_rgbs.mean(axis=0)
# cluster_image[pixel_indices] = cluster_rgb_mean
cluster_centers.append(cluster_center)
cluster_rgb_means.append(cluster_rgb_mean)
# viewer = ImageViewer(cluster_image)
# viewer.show()
return cluster_centers, cluster_rgb_means
def save_site_points_colors(img, points, colors):
with open('site_points.txt', 'w') as point_file:
print(len(points), img.shape[0], img.shape[1], file=point_file)
for point in points:
print(*list(point), file=point_file)
np.save('site_colors', colors)
if __name__ == "__main__":
if len(sys.argv) < 3:
print('Usage: python voronoi_art.py <image file name> <# of site points>')
exit(0)
img = read_image(sys.argv[1])
points, colors = get_representative_points(img, int(sys.argv[2]))
save_site_points_colors(img, points, colors)