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 image<float> *smooth(image<float> *src, float sigma) { |
---|
62 | std::vector<float> mask = make_fgauss(sigma); |
---|
63 | normalize(mask); |
---|
64 | |
---|
65 | image<float> *tmp = new image<float>(src->height(), src->width(), false); |
---|
66 | image<float> *dst = 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 | image<float> *smooth(image<uchar> *src, float sigma) { |
---|
76 | image<float> *tmp = imageUCHARtoFLOAT(src); |
---|
77 | image<float> *dst = smooth(tmp, sigma); |
---|
78 | delete tmp; |
---|
79 | return dst; |
---|
80 | } |
---|
81 | |
---|
82 | /* compute laplacian */ |
---|
83 | static image<float> *laplacian(image<float> *src) { |
---|
84 | int width = src->width(); |
---|
85 | int height = src->height(); |
---|
86 | image<float> *dst = new image<float>(width, height); |
---|
87 | |
---|
88 | for (int y = 1; y < height-1; y++) { |
---|
89 | for (int x = 1; x < width-1; x++) { |
---|
90 | float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) - |
---|
91 | 2*imRef(src, x, y); |
---|
92 | float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) - |
---|
93 | 2*imRef(src, x, y); |
---|
94 | imRef(dst, x, y) = d2x + d2y; |
---|
95 | } |
---|
96 | } |
---|
97 | return dst; |
---|
98 | } |
---|
99 | |
---|
100 | #endif |
---|