Optimized Belief Propagation (CPU and GPU)
convolve.h
Go to the documentation of this file.
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 
34 
35 class Convolve
36 {
37 public:
38  /* convolve src with mask. dst is flipped! */
39  static void convolve_even(image<float> *src, image<float> *dst,
40  std::vector<float> &mask) {
41  int width = src->width();
42  int height = src->height();
43  int len = mask.size();
44 
45  for (int y = 0; y < height; y++) {
46  for (int x = 0; x < width; x++) {
47  float sum = mask[0] * imRef(src, x, y);
48  for (int i = 1; i < len; i++) {
49  sum += mask[i] *
50  (imRef(src, std::max(x-i,0), y) +
51  imRef(src, std::min(x+i, width-1), y));
52  }
53  imRef(dst, y, x) = sum;
54  }
55  }
56  }
57 
58  /* convolve src with mask. dst is flipped! */
59  static void convolve_odd(image<float> *src, image<float> *dst,
60  std::vector<float> &mask) {
61  int width = src->width();
62  int height = src->height();
63  int len = mask.size();
64 
65  for (int y = 0; y < height; y++) {
66  for (int x = 0; x < width; x++) {
67  float sum = mask[0] * imRef(src, x, y);
68  for (int i = 1; i < len; i++) {
69  sum += mask[i] *
70  (imRef(src, std::max(x-i,0), y) -
71  imRef(src, std::min(x+i, width-1), y));
72  }
73  imRef(dst, y, x) = sum;
74  }
75  }
76  }
77 };
78 
79 };
80 
81 #endif
static void convolve_odd(image< float > *src, image< float > *dst, std::vector< float > &mask)
Definition: convolve.h:59
static void convolve_even(image< float > *src, image< float > *dst, std::vector< float > &mask)
Definition: convolve.h:39
int height() const
Definition: image.h:51
#define imRef(im, x, y)
Definition: image.h:64
Class and structs in single-thread CPU bp implementation by Pedro Felzenwalb available at https://cs....
Definition: convolve.h:33