-
Notifications
You must be signed in to change notification settings - Fork 0
/
mix_channels
executable file
·167 lines (120 loc) · 4.62 KB
/
mix_channels
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
#!/usr/bin/env 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 sys
import numpy as np
import cv2
import tifffile
import argparse
from astro_utils import ExpDiff, poly_bg
def apply_gamma(img, gamma):
lut = np.fromiter( ( (x / 65535.0)**gamma * 65535.0 for x in range(65536)), dtype=np.uint16 )
return np.take(lut, np.array(img, dtype=np.int64))
parser = argparse.ArgumentParser()
parser.add_argument("outfile",
help="output tiff file")
parser.add_argument("infile",
help="input tiff file or R channel")
parser.add_argument("infile_g", nargs ='?',
help="input tiff file - G channel")
parser.add_argument("infile_b", nargs ='?',
help="input tiff file - B channel")
parser.add_argument("--matrix", type=float, nargs=9,
help="camera matrix")
parser.add_argument("--wb", type=float, nargs=3,
help="white balance")
parser.add_argument('--zero', type=int, default = 0,
help='zero level')
parser.add_argument("--ingamma", type=float, default=1.0,
help="input file gamma")
parser.add_argument("--outgamma", type=float, default=1.0,
help="output file gamma")
args = parser.parse_args()
img16 = tifffile.imread(args.infile)
transp = None
if args.infile_g is not None:
if args.infile_b is None:
print("missing b")
sys.exit(1)
img16_g = tifffile.imread(args.infile_g)
img16_b = tifffile.imread(args.infile_b)
img16 = np.atleast_3d(img16)
img16_g = np.atleast_3d(img16_g)
img16_b = np.atleast_3d(img16_b)
if img16.shape[2] > 1:
transp = img16[:, :, 1]
if img16_g.shape[2] > 1:
transp = img16_g[:, :, 1]
if img16_b.shape[2] > 1:
transp = img16_b[:, :, 1]
img16 = img16[:, :, 0]
img16_g = img16_g[:, :, 0]
img16_b = img16_b[:, :, 0]
if args.ingamma != 1.0 :
img16 = apply_gamma(img16, args.ingamma)
img16_g = apply_gamma(img16_g, args.ingamma)
img16_b = apply_gamma(img16_b, args.ingamma)
img = np.empty((img16.shape[0], img16.shape[1], 3), dtype = np.float32)
img[:, :, 0] = img16
img[:, :, 1] = img16_g
img[:, :, 2] = img16_b
else:
if np.atleast_3d(img16).shape[2] > 3:
transp = img16[:, :, 3]
if args.ingamma != 1.0 :
img16[:, :, 0:3] = apply_gamma(img16[:, :, 0:3], args.ingamma)
img = np.array(img16[:, :, 0:3], dtype = np.float32)
#zero_r = np.min(cv2.blur(img[:,:,0], (15,15)))
#zero_g = np.min(cv2.blur(img[:,:,1], (15,15)))
#zero_b = np.min(cv2.blur(img[:,:,2], (15,15)))
#zero = np.min([zero_r, zero_g, zero_b, args.zero]) - 1
#print "zero", zero, zero_r, zero_g, zero_b
#img[:,:,0] -= zero_r
#img[:,:,1] -= zero_g
#img[:,:,2] -= zero_b
#wb = np.array([2.334676, 1.000000, 1.343017])
#for i in range(0,3):
# img[:, :, i] = cv2.multiply(img[:, :, i], wb[i])
#if args.matrix is not None:
cam_xyz = np.array([0.6844, -0.0996, -0.0856, -0.3876, 1.1761, 0.2396, -0.0593, 0.1772, 0.6198]).reshape((3, 3))
xyz_rgb = np.array([0.412453, 0.357580, 0.180423, 0.212671, 0.715160, 0.072169, 0.019334, 0.119193, 0.950227]).reshape((3, 3));
cam_rgb = np.matrix(cam_xyz) * np.matrix(xyz_rgb)
print(cam_rgb)
rgb_cam = np.array(cam_rgb.I).T
print(rgb_cam)
#m = np.array([1,0,0, 0,1,0, 0,0,1]).reshape((3, 3))
#m = np.array(args.matrix).reshape((3, 3))
#img[:, :, 0:3] = np.dot(img[:, :, 0:3], rgb_cam)
if True:
bg = poly_bg(img, order = 1, scale = 1, kappa = 1, it = 15)
img -= bg
if True:
ed_r = ExpDiff(img[:,:,0], 0, 1, 5000, bg_dist = 2, name=0)
ed_g = ExpDiff(img[:,:,1], 0, 1, 5000, bg_dist = 2, name=1)
ed_b = ExpDiff(img[:,:,2], 0, 1, 5000, bg_dist = 2, name=2)
wb_r = ed_r.diff(ed_g)
wb_b = ed_b.diff(ed_g)
# wb_r = (1.5, -8)
# wb_b = (1.3, 0)
img[:, :, 0] = img[:, :, 0] * wb_r[0] + wb_r[1]
img[:, :, 2] = img[:, :, 2] * wb_b[0] + wb_b[1]
if False:
m, sigma = poly_bg(img[:, :, 1], order = 1, scale = 1, it = 3, get_mask = True)
print("sigma", sigma)
#filt = cv2.ximgproc.createGuidedFilter(img[:, :, 1], 12, sigma ** 2)
filt = cv2.ximgproc.createDTFilter(img[:, :, 1], 20, sigma * 2)
img = filt.filter(img)
img += args.zero
if transp is not None:
img16 = np.empty((img.shape[0], img.shape[1], 4), dtype=np.uint16)
img16[:, :, 3] = transp
else:
img16 = np.empty((img.shape[0], img.shape[1], 3), dtype=np.uint16)
img16[:, :, 0:3] = cv2.add(img, 0, dtype = cv2.CV_16UC3)
if args.outgamma != 1.0 :
img16[:, :, 0:3] = apply_gamma(img16[:, :, 0:3], args.outgamma)
tifffile.imsave(args.outfile, img16)