forked from uneupane/guided-filter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
guided_filter.c
190 lines (149 loc) · 3.74 KB
/
guided_filter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <float.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <memory.h>
void calculatecumsum(unsigned char *,int, int, int**);
void boxfilter1(unsigned char *, int, int, int);
void guidedFilter(unsigned char * src, unsigned char * guidance,unsigned char * dest,int radius,float eps, int rows, int cols)
{
int i,j, k, l;
float mean_I, mean_Ip, cov_Ip, mean_II, var_I, a, b, mean_a, mean_b, mean_p, q;
const float e=eps*eps;
float *distance1;
float *distance2;
float *distance3;
float *distance4;
float *distance5;
float *distance6;
float *N;
int value = 1;
//the size of each local patch; N=(2r+1)^2 except for boundary pixels
unsigned char* unitfilter;
unsigned char Ip;
unsigned char* Ipp;
unsigned char Ii;
unsigned char* Iii;
unsigned char ai;
unsigned char* aii;
unsigned char bi;
unsigned char* bii;
unitfilter = (unsigned char*) calloc(rows*cols, sizeof(unsigned char));
for(k=0; k<rows; k++)
{
for(int l=0; l<cols; l++)
{
unitfilter[k*cols+l] = value;
}
}
boxfilter1(unitfilter, rows, cols, radius, N); //N = boxfilter(ones(hei, wid), r);
boxfilter1(guidance, rows, cols, radius, distance1);
boxfilter1(src, rows, cols, radius, distance2);
Ip = (*src)*(*guidance);
Ipp = &Ip;
boxfilter1(Ipp, rows, cols, radius, distance3);
mean_I = (*distance1)/(*N);
mean_p = (*distance2)/(*N);
mean_Ip = (*distance3)/(*N);
cov_Ip = mean_Ip - mean_I * mean_p;
Ii = (*guidance)*(*guidance);
Iii = &Ii;
boxfilter1(Iii, rows, cols, radius, distance4);
mean_II = (*distance4)/(*N);
var_I = mean_II - mean_I * mean_I;
a = cov_Ip / (var_I + eps);// Eqn. (5) in the paper;
b = mean_p - a * mean_I;// Eqn. (6) in the paper;
ai = (unsigned char)a;
aii = &ai;
bi = (unsigned char)b;
bii = &bi;
boxfilter1(aii, rows, cols, radius, distance5);
boxfilter1(bii, rows, cols, radius, distance6);
mean_a = (*distance5)/(*N);
mean_b = (*distance6)/(*N);
q = mean_a * (float)*src + mean_b;// Eqn. (8) in the paper;
}
void boxfilter1(unsigned char *image, int N, int M, int radius, float *distance)
{
int m, n, i, j;
int *cumsum;
//float *distance;
(distance) = (float *) calloc(N*M, sizeof(float));
calculatecumsum(image, M, N, &cumsum);
// Cumulative SUM over Y axis
for(n = 0; n <N; n++)
{
for(m = 0; m < M; m++)
{
if(m==0)
{
distance[m,n]= distance[m,n];
}
else
{
distance[m,n]= distance[m,n] + cumsum[m-1, n];
}
}
}
// Difference over Y axis
for(m = 0; m <=radius; m++)
{
for(n = 0; n < N; n++)
{
distance[m,n]= cumsum[m+radius, n];
}
}
//imDst(1:r+1, :) = imCum(1+r:2*r+1, :);
for(m = radius+1; m <= M-radius-1; m++)
{
for(n = 0; n < N; n++)
{
distance[m,n]= cumsum[m+radius, n]-cumsum[m*radius-1, n];
}
}
for(m = M-radius; m < M; m++)
{
for(n = 0; n < N; n++)
{
distance[m,n]= cumsum[M-1, n]-cumsum[m*radius-1, n];
}
}
// Cumulative SUM over X axis
for(m = 0; m <M; m++)
{
for(n = 0; n < N; n++)
{
if(n==0)
{
distance[m,n]= distance[m,n];
}
else
{
distance[m,n]= distance[m,n] + cumsum[m, n-1];
}
}
}
// Difference over X axis
for(m = 0; m <M; m++)
{
for(n = 0; n <= radius; n++)
{
distance[m,n]= cumsum[m,n+radius];
}
}
free(distance);
}
void calculatecumsum(unsigned char *image, int rows, int cols,
int **cumsumim)
{
int y,x;
(*cumsumim) = (int *) calloc(rows*cols, sizeof(int));
(*cumsumim) = (int *) image;
for (y = 0 ; y < rows ; y++ )
{
for (x = 0; x < cols ; x++ )
{
(cumsumim)[y*cols+x] += (int)(cumsumim)[y*cols+ x];
}
}
}