aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJinweiClarkChao <[email protected]>2015-08-24 10:15:00 +0800
committerJinweiClarkChao <[email protected]>2015-08-24 10:15:00 +0800
commit17fbf4355747b1ceb9caca120aec9830a44621cf (patch)
tree6ec188fe59b0ae8c65a812765ef8f621ecf87ec3 /DCP/guidedfilter.cpp
downloadDehaze-17fbf4355747b1ceb9caca120aec9830a44621cf.tar.gz
init
Diffstat (limited to 'DCP/guidedfilter.cpp')
-rw-r--r--DCP/guidedfilter.cpp195
1 files changed, 195 insertions, 0 deletions
diff --git a/DCP/guidedfilter.cpp b/DCP/guidedfilter.cpp
new file mode 100644
index 0000000..46040ad
--- /dev/null
+++ b/DCP/guidedfilter.cpp
@@ -0,0 +1,195 @@
1#include "dcp_core.h"
2
3#include <iostream>
4#include <vector>
5
6using namespace std;
7
8void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r)
9{
10 int height = q->height;
11 int width = q->width;
12 int widthstep = II->widthStep;
13 int gwidthstep = p->widthStep;
14 int nch = II->nChannels;
15
16 int i, j;
17 int m, n;
18 int w;
19 int e = 0;
20 int st_row, ed_row;
21 int st_col, ed_col;
22
23 double sum_Ir, sum_Ig, sum_Ib;
24 double sum_Ir_square, sum_Ig_square, sum_Ib_square;
25 double sum_IrIg, sum_IgIb, sum_IrIb;
26 double sum_PiIr, sum_PiIg, sum_PiIb;
27 double sum_Pi;
28
29 double A, B, C, D, E, F, G, H, I, J, K, L;
30 double X, Y, Z;
31 double ak_r, ak_g, ak_b;
32 double bk;
33 double det;
34
35 double tmp_Ir, tmp_Ig, tmp_Ib;
36 double tmp_p, tmp_q;
37
38 vector<double> v_ak_r;
39 vector<double> v_ak_g;
40 vector<double> v_ak_b;
41 vector<double> v_bk;
42
43 for (i = 1; i <= height; i++)
44 {
45 if (i % 10 == 0)
46 printf("%d\n", i);
47
48 for (j = 1; j <= width; j++)
49 {
50 st_row = i - r, ed_row = i + r;
51 st_col = j - r, ed_col = j + r;
52
53 if (st_row <= 0)
54 st_row = 1;
55 if (ed_row > height)
56 ed_row = height;
57 if (st_col <= 0)
58 st_col = 1;
59 if (ed_col > width)
60 ed_col = width;
61
62 sum_Ir = sum_Ig = sum_Ib = 0;
63 sum_Ir_square = sum_Ig_square = sum_Ib_square = 0;
64 sum_IrIg = sum_IgIb = sum_IrIb = 0;
65 sum_PiIr = sum_PiIg = sum_PiIb = 0;
66 sum_Pi = 0;
67 w = 0;
68
69 for (m = st_row; m <= ed_row; m++)
70 {
71 for (n = st_col; n <= ed_col; n++)
72 {
73 tmp_Ib = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch);
74 tmp_Ig = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 1);
75 tmp_Ir = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 2);
76
77 tmp_p = *(uchar *)(p->imageData + (m - 1) * gwidthstep + (n - 1));
78
79 sum_Ib += tmp_Ib;
80 sum_Ig += tmp_Ig;
81 sum_Ir += tmp_Ir;
82
83 sum_Ib_square += tmp_Ib * tmp_Ib;
84 sum_Ig_square += tmp_Ig * tmp_Ig;
85 sum_Ir_square += tmp_Ir * tmp_Ir;
86
87 sum_IrIg += tmp_Ir * tmp_Ig;
88 sum_IgIb += tmp_Ig * tmp_Ib;
89 sum_IrIb += tmp_Ir * tmp_Ib;
90
91 sum_Pi += tmp_p;
92 sum_PiIb += tmp_p * tmp_Ib;
93 sum_PiIg += tmp_p * tmp_Ig;
94 sum_PiIr += tmp_p * tmp_Ir;
95
96 w++;
97 }
98 }
99
100 A = (sum_Ir_square + w * eps) * sum_Ig - sum_Ir * sum_IrIg;
101 B = sum_IrIg * sum_Ig - sum_Ir * (sum_Ig_square + w * eps);
102 C = sum_IrIb * sum_Ig - sum_Ir * sum_IgIb;
103 D = sum_PiIr * sum_Ig - sum_PiIg * sum_Ir;
104 E = (sum_Ir_square + w * eps) * sum_Ib - sum_IrIb * sum_Ir;
105 F = sum_IrIg * sum_Ib - sum_IgIb * sum_Ir;
106 G = sum_IrIb * sum_Ib - (sum_Ib_square + w * eps) * sum_Ir;
107 H = sum_PiIr * sum_Ib - sum_PiIb * sum_Ir;
108 I = (sum_Ir_square + w * eps) * w - sum_Ir * sum_Ir;
109 J = sum_IrIg * w - sum_Ig * sum_Ir;
110 K = sum_IrIb * w - sum_Ib * sum_Ir;
111 L = sum_PiIr * w - sum_Pi * sum_Ir;
112
113 det = A * F * K + B * G * I + C * E * J - C * F * I - A * G * J - B * E * K;
114 X = D * F * K + B * G * L + C * H * J - C * F * L - D * G * J - B * H * K;
115 Y = A * H * K + D * G * I + C * E * L - C * H * I - D * E * K - A * G * L;
116 Z = A * F * L + B * H * I + D * J * E - D * F * I - B * E * L - A * H * J;
117
118 ak_r = X / det;
119 ak_g = Y / det;
120 ak_b = Z / det;
121
122 bk = (sum_PiIg - sum_IrIg * ak_r - (sum_Ig_square + w * eps) * ak_g - sum_IgIb * ak_b) / sum_Ig;
123
124 tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch);
125 tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1);
126 tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2);
127
128 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk;
129
130 if (tmp_q > 255)
131 tmp_q = 255;
132 else if (tmp_q < 0)
133 tmp_q = 0;
134
135 *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q;
136
137 v_ak_b.push_back(ak_b);
138 v_ak_g.push_back(ak_g);
139 v_ak_r.push_back(ak_r);
140 v_bk.push_back(bk);
141 }
142 }
143
144 for (int i = 1; i <= height; i++)
145 {
146 for (int j = 1; j <= width; j++)
147 {
148 st_row = i - r, ed_row = i + r;
149 st_col = j - r, ed_col = j + r;
150
151 if (st_row <= 0)
152 st_row = 1;
153 if (ed_row > height)
154 ed_row = height;
155 if (st_col <= 0)
156 st_col = 1;
157 if (ed_col > width)
158 ed_col = width;
159
160 double ak_r, ak_g, ak_b, bk;
161 ak_r = ak_g = ak_b = bk = 0;
162
163 int number = 0;
164 for (int m = st_row; m <= ed_row; m++)
165 {
166 for (int n = st_col; n <= ed_col; n++)
167 {
168 ak_r += v_ak_r[(m - 1) * width + n - 1];
169 ak_g += v_ak_g[(m - 1) * width + n - 1];
170 ak_b += v_ak_b[(m - 1) * width + n - 1];
171 bk += v_bk[(m - 1) * width + n - 1];
172 number++;
173 }
174 }
175
176 ak_r /= number;
177 ak_g /= number;
178 ak_b /= number;
179 bk /= number;
180
181 tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch);
182 tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1);
183 tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2);
184
185 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk;
186
187 if (tmp_q > 255)
188 tmp_q = 255;
189 else if (tmp_q < 0)
190 tmp_q = 0;
191
192 *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q;
193 }
194 }
195}
Powered by cgit v1.2.3 (git 2.41.0)