-
Notifications
You must be signed in to change notification settings - Fork 6
/
raw_process.py
146 lines (118 loc) · 4.8 KB
/
raw_process.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
import rawpy
import numpy as np
import math
import sys
""" Process RAW file into a image file.
Example usage:
raw = read("sample.ARW")
rgb = process(raw)
write(rgb, "output.ARW")
"""
def read(filename):
"""
Read RAW data from specified file. Currently supported formats are
ARW (Sony RAW format)
:param filename: path to the target RAW file
"""
return rawpy.imread(filename)
def process(raw, color_matrix=[1024, 0, 0, 0, 1024, 0, 0, 0, 1024]):
"""
This processes RAW data that was read by read() method.
Must be called after read() operation. No error is checked.
"""
raw_array = get_raw_array(raw)
blc_raw = black_level_correction(raw, raw_array)
dms_img = preview_demosaic(raw, blc_raw)
img_wb = white_balance(raw, dms_img)
img_ccm = color_correction_matrix(img_wb, color_matrix)
img_gamma = gamma_correction(img_ccm)
return img_gamma
def write(rgb_image, output_filename):
"""
Write the processed RGB image to a specified file as PNG format.
Thsi must be called after process(). No error is checked.
:param output_filename: path to the output file. Extension must be png.
"""
import imageio
outimg = rgb_image.copy()
outimg[outimg < 0] = 0
outimg = outimg / outimg.max() * 255
imageio.imwrite(output_filename, outimg.astype('uint8'))
def get_raw_array(raw):
h, w = raw.sizes.raw_height, raw.sizes.raw_width
raw_array = np.array(raw.raw_image).reshape((h, w)).astype('float')
return raw_array
def black_level_correction(raw, raw_array):
blc = raw.black_level_per_channel
bayer_pattern = raw.raw_pattern
blc_raw = raw_array.copy()
h, w = raw.sizes.raw_height, raw.sizes.raw_width
for y in range(0, h, 2):
for x in range(0, w, 2):
blc_raw[y + 0, x + 0] -= blc[bayer_pattern[0, 0]]
blc_raw[y + 0, x + 1] -= blc[bayer_pattern[0, 1]]
blc_raw[y + 1, x + 0] -= blc[bayer_pattern[1, 0]]
blc_raw[y + 1, x + 1] -= blc[bayer_pattern[1, 1]]
return blc_raw
def preview_demosaic(raw, raw_array):
bayer_pattern = raw.raw_pattern
h, w = raw.sizes.raw_height, raw.sizes.raw_width
dms_img = np.zeros((h // 2, w // 2, 3))
for y in range(0, h, 2):
for x in range(0, w, 2):
colors = [0, 0, 0, 0]
colors[bayer_pattern[0, 0]] += raw_array[y + 0, x + 0]
colors[bayer_pattern[0, 1]] += raw_array[y + 0, x + 1]
colors[bayer_pattern[1, 0]] += raw_array[y + 1, x + 0]
colors[bayer_pattern[1, 1]] += raw_array[y + 1, x + 1]
dms_img[y // 2, x // 2, 0] = colors[0]
dms_img[y // 2, x // 2, 1] = (colors[1] + colors[3]) / 2
dms_img[y // 2, x // 2, 2] = colors[2]
return dms_img
def white_balance(raw, rgb_array):
wb = np.array(raw.camera_whitebalance)
img_wb = np.zeros_like(rgb_array).reshape((-1, 3))
for index, pixel in enumerate(rgb_array.reshape(-1, 3)):
pixel = pixel * wb[:3] / 1024
img_wb[index] = pixel
return img_wb.reshape(rgb_array.shape)
def color_correction_matrix(rgb_array, color_matrix):
img_ccm = np.zeros_like(rgb_array).reshape((-1, 3))
ccm = np.array(color_matrix).reshape((3, 3))
for index, pixel in enumerate(rgb_array.reshape((-1, 3))):
pixel = np.dot(ccm, pixel)
img_ccm[index] = pixel
return img_ccm.reshape(rgb_array.shape)
def gamma_correction(rgb_array):
img_gamma = rgb_array.copy().flatten()
img_gamma[img_gamma < 0] = 0
img_gamma = img_gamma / img_gamma.max()
for index, val in enumerate(img_gamma):
img_gamma[index] = math.pow(val, 1 / 2.4)
return img_gamma.reshape(rgb_array.shape)
def main(argv):
if (len(argv) < 2):
print("Usage: {} input_filename [output_filename] [color_matrix]".format(argv[0]))
print("\tDefault output_filename is output.png")
print("\tDefault matrix is identity matrix ([1024, 0, 0, 0, 1024, 0, 0, 0, 1024]")
print("\tExample: python3 {} sample.ARW sample.png \"1141, -205, 88, -52, 1229, -154, 70, -225, 1179\"".format(argv[0]))
print("\tSupported RAW format is ARW (Sony RAW)")
return
filename = argv[1]
output_filename = "output.png"
color_matrix = [1024, 0, 0, 0, 1024, 0, 0, 0, 1024]
if len(argv) > 2:
output_filename = argv[2]
if len(argv) > 3:
color_matrix = [int(value) for value in (argv[3]).split(',')]
color_matrix = [1024, 0, 0, 0, 1024, 0, 0, 0, 1024]
raw = read(filename)
raw_array = get_raw_array(raw)
blc_raw = black_level_correction(raw, raw_array)
dms_img = preview_demosaic(raw, blc_raw)
img_wb = white_balance(raw, dms_img)
img_ccm = color_correction_matrix(img_wb, color_matrix)
rgb_image = gamma_correction(img_ccm)
write(rgb_image, output_filename)
if __name__ == "__main__":
main(sys.argv)