Optimized Belief Propagation (CPU and GPU)
filter.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 /* 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 
37 namespace bp_single_thread_imp {
38 
40 {
41 public:
42  /* normalize mask so it integrates to one */
43  static void normalize(std::vector<float> &mask) {
44  int len = mask.size();
45  float sum = 0;
46  for (int i = 1; i < len; i++) {
47  sum += fabs(mask[i]);
48  }
49  sum = 2*sum + fabs(mask[0]);
50  for (int i = 0; i < len; i++) {
51  mask[i] /= sum;
52  }
53  }
54 
55  /* make filters */
56  #define MAKE_FILTER(name, fun) \
57  static std::vector<float> make_ ## name (float sigma) { \
58  sigma = std::max(sigma, 0.01F); \
59  int len = (int)ceil(sigma * WIDTH) + 1; \
60  std::vector<float> mask(len); \
61  for (int i = 0; i < len; i++) { \
62  mask[i] = fun; \
63  } \
64  return mask; \
65  }
66 
67  MAKE_FILTER(f0, exp(-0.5*square(i/sigma)));
68 
69  /* convolve image with gaussian filter */
70  static image<float> *smooth(image<float> *src, float sigma) {
71  std::vector<float> mask = make_f0(sigma);
72  normalize(mask);
73 
74  image<float> *tmp = new image<float>(src->height(), src->width(), false);
75  image<float> *dst = new image<float>(src->width(), src->height(), false);
78 
79  delete tmp;
80  return dst;
81  }
82 
83  /* convolve image with gaussian filter */
84  static image<float> *smooth(image<uchar> *src, float sigma) {
85  image<float> *tmp = imageUCHARtoFLOAT(src);
86  image<float> *dst = smooth(tmp, sigma);
87  delete tmp;
88  return dst;
89  }
90 
91  /* compute laplacian */
93  int width = src->width();
94  int height = src->height();
95  image<float> *dst = new image<float>(width, height);
96 
97  for (int y = 1; y < height-1; y++) {
98  for (int x = 1; x < width-1; x++) {
99  float d2x = imRef(src, x-1, y) + imRef(src, x+1, y) -
100  2*imRef(src, x, y);
101  float d2y = imRef(src, x, y-1) + imRef(src, x, y+1) -
102  2*imRef(src, x, y);
103  imRef(dst, x, y) = d2x + d2y;
104  }
105  }
106  return dst;
107  }
108 };
109 
110 };
111 
112 #endif
static void convolve_even(image< float > *src, image< float > *dst, std::vector< float > &mask)
Definition: convolve.h:39
static image< float > * laplacian(image< float > *src)
Definition: filter.h:92
static void normalize(std::vector< float > &mask)
Definition: filter.h:43
static image< float > * smooth(image< float > *src, float sigma)
Definition: filter.h:70
MAKE_FILTER(f0, exp(-0.5 *square(i/sigma)))
static image< float > * smooth(image< uchar > *src, float sigma)
Definition: filter.h:84
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
T square(const T &x)
Definition: misc.h:51