-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter_misc.c
293 lines (263 loc) · 8.2 KB
/
filter_misc.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* filter_misc.c
Various filter kernels
Copyright (c) 2007,2008,2009,2010 Matthias Kramm <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <assert.h>
#include <math.h>
#include "matrix.h"
#include "filter.h"
#include "image.h"
static void normalize(matrix_t*m)
{
double sum = 0;
int t;
int l = m->width*m->height;
for(t=0;t<l;t++) {
sum += (m->data[t]*m->data[t]);
}
if(fabs(sum<0.0001))
return;
//sum = sqrt(sum);
for(t=0;t<l;t++) {
m->data[t] /= sum;
}
}
matrix_t* gabor_filter(int width, int height, float ax, float ay, float phase_shift, float ifreq, float angle, char complex_counterpart)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float ccos = cos(angle*M_PI/180);
float csin = sin(angle*M_PI/180);
int x0 = width/2;
int y0 = height/2;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = (x-x0)*ccos + (y-y0)*csin;
float yy = (x-x0)*csin - (y-y0)*ccos;
float r = ax*ax*xx*xx + ay*ay*yy*yy;
if(complex_counterpart)
m->data[y*width+x] = exp(-r)*cos(2*M_PI*ifreq*xx+phase_shift);
else
m->data[y*width+x] = exp(-r)*sin(2*M_PI*ifreq*xx+phase_shift);
}
normalize(m);
return m;
}
static void normalize_complex(complex_matrix_t*m)
{
double sum = 0;
int t;
int l = m->width*m->height;
for(t=0;t<l;t++) {
sum += (m->data[t].real*m->data[t].real + m->data[t].imag*m->data[t].imag);
}
if(fabs(sum<0.0001))
return;
//sum = sqrt(sum);
for(t=0;t<l;t++) {
m->data[t].real /= sum;
m->data[t].imag /= sum;
}
}
complex_matrix_t* gabor2_filter(int width, int height, float a, float f1, float f2)
{
complex_matrix_t*m = complex_matrix_new(width, height);
int x,y;
int x0 = width/2;
int y0 = height/2;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = (x-x0);
float yy = (y-y0);
float r = a*a*xx*xx + a*a*yy*yy;
m->data[y*width+x].real = exp(-r)*cos(2*M_PI*(f1*xx+f2*yy) / width);
m->data[y*width+x].imag = exp(-r)*sin(2*M_PI*(f1*xx+f2*yy) / width);
}
normalize_complex(m);
return m;
}
matrix_t* gauss_diff(int width, int height, float a, float angle, float len)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float ccos = cos(angle*M_PI/180);
float csin = sin(angle*M_PI/180);
float x0 = width/2 - ccos*len;
float y0 = height/2 - csin*len;
float x1 = width/2 + ccos*len;
float y1 = height/2 + csin*len;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float r0 = a*a*(x-x0)*(x-x0) + a*a*(y-y0)*(y-y0);
float r1 = a*a*(x-x1)*(x-x1) + a*a*(y-y1)*(y-y1);
float e0 = exp(-r0);
float e1 = exp(-r1);
m->data[y*width+x] = e0-e1;
}
normalize(m);
return m;
}
/* derivative of a gaussian */
matrix_t* gauss_diff2(int width, int height, float d, float angle)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float x0 = width/2;
float y0 = height/2;
float ccos = cos(angle*M_PI/180);
float csin = sin(angle*M_PI/180);
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = x-x0;
float yy = y-y0;
float r = (xx*xx+yy*yy)/(2*d*d);
float l = ccos*xx + csin*yy;
float e = l*exp(-r);
m->data[y*width+x] = e;
}
normalize(m);
return m;
}
matrix_t* gauss_highpass(int width, int height, float r0, float a)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float x0 = width/2;
float y0 = height/2;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = x-x0;
float yy = y-y0;
float r = (sqrt(xx*xx+yy*yy) - r0)*a;
float e = exp(-r*r);
m->data[y*width+x] = e;
}
normalize(m);
return m;
}
matrix_t* gauss_filter(int width, int height, float d)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float x0 = width/2;
float y0 = height/2;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = x-x0;
float yy = y-y0;
float r = (xx*xx+yy*yy) / (2*d*d);
float e = exp(-r);
m->data[y*width+x] = e;
}
normalize(m);
return m;
}
matrix_t* lgauss_filter(int width, int height, float d)
{
matrix_t*m = matrix_new(width, height);
int x,y;
float x0 = width/2;
float y0 = height/2;
for(y=0;y<height;y++)
for(x=0;x<width;x++) {
float xx = x-x0;
float yy = y-y0;
float r = (xx*xx+yy*yy) / (2*d*d);
float e = (1.0/(M_PI*d*d*d*d))*(1-r)*exp(-r);
m->data[y*width+x] = e;
}
normalize(m);
return m;
}
#define ADD_MATRIX(m) {if(pass==1) {filter_t*f = filter_new(filtertype_convolve, FILTER_DIRECTIONAL, m, 1, 1);base->children[num] = filtertree_new(0, f);} num++;}
#define ADD_COMPLEX_MATRIX(m) {if(pass==1) {filter_t*f = filter_new(filtertype_convolve, FILTER_DIRECTIONAL|FILTER_COMPLEX, (matrix_t*)m, 1, 1);base->children[num] = filtertree_new(0, f);} num++;}
static void check_filtertree_uniqueness(filtertree_t*base)
{
int s,t;
for(t=0;t<base->num_children;t++) {
for(s=0;s<base->num_children;s++) {
if(s!=t && matrix_diff(base->children[t]->filter->matrix, base->children[s]->filter->matrix) < 0.001)
printf("matrix %d and %d are equal\n", s, t);
}
}
}
filtertree_t* makeGaborTree(int width, int height, int xx, int yy)
{
int pass;
int num;
filtertree_t*base = 0;
for(pass=0;pass<2;pass++) {
if(pass==1)
base = filtertree_new(num, 0);
num = 0;
int x,y;
for(x=0;x<xx;x++) {
for(y=0;y<yy;y++) {
if(x||y) {
ADD_COMPLEX_MATRIX(gabor2_filter(width,height, 80.0 / (width*height), x, y));
}
}
}
}
check_filtertree_uniqueness(base);
return base;
}
complex_matrix_t*gauss_diff_complex(int width, int height, double a)
{
matrix_t*g1 = gauss_diff2(width, height, a, 0);
matrix_t*g2 = gauss_diff2(width, height, a, 90);
complex_matrix_t*g = complex_matrix_new(g1->width, g1->height);
int t;
for(t=0;t<g->width*g->height;t++) {
g->data[t].real = g1->data[t];
g->data[t].imag = g2->data[t];
}
matrix_delete(g1);
matrix_delete(g2);
return g;
}
complex_matrixset_t* filterset_new_dct(int width, int height, int stepx, int stepy, char complexversion)
{
complex_matrixset_t* f = complex_matrixset_new((width/stepx)*(height/stepy));
int x1,y1,x2,y2;
int size = width*height;
int pos = 0;
for(y1=0;y1<height;y1+=stepy)
for(x1=0;x1<width;x1+=stepx) {
f->m[pos] = complex_matrix_new(width,height);
complex_t*data = f->m[pos]->data;
double ax = 3.14159265358979*x1/(double)(2*width);
double ay = 3.14159265358979*y1/(double)(2*height);
double sum = 0;
double c = 0.25;
if(!x1) c *= 0.70710678118654746;
if(!y1) c *= 0.70710678118654746;
for(x2=0;x2<width;x2++)
for(y2=0;y2<height;y2++) {
double xx2 = (x2*2+1);
double yy2 = (y2*2+1);
if(complexversion) {
data[y2*width+x2].real = c*(cos(ax*xx2)*cos(ay*yy2) - sin(ax*xx2)*sin(ay*yy2));
data[y2*width+x2].imag = c*(cos(ax*xx2)*sin(ay*yy2) + sin(ax*xx2)*cos(ay*yy2));
} else {
data[y2*width+x2].real = c*(cos(ax*xx2)*cos(ay*yy2));
data[y2*width+x2].imag = 0;
}
}
pos++;
}
return f;
}