segmentation
All Data Structures Namespaces Files Functions Variables Modules Pages
convolve.h
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 /* convolution */
20 
21 #ifndef CONVOLVE_H
22 #define CONVOLVE_H
23 
24 #include <vector>
25 #include <algorithm>
26 #include <cmath>
27 #include "image.h"
28 
29 /* convolve src with mask. dst is flipped! */
30 static void convolve_even(image<float> *src, image<float> *dst,
31  std::vector<float> &mask) {
32  int width = src->width();
33  int height = src->height();
34  int len = mask.size();
35 
36  for (int y = 0; y < height; y++) {
37  for (int x = 0; x < width; x++) {
38  float sum = mask[0] * imRef(src, x, y);
39  for (int i = 1; i < len; i++) {
40  sum += mask[i] *
41  (imRef(src, std::max(x-i,0), y) +
42  imRef(src, std::min(x+i, width-1), y));
43  }
44  imRef(dst, y, x) = sum;
45  }
46  }
47 }
48 
49 /* convolve src with mask. dst is flipped! */
50 static void convolve_odd(image<float> *src, image<float> *dst,
51  std::vector<float> &mask) {
52  int width = src->width();
53  int height = src->height();
54  int len = mask.size();
55 
56  for (int y = 0; y < height; y++) {
57  for (int x = 0; x < width; x++) {
58  float sum = mask[0] * imRef(src, x, y);
59  for (int i = 1; i < len; i++) {
60  sum += mask[i] *
61  (imRef(src, std::max(x-i,0), y) -
62  imRef(src, std::min(x+i, width-1), y));
63  }
64  imRef(dst, y, x) = sum;
65  }
66  }
67 }
68 
69 #endif