-
Notifications
You must be signed in to change notification settings - Fork 0
/
psColorize.py
executable file
·203 lines (167 loc) · 6.24 KB
/
psColorize.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
##########################
## Colorizing algorithm ##
## B = (2g+r)/3 ##
## G = (2r+i)/3 ##
## R = (i+z)/2 ##
##########################
import sys, getopt,os
import numpy as np
from itertools import product
from subprocess import run
from skimage.io import imread,imsave
import time
import warnings
warnings.filterwarnings("ignore", ".* is a low contrast image")
def colorize(depth,dirBase,outDir,txrange,tyrange,restart = False):
"""
Colorizes 4 TOASTED wavebands (griz) into rgb images.
Colorizing algorithm
B = (2g+r)/3
G = (2r+i)/3
R = (i+z)/2
Parameters
----------
depth: int
TOAST layer to be colorized
dirBase: string
waveband directory base, waveband directories should be of the form dirBase+'g'
outDir: string
directory in which the colorized tiles will be places
(within numbered layer directory)
txrange/tyrange: array
x and y ranges of tiles to colorize in the form [min,max]
restart: boolean (default False)
If true, tiles which already exist in the colorized directory will no be re-colorized
"""
for tx,ty in product(range(*txrange),range(*tyrange)):
pth = '/' + str(depth) + '/' + str(ty) + '/' + str(ty) + '_' + str(tx) + '.png'
# checking if the color file already exists
if os.path.isfile(outDir+pth):
if restart:
continue
else:
os.remove(outDir+pth)
# check we have all the files we need
if not (os.path.isfile(dirBase+'g'+pth) and os.path.isfile(dirBase+'r'+pth)
and os.path.isfile(dirBase+'i'+pth) and os.path.isfile(dirBase+'z'+pth)):
#and os.path.isfile(dirBase+'y'+pth)):
continue
g = imread(dirBase+'g'+pth)
r = imread(dirBase+'r'+pth)
i = imread(dirBase+'i'+pth)
z = imread(dirBase+'z'+pth)
#y = imread(dirBase+'y'+pth) (final colorizing does not use y)
G = g.astype(np.float64)
R = r.astype(np.float64)
I = i.astype(np.float64)
Z = z.astype(np.float64)
#Y = y.astype(np.float64)
# bad pixels will be sturated in only one band (hopefully)
maxDif = 175
PM = np.median(np.array([G,R,I,Z]), axis=0)
g[((G - PM) > maxDif) & (g == 255)] = 0
r[((R - PM) > maxDif) & (r == 255)] = 0
i[((I - PM) > maxDif) & (i == 255)] = 0
z[((Z - PM) > maxDif) & (z == 255)] = 0
#y[((Y - PM) > maxDif) & (y == 255)] = 0
rgb = np.dstack((np.mean(np.array([i,z]),axis=0).astype(np.uint8),
np.mean(np.array([r,r,i]),axis=0).astype(np.uint8),
np.mean(np.array([g,g,r]),axis=0).astype(np.uint8)))
direc, _ = os.path.split(outDir+pth)
if not os.path.exists(direc):
os.makedirs(direc)
try:
imsave(outDir+pth, rgb)
except:
print("Problem saving %s" % (outDir+pth))
def usage():
print("psColorize.py -b <base directory> -o <output directory> -d <depth> [-x <tile x range> -y <tile y range> -r]")
print("""
Colorizes 4 TOASTED wavebands (griz) into rgb images.
Colorizing algorithm
B = (2g+r)/3
G = (2r+i)/3
R = (i+z)/2
Parameters
----------
depth: int
TOAST layer to be colorized
dirBase: string
waveband directory base, waveband directories should be of the form dirBase+'g'
outDir: string
directory in which the colorized tiles will be places
(within numbered layer directory)
txrange/tyrange: array
x and y ranges of tiles to colorize in the form [min,max]
restart: boolean (default False)
If true, tiles which already exist in the colorized directory will no be re-colorized
""")
if __name__ == "__main__":
try:
opts, args = getopt.getopt(sys.argv[1:],"hb:o:d:x:y:r",["help","baseDir=","outDir","depth=","txrange=","tyrange=","restart"])
except getopt.GetoptError:
usage()
sys.exit(2)
dirBase = ''
outDir = '.'
depth = None
txRange = None
tyRange = None
restart = False
for opt, arg in opts:
if opt in ('-h','--help'):
usage()
sys.exit()
if opt in ('-b','--baseDir'):
dirBase = arg
if opt in ('-o','--outDir'):
outDir = arg
if opt in ('-d','--depth'):
try:
depth = int(arg)
except ValueError:
print("Depth must be an integer (base 10 please)")
sys.exit(2)
if opt in ('-x','--txrange'):
try:
txRange = [int(x) for x in arg.split(',')]
except ValueError:
print("Range must be of the form: min,max")
sys.exit(2)
if len(txRange) != 2:
print("Range must be of the form: min,max")
sys.exit(2)
if opt in ('-y','--tyrange'):
try:
tyRange = [int(x) for x in arg.split(',')]
except ValueError:
print("Range must be of the form: min,max")
sys.exit(2)
if len(tyRange) != 2:
print("Range must be of the form: min,max")
sys.exit(2)
if opt in ('-r','--restart'):
restart = True
if not (dirBase):
print("Directory containing grizy greyscale directories must be supplied.")
usage()
sys.exit(2)
if not depth:
print("Depth to be colorized must be supplied.")
usage()
sys.exit(2)
if not outDir:
outDir = dirBase + "color"
maxTileDim = 2**depth
if not txRange:
txRange = maxTileDim
if not tyRange:
tyRange = maxTileDim
if (depth > 8) and (txRange == maxTileDim) and (tyRange == maxTileDim):
print("You have requested colorization of all tiles at depth %d." % depth)
print("This may take a while, please be patient, or start with a smaller section.")
start = time.time()
colorize(depth,dirBase,outDir,txRange,tyRange,restart)
end = time.time()
print(end-start)