# 色调映射

## Photographic 演算法[6]

### 步驟一　整個場景的亮度改變

${\displaystyle {\overline {L_{w}}}=\exp {({\frac {(\sum _{x,y}\log {(\delta +L_{w}(x,y))}}{N}})}}$

${\displaystyle \delta }$: 一個極小值，避免在算黑色像素的時候會遇到${\displaystyle \log =0}$的情況 算出場景中的關鍵值後，我們要將此關鍵值重新調整到中間值${\displaystyle \alpha }$，調整方法為

${\displaystyle L(x,y)={\frac {\alpha }{\overline {L_{w}}}}L_{w}(x,y)}$

${\displaystyle L_{d}(x,y)={\frac {L(x,y)(1+{\frac {L(x,y)}{{L_{white}}^{2}}})}{1+L(x,y)}}}$

### 步驟二 　局部區塊的亮度

${\displaystyle V_{i}(x,y,s)=L(x,y,)\otimes R_{i}(x,y,s)}$
${\displaystyle R_{i}(x,y,s)={\frac {1}{\pi (\alpha _{i}s)^{2}}}\exp {-{\frac {x^{2}+y^{2}}{(\alpha _{i}s)^{2}}}}}$

${\displaystyle V(x,y,s)={\frac {V_{1}(x,y,s)-V_{2}(x,y,s)}{2^{\phi }a/s^{2}+V_{1}(x,y,s)}}}$

${\displaystyle L_{d}(x,y)={\frac {L(x,y)}{1+V_{1}(x,y,s_{m}(x,y))}}}$

## 實作Photographic 演算法

1 function rgb = tonemap_p( hdr )
2     //步驟1,調整整個場景的亮度
3     a = 0.6;//關鍵值設定
4     epsilon = 0.05;
5
6     //利用rgb計算照片亮度
7     lum_w = 0.27 * hdr(:,:,1) + 0.67 * hdr(:,:,2) + 0.06 * hdr(:,:,3);
8     image_size = size( hdr );
9     [height, width] = size(image_size);
10
11     //計算L_w_bar(x,y)
12     sum_all = 0;
13     for i = 1: height
14         for j = 1: width
15             sum_all = sum_all + log( 0.00000001+ lum_w(i, j) );
16         end
17     end
18     lum_white = 1e20;
19     lum_w_bar = double( exp (sum_all / N) );
20
21     //計算L_d(x,y)
22     lum = a / lum_w_bar * lum_w;
23     for i = 1: height
24         for j = 1: width
25             lum_d(i, j) = lum(i, j) * ( 1 + lum(i, j) / lum_white / lum_white ) / (1 + lum(i, j) );
26         end
27     end
28
29     //步驟二：加亮與加深(Dodging & Burning)
30     phi = 8;
31     //計算兩個不同變異數大小的高斯函數
32     for i = 1: height
33         for j = 1: width
34             for k = 1: 8
35                 s = 1 * 1.6 ^ k;
36                 alpha1 = 0.35;
37                 R1(i, j, k) = exp( -( i ^ 2 + j ^ 2) / ( alpha1 ) ^ 2 ) / ( pi * ( alpha1 * s ) ^ 2 );
38                 alpha2 = 0.35 * 1.6;
39                 R2(i, j, k) = exp( -( i ^ 2 + j ^ 2) / ( alpha2 ) ^ 2 ) / ( pi * ( alpha2 * s ) ^ 2 );
40             end
41         end
42     end
43     //將亮度與高斯函數做卷積
44     for k = 1: 8
45         V1(:,:,k) = conv2( lum_w, R1(:,:,k));
46         V2(:,:,k) = conv2( lum_w, R2(:,:,k));
47     end
48
49     //計算函數V(x, y, s)
50     for i = 1: height
51         for j = 1: width
52             for k = 1:8
53                 V(i, j, k) = ( V1(i, j, k) - V2(i, j, k)) / (2 ^ phi * a / (k ^ 2) + V1(i, j, k));
54             end
55         end
56     end
57
58     //找尋符合V小於臨界值epsilon的scale
59     for i = 1: height
60         for j = 1: width
61             for k = 1:8
62                 if abs(V(i, j, k)) < epsilon
63                     V1_new(i, j) = V(i, j, k);
64                 end
65             end
66         end
67     end
68
69     //計算新的亮度
70     for i = 1: height
71         for j = 1: width
72             lum_d2(i, j) = lum_w(i, j) / (1 + V1_new(i, j));
73         end
74     end
75
76     //將rgb影像轉到hsv的系統後,改變亮度大小,最後輸出成色調映射後的結果
77     hvs = rgb2hsv( hdr );
78     hvs(:,:,3) = lum_d2;
79     rgb = hsv2rgb( hsv);
80 end

## 参考文献

1. ^ Kate Devlin, Alan Chalmers, Alexander Wilkie, Werner Purgathofer. "STAR Report on Tone Reproduction and Physically Based Spectral Rendering" in Eurographics 2002. DOI: 10.1145/1073204.1073242
2. ^ Raanan Fattal, Dani Lischinski, Michael Werman. "梯度域高动态范围压缩"
3. ^ Rafal Mantiuk, Karol Myszkowski, Hans-Peter Seidel. "高动态范围图像对比度处理的感知架构"
4. ^ Alan Gilchrist. "An Anchoring Theory of Lightness Perception".
5. ^ Grzegorz Krawczyk, Karol Myszkowski, Hans-Peter Seidel. "高动态范围图像色调重现中的亮度感知"
6. ^ Reinhard, Erik, et al. "Photographic tone reproduction for digital images." ACM transactions on graphics (TOG) 21.3 (2002): 267-276.