为了正确可视化RAW图像,需要做好:白平衡、提亮以及色彩映射。
1 import numpy as np 2 import struct 3 from PIL import Image 4 import rawpy 5 import glob 6 import os 7 8 9 def conv(v): 10 s = 0 11 for i in range(len(v)): 12 s += i * v[i] 13 v[i] = s 14 return v 15 16 17 def diff(v): 18 n = len(v) 19 v_diff = np.zeros([len(v)]) 20 for i in range(n-1): 21 v_diff[n-1-i] = v[n-1-i] - v[n-1-i-1] 22 return v_diff 23 24 25 def gray_ps(rgb): 26 return np.power(np.power(rgb[:,:,0], 2.2) * 0.2973 + np.power(rgb[:,:,1], 2.2) * 0.6274 + np.power(rgb[:,:,2], 2.2) * 0.0753, 1/2.2) + 1e-7 27 28 29 def HDR(x, curve_ratio): 30 gray_scale = np.expand_dims(gray_ps(x), axis=-1) 31 gray_scale_new = np.power(gray_scale, curve_ratio) 32 return np.minimum(x * gray_scale_new / gray_scale, 1.0) 33 34 35 def gray_world_balance(x): 36 mean_ = np.maximum(np.mean(x, axis=(0, 1)), 1e-6) 37 ratio_ = mean_.mean() / mean_ 38 return np.minimum(x * ratio_, 1.0) 39 40 41 def show_bbf(path): 42 print('====== %s =====' % path) 43 max_level = 1023 44 black_level = 64 45 height = 3024 46 width = 4032 47 if path.endswith('dng') or path.endswith('DNG'): 48 raw = rawpy.imread(path) 49 im = raw.raw_image_visible.astype(np.float32) 50 res_path = str.replace(path, '.dng', '.jpg') 51 elif path.endswith('raw'<