-
Notifications
You must be signed in to change notification settings - Fork 0
/
genPoints.py
153 lines (117 loc) · 4.46 KB
/
genPoints.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import click
import cv2
import os
import decimal
import numpy as np
import PoissonDisk as pd
from PIL import Image, ImageDraw
def getUniqueFileName(filename):
i = 1
fn = filename.split('.')
if os.path.isfile(filename):
while(1):
newFilename = fn[0]+str(i)+'.'+fn[1]
if os.path.isfile(newFilename):
i += 1
else:
break
filename = newFilename
return filename
def drange(x, y, jump):
while x < y:
yield float(x)
x = decimal.Decimal(str(x)) + decimal.Decimal(jump)
def lerpColor(c1, c2, t):
return (c1 + t * (c2 - c1)).astype(int)
def lerp(n1, n2, t):
return n1 + t * (n2 - n1)
def getLevels(image, nmbLevels):
img = Image.open(image)
img = img.quantize(nmbLevels)
tmpLevels = img.getpalette()
levels = []
for i in range(0, len(tmpLevels), 3):
levels.append(tmpLevels[i])
if (len(levels) >= nmbLevels): break
return levels
def getColorGrad(max, min, granularity):
colors = []
foo = 1/(granularity-1)
for i in drange(0.0, 1.0 + foo, foo):
colors.append(tuple(lerpColor(max, min, i)))
return colors
def getRadiusGrad(max, min, granularity):
radius = []
foo = 1/(granularity-1)
for i in drange(0, 1 + foo, foo):
radius.append(round(lerp(max, min, i)))
return radius
def filterPoints(points, sizeList, nmbLevels, mask_filename):
filteredPoints = []
mask = cv2.imread(mask_filename)
levels = getLevels(mask_filename, nmbLevels)
for p in points:
coord = int(p[1]), int(p[0])
brightness = mask[coord][0]
i = 0
addPoint = False
for l in levels:
if (brightness >= l):
i = levels.index(l)
addPoint = True
break
if (addPoint):
filteredPoints.append((p[0], p[1], sizeList[i]))
return filteredPoints
def drawPointsToImage(image_filename, points, radiusList, colorList, nmbLevels):
img = Image.open(image_filename)
output_image = Image.new("RGB", img.size)
draw = ImageDraw.Draw(output_image)
mask = cv2.imread(image_filename)
levels = getLevels(image_filename, nmbLevels)
for p in points:
coord = int(p[1]), int(p[0])
i = 0
drawObj = False
brightness = mask[coord][0]
for l in levels:
if (brightness >= l):
i = levels.index(l)
drawObj = True
break
if (drawObj):
draw.ellipse((int(p[0]) - radiusList[i],
int(p[1]) - radiusList[i],
int(p[0]) + radiusList[i],
int(p[1]) + radiusList[i]),
fill=colorList[i],
outline =colorList[i])
return output_image
def savePointsToFile(points, fileName):
file = open(fileName, "w")
# xpos ypos size
for p in points:
file.write(str(p[0])+" "+str(p[1])+" "+str(p[2])+"\n")
@click.command()
@click.option('-o', 'output_filename', default='output.txt', help='output file')
@click.option('-c', 'colors', default=((0,)*3,(255,)*3), type=(int, int, int), multiple=True, help='colors for points')
@click.option('-r', 'radii', nargs=2, default=(5, 1), type=click.INT, help='max/min radii')
@click.option('-sampleRadius', 'sampleRadius', default=10, type=click.INT, help='sample radius for poisson')
@click.option('-rejectionSamples', 'rejectionSamples', default=10, type=click.INT, help='number of rejection samples for poisson')
@click.argument('mask_filename', type=click.Path(exists=True))
@click.argument('levels', type=click.INT)
def genPoints(mask_filename, output_filename, sampleRadius, rejectionSamples, levels, colors, radii):
mask = cv2.imread(mask_filename)
sampleRegionSize = np.array([mask.shape[0], mask.shape[1]])
grassPoints = pd.PoissonDiskSampling(sampleRadius, sampleRegionSize, rejectionSamples)
maxc, minc = colors
maxr, minr= radii
colorList = getColorGrad(np.array(maxc), np.array(minc), levels)
radiiList = getRadiusGrad(maxr, minr, levels)
filteredGrassPoints = filterPoints(grassPoints, radiiList, levels, mask_filename)
savePointsToFile(filteredGrassPoints, getUniqueFileName(output_filename))
colorPoints = drawPointsToImage(mask_filename, filteredGrassPoints, radiiList, colorList, levels)
colorPoints.show()
colorPoints.save(getUniqueFileName('img.png'))
if __name__ == '__main__':
genPoints()