1
+ #include " function.h"
2
+
3
+ void simple_color_balance (float ** input_img, float ** out_img, int rows, int cols) {
4
+ float max_value = 0 ;
5
+ float min_value = 256 ;
6
+ for (int i = 0 ; i < rows; i++) {
7
+ for (int j = 0 ; j < cols; j++) {
8
+ max_value = max (max_value,input_img[i][j]);
9
+ min_value = min (min_value, input_img[i][j]);
10
+ }
11
+ }
12
+ if (max_value <= min_value) {
13
+ for (int i = 0 ; i < rows; i++) {
14
+ for (int j = 0 ; j < cols; j++) {
15
+ out_img[i][j] = max_value;
16
+ }
17
+ }
18
+ }
19
+ else {
20
+ float scale = 255.0 / (max_value - min_value);
21
+ for (int i = 0 ; i < rows; i++) {
22
+ for (int j = 0 ; j < cols; j++) {
23
+ if (input_img[i][j] < min_value) {
24
+ out_img[i][j] = 0 ;
25
+ }
26
+ else if (input_img[i][j] > max_value) {
27
+ out_img[i][j] = 255 ;
28
+ }
29
+ else {
30
+ out_img[i][j] = scale * (input_img[i][j] - min_value);
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ int HDR (cv::Mat input_img, cv::Mat out_img) {
38
+ int rows = input_img.rows ;
39
+ int cols = input_img.cols ;
40
+ // DouImg
41
+ float ***DouImg;
42
+ DouImg = new float **[rows];
43
+ for (int i = 0 ; i < rows; i++) {
44
+ DouImg[i] = new float *[cols];
45
+ for (int j = 0 ; j < cols; j++) {
46
+ DouImg[i][j] = new float [3 ];
47
+ }
48
+ }
49
+ for (int i = 0 ; i < rows; i++) {
50
+ for (int j = 0 ; j < cols; j++) {
51
+ DouImg[i][j][0 ] = (float )input_img.at <Vec3b>(i, j)[0 ];
52
+ DouImg[i][j][1 ] = (float )input_img.at <Vec3b>(i, j)[1 ];
53
+ DouImg[i][j][2 ] = (float )input_img.at <Vec3b>(i, j)[2 ];
54
+ }
55
+ }
56
+ // Lw
57
+ float **Lw;
58
+ Lw = new float *[rows];
59
+ for (int i = 0 ; i < rows; i++) {
60
+ Lw[i] = new float [cols];
61
+ }
62
+ for (int i = 0 ; i < rows; i++) {
63
+ for (int j = 0 ; j < cols; j++) {
64
+ Lw[i][j] = 0 ;
65
+ }
66
+ }
67
+ // B
68
+ float **B;
69
+ B = new float *[rows];
70
+ for (int i = 0 ; i < rows; i++) {
71
+ B[i] = new float [cols];
72
+ }
73
+ for (int i = 0 ; i < rows; i++) {
74
+ for (int j = 0 ; j < cols; j++) {
75
+ B[i][j] = (float )input_img.at <Vec3b>(i, j)[0 ];
76
+ }
77
+ }
78
+ // G
79
+ float **G;
80
+ G = new float *[rows];
81
+ for (int i = 0 ; i < rows; i++) {
82
+ G[i] = new float [cols];
83
+ }
84
+ for (int i = 0 ; i < rows; i++) {
85
+ for (int j = 0 ; j < cols; j++) {
86
+ G[i][j] = (float )input_img.at <Vec3b>(i, j)[1 ];
87
+ }
88
+ }
89
+ // R
90
+ float **R;
91
+ R = new float *[rows];
92
+ for (int i = 0 ; i < rows; i++) {
93
+ R[i] = new float [cols];
94
+ }
95
+ for (int i = 0 ; i < rows; i++) {
96
+ for (int j = 0 ; j < cols; j++) {
97
+ R[i][j] = (float )input_img.at <Vec3b>(i, j)[2 ];
98
+ }
99
+ }
100
+ // Lwmax
101
+ float Lwmax = 0.0 ;
102
+ // Lw
103
+ for (int i = 0 ; i < rows; i++) {
104
+ for (int j = 0 ; j < cols; j++) {
105
+ Lw[i][j] = 0.299 * R[i][j] + 0.587 * G[i][j] + 0.114 * B[i][j];
106
+ if (Lw[i][j] == 0 ) {
107
+ Lw[i][j] = 1 ;
108
+ }
109
+ Lwmax = max (Lw[i][j], Lwmax);
110
+ }
111
+ }
112
+ // Lw_sum
113
+ float Lw_sum = 0 ;
114
+ // log_Lw
115
+ float **log_Lw;
116
+ log_Lw = new float *[rows];
117
+ for (int i = 0 ; i < rows; i++) {
118
+ log_Lw[i] = new float [cols];
119
+ }
120
+ for (int i = 0 ; i < rows; i++) {
121
+ for (int j = 0 ; j < cols; j++) {
122
+ log_Lw[i][j] = log (0.001 + Lw[i][j]);
123
+ Lw_sum += log_Lw[i][j];
124
+ }
125
+ }
126
+ // Lwaver
127
+ float Lwaver = exp (Lw_sum / (rows * cols));
128
+ // Lg
129
+ float **Lg;
130
+ Lg = new float *[rows];
131
+ for (int i = 0 ; i < rows; i++) {
132
+ Lg[i] = new float [cols];
133
+ }
134
+ for (int i = 0 ; i < rows; i++) {
135
+ for (int j = 0 ; j < cols; j++) {
136
+ Lg[i][j] = log (Lw[i][j] / Lwaver + 1 ) / log (Lwmax / Lwaver + 1 );
137
+ }
138
+ }
139
+ // gain
140
+ float **gain;
141
+ gain = new float *[rows];
142
+ for (int i = 0 ; i < rows; i++) {
143
+ gain[i] = new float [cols];
144
+ }
145
+ for (int i = 0 ; i < rows; i++) {
146
+ for (int j = 0 ; j < cols; j++) {
147
+ gain[i][j] = Lg[i][j] / Lw[i][j];
148
+ }
149
+ }
150
+ // gain*B, gain*G, gain*R
151
+ for (int i = 0 ; i < rows; i++) {
152
+ for (int j = 0 ; j < cols; j++) {
153
+ B[i][j] *= gain[i][j];
154
+ G[i][j] *= gain[i][j];
155
+ R[i][j] *= gain[i][j];
156
+ }
157
+ }
158
+ simple_color_balance (B, B, rows, cols);
159
+ simple_color_balance (G, G, rows, cols);
160
+ simple_color_balance (R, R, rows, cols);
161
+
162
+ for (int i = 0 ; i < rows; i++) {
163
+ for (int j = 0 ; j < cols; j++){
164
+ out_img.at <Vec3b>(i, j)[0 ] = uchar ((int )B[i][j]);
165
+ out_img.at <Vec3b>(i, j)[1 ] = uchar ((int )G[i][j]);
166
+ out_img.at <Vec3b>(i, j)[2 ] = uchar ((int )R[i][j]);
167
+ }
168
+ }
169
+ // Free
170
+ // DouImg
171
+ for (int i = 0 ; i < rows; i++) {
172
+ for (int j = 0 ; j < cols; j++) {
173
+ delete DouImg[i][j];
174
+ }
175
+ delete DouImg[i];
176
+ }
177
+ delete DouImg;
178
+ // Lw
179
+ for (int i = 0 ; i < rows; i++) {
180
+ delete[] Lw[i];
181
+ }
182
+ delete Lw;
183
+ // log_Lw
184
+ for (int i = 0 ; i < rows; i++) {
185
+ delete[] log_Lw[i];
186
+ }
187
+ delete log_Lw;
188
+ // B
189
+ for (int i = 0 ; i < rows; i++) {
190
+ delete[] B[i];
191
+ }
192
+ delete B;
193
+ // G
194
+ for (int i = 0 ; i < rows; i++) {
195
+ delete[] G[i];
196
+ }
197
+ delete G;
198
+ // R
199
+ for (int i = 0 ; i < rows; i++) {
200
+ delete[] R[i];
201
+ }
202
+ delete R;
203
+ // Lg
204
+ for (int i = 0 ; i < rows; i++) {
205
+ delete[] Lg[i];
206
+ }
207
+ delete Lg;
208
+ // gain
209
+ for (int i = 0 ; i < rows; i++) {
210
+ delete[] gain[i];
211
+ }
212
+ delete gain;
213
+ return 0 ;
214
+ }
0 commit comments