aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'DCP/guidedfilter.cpp')
-rw-r--r--DCP/guidedfilter.cpp106
1 files changed, 46 insertions, 60 deletions
diff --git a/DCP/guidedfilter.cpp b/DCP/guidedfilter.cpp
index bfdf956..34d255a 100644
--- a/DCP/guidedfilter.cpp
+++ b/DCP/guidedfilter.cpp
@@ -1,10 +1,5 @@
1#include "dcp_core.h" 1#include "dcp_core.h"
2 2
3#include <iostream>
4#include <vector>
5
6using namespace std;
7
8void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r) 3void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r)
9{ 4{
10 int height = q->height; 5 int height = q->height;
@@ -35,26 +30,24 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
35 double tmp_Ir, tmp_Ig, tmp_Ib; 30 double tmp_Ir, tmp_Ig, tmp_Ib;
36 double tmp_p, tmp_q; 31 double tmp_p, tmp_q;
37 32
38 vector<double> v_ak_r; 33 double *v_ak_r = (double *)malloc(sizeof(double) * height * width);
39 vector<double> v_ak_g; 34 double *v_ak_g = (double *)malloc(sizeof(double) * height * width);
40 vector<double> v_ak_b; 35 double *v_ak_b = (double *)malloc(sizeof(double) * height * width);
41 vector<double> v_bk; 36 double *v_bk = (double *)malloc(sizeof(double) * height * width);
37
38 int count = 0;
42 39
43 for (i = 1; i <= height; i++) 40 for (i = 0; i < height; i++)
44 { 41 {
45 for (j = 1; j <= width; j++) 42 for (j = 0; j < width; j++)
46 { 43 {
47 st_row = i - r, ed_row = i + r; 44 st_row = i - r, ed_row = i + r;
48 st_col = j - r, ed_col = j + r; 45 st_col = j - r, ed_col = j + r;
49 46
50 if (st_row <= 0) 47 st_row = st_row < 0 ? 0 : st_row;
51 st_row = 1; 48 ed_row = ed_row >= height ? (height - 1) : ed_row;
52 if (ed_row > height) 49 st_col = st_col < 0 ? 0 : st_col;
53 ed_row = height; 50 ed_col = ed_col >= width ? (width - 1) : ed_col;
54 if (st_col <= 0)
55 st_col = 1;
56 if (ed_col > width)
57 ed_col = width;
58 51
59 sum_Ir = sum_Ig = sum_Ib = 0; 52 sum_Ir = sum_Ig = sum_Ib = 0;
60 sum_Ir_square = sum_Ig_square = sum_Ib_square = 0; 53 sum_Ir_square = sum_Ig_square = sum_Ib_square = 0;
@@ -67,11 +60,11 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
67 { 60 {
68 for (n = st_col; n <= ed_col; n++) 61 for (n = st_col; n <= ed_col; n++)
69 { 62 {
70 tmp_Ib = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch); 63 tmp_Ib = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch);
71 tmp_Ig = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 1); 64 tmp_Ig = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch + 1);
72 tmp_Ir = *(uchar *)(II->imageData + (m - 1) * widthstep + (n - 1) * nch + 2); 65 tmp_Ir = *(uchar *)(II->imageData + (m) * widthstep + (n) * nch + 2);
73 66
74 tmp_p = *(uchar *)(p->imageData + (m - 1) * gwidthstep + (n - 1)); 67 tmp_p = *(uchar *)(p->imageData + (m) * gwidthstep + (n));
75 68
76 sum_Ib += tmp_Ib; 69 sum_Ib += tmp_Ib;
77 sum_Ig += tmp_Ig; 70 sum_Ig += tmp_Ig;
@@ -118,41 +111,34 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
118 111
119 bk = (sum_PiIg - sum_IrIg * ak_r - (sum_Ig_square + w * eps) * ak_g - sum_IgIb * ak_b) / sum_Ig; 112 bk = (sum_PiIg - sum_IrIg * ak_r - (sum_Ig_square + w * eps) * ak_g - sum_IgIb * ak_b) / sum_Ig;
120 113
121 tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch); 114 tmp_Ib = *(uchar *)(II->imageData + i * widthstep + j * nch);
122 tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1); 115 tmp_Ig = *(uchar *)(II->imageData + i * widthstep + j * nch + 1);
123 tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2); 116 tmp_Ir = *(uchar *)(II->imageData + i * widthstep + j * nch + 2);
124 117
125 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk; 118 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk;
119 tmp_q = tmp_q > 255 ? 255 : (tmp_q < 0 ? 0 : tmp_q);
126 120
127 if (tmp_q > 255) 121 *(uchar *)(q->imageData + i * gwidthstep + j) = cvRound(tmp_q);
128 tmp_q = 255;
129 else if (tmp_q < 0)
130 tmp_q = 0;
131 122
132 *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q; 123 v_ak_b[count] = ak_b;
133 124 v_ak_g[count] = ak_g;
134 v_ak_b.push_back(ak_b); 125 v_ak_r[count] = ak_r;
135 v_ak_g.push_back(ak_g); 126 v_bk[count] = bk;
136 v_ak_r.push_back(ak_r); 127 count++;
137 v_bk.push_back(bk);
138 } 128 }
139 } 129 }
140 130
141 for (int i = 1; i <= height; i++) 131 for (int i = 0; i < height; i++)
142 { 132 {
143 for (int j = 1; j <= width; j++) 133 for (int j = 0; j < width; j++)
144 { 134 {
145 st_row = i - r, ed_row = i + r; 135 st_row = i - r, ed_row = i + r;
146 st_col = j - r, ed_col = j + r; 136 st_col = j - r, ed_col = j + r;
147 137
148 if (st_row <= 0) 138 st_row = st_row < 0 ? 0 : st_row;
149 st_row = 1; 139 ed_row = ed_row >= height ? (height - 1) : ed_row;
150 if (ed_row > height) 140 st_col = st_col < 0 ? 0 : st_col;
151 ed_row = height; 141 ed_col = ed_col >= width ? (width - 1) : ed_col;
152 if (st_col <= 0)
153 st_col = 1;
154 if (ed_col > width)
155 ed_col = width;
156 142
157 double ak_r, ak_g, ak_b, bk; 143 double ak_r, ak_g, ak_b, bk;
158 ak_r = ak_g = ak_b = bk = 0; 144 ak_r = ak_g = ak_b = bk = 0;
@@ -162,10 +148,10 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
162 { 148 {
163 for (int n = st_col; n <= ed_col; n++) 149 for (int n = st_col; n <= ed_col; n++)
164 { 150 {
165 ak_r += v_ak_r[(m - 1) * width + n - 1]; 151 ak_r += v_ak_r[(m) * width + n];
166 ak_g += v_ak_g[(m - 1) * width + n - 1]; 152 ak_g += v_ak_g[(m) * width + n];
167 ak_b += v_ak_b[(m - 1) * width + n - 1]; 153 ak_b += v_ak_b[(m) * width + n];
168 bk += v_bk[(m - 1) * width + n - 1]; 154 bk += v_bk[(m) * width + n];
169 number++; 155 number++;
170 } 156 }
171 } 157 }
@@ -175,18 +161,18 @@ void GuidedFilterColor(IplImage *q, IplImage *II, IplImage *p, double eps, int r
175 ak_b /= number; 161 ak_b /= number;
176 bk /= number; 162 bk /= number;
177 163
178 tmp_Ib = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch); 164 tmp_Ib = *(uchar *)(II->imageData + i * widthstep + j * nch);
179 tmp_Ig = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 1); 165 tmp_Ig = *(uchar *)(II->imageData + i * widthstep + j * nch + 1);
180 tmp_Ir = *(uchar *)(II->imageData + (i - 1) * widthstep + (j - 1) * nch + 2); 166 tmp_Ir = *(uchar *)(II->imageData + i * widthstep + j * nch + 2);
181 167
182 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk; 168 tmp_q = ak_b * tmp_Ib + ak_g * tmp_Ig + ak_r * tmp_Ir + bk;
183 169 tmp_q = tmp_q > 255 ? 255 : (tmp_q < 0 ? 0 : tmp_q);
184 if (tmp_q > 255) 170
185 tmp_q = 255; 171 *(uchar *)(q->imageData + i * gwidthstep + j) = cvRound(tmp_q);
186 else if (tmp_q < 0)
187 tmp_q = 0;
188
189 *(uchar *)(q->imageData + (i - 1) * gwidthstep + (j - 1)) = tmp_q;
190 } 172 }
191 } 173 }
174 free(v_ak_b);
175 free(v_ak_g);
176 free(v_ak_r);
177 free(v_bk);
192} 178}
Powered by cgit v1.2.3 (git 2.41.0)