-
Notifications
You must be signed in to change notification settings - Fork 0
/
test2.py
78 lines (67 loc) · 2.74 KB
/
test2.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
from PIL import Image
from PIL import ImageDraw
from PIL import ImageChops
import quad_coll
import os
depths = [4, 6, 8, 12, 14, 16]
sizes = [2, 4, 6, 8, 10]
ratios = [.01, .05, .1, .25]
opacities = [.1, .25, .5, .75, .9]
for f in os.listdir('.'):
try:
img = Image.open(f)
except IOError:
pass
else:
idx = f.rfind('.')
if (idx == -1):
imgName = f
imgExtension = ''
else:
imgName = f[:idx]
imgExtension = f[idx:]
dirName = imgName + '_test'
try:
os.mkdir(dirName)
except OSError as e:
if (e.errno != 17):
raise e
n = 0
csv = file(dirName + '/' + imgName + '_trees.csv', 'w')
csv.write('Input file name,Output file name,Max tree depth,Min node size,Min fill ratio,Min pixel opacity,# of nodes,# of leaf nodes,Image pixels,Tree pixels,False positives,False negatives,Total error\n')
for d in depths:
quad_coll.MAX_DEPTH = d
for s in sizes:
quad_coll.MIN_SIZE = s
for r in ratios:
quad_coll.MIN_RATIO = r
for o in opacities:
quad_coll.MIN_OPACITY = o
tree = quad_coll.makeTree(img)
img2 = Image.new('RGBA', img.size)
quad_coll.drawTree(img2, tree, True)
draw = ImageDraw.Draw(img2)
draw.text((0, 0), 'max tree depth: %d' % d)
draw.text((0, 10), 'min node size: %d pixels' % s)
draw.text((0, 20), 'min node fill ratio: %f pixels' % r)
draw.text((0, 30), 'min filled pixel opactiy:%f alpha' % o)
outfilename = dirName + '/' + imgName + ('%04d' % n) + f[f.rfind('.'):]
ImageChops.composite(img2, img, img2).save(outfilename)
n = n + 1
error = quad_coll.calcError(img, tree)
csv.write('%s,%s,%d,%d,%f,%f,%d,%d,%d,%d,%d,%d,%d\n' % (\
f,\
outfilename,\
d,\
s,\
r,\
o,\
tree.getNumNodes(),\
tree.getNumLeafNodes(),\
error['imgPixels'],\
error['treePixels'],\
error['falsePositives'],\
error['falseNegatives'],\
error['falsePositives'] + error['falseNegatives']\
))
csv.close()