1 | /* |
---|
2 | Copyright (C) 2006 Pedro Felzenszwalb |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | */ |
---|
18 | |
---|
19 | /* simple filters */ |
---|
20 | |
---|
21 | #ifndef FILTER_H |
---|
22 | #define FILTER_H |
---|
23 | |
---|
24 | #include <vector> |
---|
25 | #include <cmath> |
---|
26 | #include "image.h" |
---|
27 | #include "misc.h" |
---|
28 | #include "convolve.h" |
---|
29 | #include "imconv.h" |
---|
30 | |
---|
31 | #define WIDTH 4.0 |
---|
32 | |
---|
33 | /* normalize mask so it integrates to one */ |
---|
34 | static void normalize(std::vector<float> &mask) { |
---|
35 | int len = mask.size(); |
---|
36 | float sum = 0; |
---|
37 | for (int i = 1; i < len; i++) { |
---|
38 | sum += fabs(mask[i]); |
---|
39 | } |
---|
40 | sum = 2*sum + fabs(mask[0]); |
---|
41 | for (int i = 0; i < len; i++) { |
---|
42 | mask[i] /= sum; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | /* make filters */ |
---|
47 | #define MAKE_FILTER(name, fun) \ |
---|
48 | static std::vector<float> make_ ## name (float sigma) { \ |
---|
49 | sigma = std::max(sigma, 0.01F); \ |
---|
50 | int len = (int)ceil(sigma * WIDTH) + 1; \ |
---|
51 | std::vector<float> mask(len); \ |
---|
52 | for (int i = 0; i < len; i++) { \ |
---|
53 | mask[i] = fun; \ |
---|
54 | } \ |
---|
55 | return mask; \ |
---|
56 | } |
---|
57 | |
---|
58 | MAKE_FILTER(fgauss, exp(-0.5*square(i/sigma))); |
---|
59 | |
---|
60 | /* convolve image with gaussian filter */ |
---|
61 | static imagef *smooth(imagef *src, float sigma) { |
---|
62 | std::vector<float> mask = make_fgauss(sigma); |
---|
63 | normalize(mask); |
---|
64 | |
---|
65 | imagef *tmp = imagef_construct(src->h, src->w, false);//new image<float>(src->height(), src->width(), false); |
---|
66 | imagef *dst = imagef_construct(src->w,src->h,false); //new image<float>(src->width(), src->height(), false); |
---|
67 | convolve_even(src, tmp, mask); |
---|
68 | convolve_even(tmp, dst, mask); |
---|
69 | |
---|
70 | delete tmp; |
---|
71 | return dst; |
---|
72 | } |
---|
73 | |
---|
74 | /* convolve image with gaussian filter */ |
---|
75 | /* |
---|
76 | image<float> *smooth(image<uchar> *src, float sigma) { |
---|
77 | image<float> *tmp = imageUCHARtoFLOAT(src); |
---|
78 | image<float> *dst = smooth(tmp, sigma); |
---|
79 | delete tmp; |
---|
80 | return dst; |
---|
81 | } |
---|
82 | */ |
---|
83 | |
---|
84 | #endif |
---|