-
Notifications
You must be signed in to change notification settings - Fork 8
/
Extract_PD_palettes.py
81 lines (48 loc) · 1.98 KB
/
Extract_PD_palettes.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
import numpy as np
from SILD_convexhull_simplification import *
from scipy.spatial import ConvexHull
import os, sys
import PIL.Image as Image
import errno
def make_sure_path_exists(path):
try:
os.makedirs(path)
except OSError as exception:
if exception.errno != errno.EEXIST:
raise
if __name__=="__main__":
img_file=sys.argv[1]
M=np.int(sys.argv[2])
foldername=sys.argv[3]
current_folder="."+foldername+"/"
output_prefix=current_folder+"Tan2016_PD_results/"
make_sure_path_exists(output_prefix)
output_prefix=output_prefix+os.path.splitext(img_file)[0]+"-"+str(M)
output_rawhull_obj_file=output_prefix+"-temp_mesh_objfile.obj"
img=Image.open(current_folder+img_file).convert("RGB")
img.save(current_folder+"Tan2016_PD_results/"+img_file)
data=np.asfarray(img).reshape((-1,3))
hull=ConvexHull(data)
write_convexhull_into_obj_file(hull, output_rawhull_obj_file)
N=5000
mesh=TriMesh.FromOBJ_FileName(output_rawhull_obj_file)
for i in range(N):
old_num=len(mesh.vs)
mesh=TriMesh.FromOBJ_FileName(output_rawhull_obj_file)
mesh=remove_one_edge_by_finding_smallest_adding_volume_with_test_conditions(mesh,option=2)
newhull=ConvexHull(mesh.vs)
write_convexhull_into_obj_file(newhull, output_rawhull_obj_file)
if len(mesh.vs)==M:
Final_hull=newhull
break
Hull_vertices=Final_hull.points[Final_hull.vertices].clip(0,255)
Hull_vertices=Hull_vertices.round().astype(np.uint8)
print Hull_vertices
import json
with open (output_prefix+"-PD_palettes.js", "w") as myfile:
json.dump({"vs":Hull_vertices.reshape((-1,3)).tolist()}, myfile)
Hull_vertices=Hull_vertices.reshape((1, M, 3))
palette_images=np.zeros((50,50*M,3),dtype=np.uint8)
for i in range(M):
palette_images[:,50*i:50*i+50,:]=Hull_vertices[:,i:i+1,:]
Image.fromarray(palette_images).save(output_prefix+"-PD_palettes.png")