-
Notifications
You must be signed in to change notification settings - Fork 0
/
darksub
executable file
·314 lines (238 loc) · 8.47 KB
/
darksub
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/python3
# Copyright (C) 2017 Vladimir Nadvornik
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
import numpy as np
import cv2
import tifffile
import argparse
import sys
from astro_utils import rawread, normalize, debayer, hp_filt_bayer, hp_filt_rgb, hp_filt_mono, poly_bg, fit_images, combine_images
from astropy.io import fits
def apply_flat(img, flats, coefs, log = False, mono = False):
if log:
flat = np.ones(img.shape, dtype=np.float32)
for i, f in enumerate(flats):
f = np.array(f, dtype=np.float32)
flat *= f ** coefs[i]
else:
flat = np.zeros(img.shape, dtype=np.float32)
for i, f in enumerate(flats):
f = np.array(f, dtype=np.float32)
flat += f * coefs[i]
flat[np.where(flat <= 0)] = 1
if mono:
flat_bl = cv2.blur(flat, (100, 100))
maxf = np.amax(flat_bl)
mulmat = maxf / flat
else:
flat_bl_b = cv2.blur(flat[0::2, 0::2], (100, 100))
flat_bl_r = cv2.blur(flat[1::2, 1::2], (100, 100))
flat_bl_g1 = cv2.blur(flat[0::2, 1::2], (100, 100))
flat_bl_g2 = cv2.blur(flat[1::2, 0::2], (100, 100))
max_b = np.amax(flat_bl_b)
max_g = (np.amax(flat_bl_g1) + np.amax(flat_bl_g2)) / 2.0
max_r = np.amax(flat_bl_r)
mulmat = np.empty_like(flat)
mulmat[0::2, 0::2] = max_b / flat[0::2, 0::2]
mulmat[1::2, 1::2] = max_r / flat[1::2, 1::2]
mulmat[0::2, 1::2] = max_g / flat[0::2, 1::2]
mulmat[1::2, 0::2] = max_g / flat[1::2, 0::2]
return cv2.multiply(img, mulmat, dtype=cv2.CV_32FC1)
def hotpix_find(img):
img_hp = hp_filt(img, size = 3)
mean, stddev = cv2.meanStdDev(img_hp)
print(mean, stddev)
hotpix_cnt = np.zeros_like(img, dtype=np.uint8)
print(hotpix_cnt.shape, img.shape)
hotpix_cnt[np.where(np.abs(img_hp) > stddev * 10)] += 100
cv2.imwrite("hotpix.tif", hotpix_cnt)
def gradient_dif_filter2(dif, w, r):
h, w = dif.shape
resize_w = int((w + r - 1) / r)
resize_h = int((w + r - 1) / r)
res = np.empty((resize_h, resize_w), dtype = dif.dtype)
for i in range(0, resize_h):
y0 = np.clip(int((i - 1) * h / resize_h), 0, h - 1)
y1 = np.clip(int((i + 2) * h / resize_h), 0, h - 1)
for j in range(0, resize_w):
x0 = np.clip(int((j - 1) * w / resize_w), 0, w - 1)
x1 = np.clip(int((j + 2) * w / resize_w), 0, w - 1)
res[i, j] = np.median(dif[y0:y1, x0:x1])
res = cv2.resize(res, (w, h), interpolation=cv2.INTER_LINEAR)
res = cv2.blur(res, (r, r))
res = cv2.blur(res, (r, r))
res = cv2.blur(res, (r, r))
return res
parser = argparse.ArgumentParser()
parser.add_argument("infile",
help="input tiff file")
parser.add_argument("--outfile",
help="output tiff file")
parser.add_argument("--outbayer",
help="output tiff file before debayer")
parser.add_argument("--outrgb", nargs=3,
help="output tiff files for R, G, B")
parser.add_argument("--flat", nargs='*',
help="flat tif")
parser.add_argument("--dark", nargs='*',
help="dark tif")
parser.add_argument('--poly-bg', type=int, default = 0,
help='order of background polynom')
parser.add_argument('--poly-bg-kappa', type=float, default = 2,
help='kappa of background clip')
parser.add_argument('--median-bg', type=int, default = 0,
help='size of median filter')
parser.add_argument('--scale', type=int, default = 4,
help='downscale')
parser.add_argument('--erode', type=int, default = 0,
help='erode')
parser.add_argument('--iter', type=int, default = 10,
help='iterations')
parser.add_argument('--min', type=int,
help='min value')
parser.add_argument('--max', type=int,
help='max value')
parser.add_argument('--zero', type=int, default = 0,
help='output zero level')
parser.add_argument("--dark-bg", action='store_true', default=False)
parser.add_argument("--final-dark", action='store_true', default=False)
parser.add_argument("--flat-bg", action='store_true', default=False)
parser.add_argument("--mono", action='store_true', default=False)
args = parser.parse_args()
if args.outrgb is None and args.outfile is None and args.outbayer is None:
print("missing output")
sys.exit(1)
minval = args.min
maxval = args.max
np.set_printoptions(edgeitems = 5)
img, minval, maxval = rawread(args.infile)
if maxval is None:
maxval = np.amax(img)
dark = []
if args.dark:
for d in args.dark:
dark.append(rawread(d)[0])
if len(dark) > 0 and minval is None:
minval = int(np.median(dark))
if minval is None:
minval = int(np.amin(img))
print("min: %d, max:%d" % (minval, maxval))
hp_filt_func = hp_filt_bayer
if args.mono:
hp_filt_func = hp_filt_mono
#print img
if len(dark) > 0:
if args.final_dark:
final_dark = dark[0]
else:
img_hp = hp_filt_func(img)
dark_hp = []
for d in dark:
dark_hp.append(hp_filt_func(d))
mean, stddev = cv2.meanStdDev(img_hp)
init_weight = 1 / (1 + (img_hp / stddev)**2)
coefs, n = fit_images(dark_hp, img_hp, 10, init_weight=init_weight)
final_dark = combine_images(dark, coefs, [minval] * len(dark))
print("dark", np.median(final_dark))
img = cv2.subtract(img, final_dark, dtype = cv2.CV_32FC1)
#hotpix_find(final_dark)
if args.outbayer is not None:
img = np.clip(img, 0, 65535)
img = np.array(img, dtype=np.uint16)
tifffile.imsave(args.outbayer, img)
exit(0)
img = cv2.subtract(img, minval)
flat = []
if args.flat:
for d in args.flat:
f = rawread(d)[0]
if f.ndim == 3:
f = f[:,:,0]
# FIXME f = cv2.subtract(f, minval)
flat.append(f)
print("flat shape", f.shape)
print("img shape", img.shape)
if len(flat) > 0:
flat = flat[0:1]
log = False
if len(flat) == 1:
coefs = [1.0]
else:
img_f = cv2.pyrDown(img)
flat_f = []
for f in flat:
ff = cv2.pyrDown(np.array(f, dtype = np.float32))
flat_f.append(ff)
mask, sigma = poly_bg(img_f, order = 3, scale = 1, erode = args.erode, it = args.iter, darkframes = flat_f, get_mask = True)
tifffile.imsave("mask.tif", mask)
if log:
flat_f.append(np.ones_like(img_f) * 2)
flat_f = np.log(np.clip(np.array(flat_f), 1e-12, 65536))
img_f = np.log(np.clip(img_f, 1e-12, 65536))
# else:
# flat_f.append(np.ones_like(img_f))
# flat.append(np.ones_like(img))
coefs, n = fit_images(flat_f, img_f, 10, kappa = 3, mask = mask)
print(coefs)
img = apply_flat(img, flat, coefs, log=log, mono=args.mono)
print(img.shape)
#if scale
#img = cv2.multiply(img, 65535.0 / float(maxval - minval), dtype = cv2.CV_16UC1)
#col = cv2.cvtColor(img, cv2.COLOR_BAYER_BG2RGB)
if args.mono:
col = np.atleast_3d(img)
n_channels = 1
res_type = cv2.CV_16UC1
else:
col = debayer(img, filt = True)
n_channels = 3
res_type = cv2.CV_16UC3
if args.median_bg > 0:
bg = np.empty_like(col, dtype=np.float32)
for i in range(0, n_channels):
bg[:, :, i] = gradient_dif_filter2(np.array(col[:, :, i], dtype=np.float32), 0, args.median_bg)
bg -= args.zero
col = cv2.subtract(col, bg, dtype = res_type)
elif args.poly_bg > 0:
bg_img = []
if len(dark) > 0 and args.dark_bg:
bg_img = bg_img + dark
if len(flat) > 0 and args.flat_bg:
bg_img = bg_img + flat
for f in flat:
bg_img.append(cv2.divide(1.0, f, dtype=cv2.CV_64F))
YX = np.indices(f.shape, dtype = np.float64) / 1000.0
bg_img.append(cv2.divide(YX[0], f, dtype=cv2.CV_64F))
bg_img.append(cv2.divide(YX[1], f, dtype=cv2.CV_64F))
bg_img.append(cv2.multiply(YX[0], f, dtype=cv2.CV_64F))
bg_img.append(cv2.multiply(YX[1], f, dtype=cv2.CV_64F))
for i in range(len(bg_img)):
df = np.array(bg_img[i], dtype=np.float32)
df = cv2.medianBlur(df,5)
df = cv2.blur(df, (50,50))
df = cv2.blur(df, (50,50))
df = cv2.blur(df, (50,50))
bg_img[i] = df
bg = poly_bg(col, order = args.poly_bg, scale = args.scale, erode = args.erode, it = args.iter, kappa = args.poly_bg_kappa, darkframes = bg_img)
bg -= args.zero
col = cv2.subtract(col, bg, dtype = res_type)
else:
if args.mono:
col = cv2.add(col, args.zero, dtype = cv2.CV_16UC1)
else:
col = cv2.add(col, (args.zero, args.zero, args.zero, 0), dtype = cv2.CV_16UC3)
if args.outfile is not None:
if args.outfile.endswith('.fits'):
hdulist = fits.open(args.infile)
hdulist[0].data = col
hdulist.writeto(args.outfile, overwrite=True)
sys.exit(0)
else:
tifffile.imsave(args.outfile, col)
if args.outrgb is not None:
for i in range(0, 3):
tifffile.imsave(args.outrgb[i], col[:, :, i])