Optimized Belief Propagation (CPU and GPU)
SmoothImage.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2024 Scott Grauer-Gray
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 
27 #include "SmoothImage.h"
28 
29 //create a Gaussian filter from a sigma value
30 std::vector<float> SmoothImage::MakeFilter(float sigma) const
31 {
32  const float sigma_use{std::max(sigma, 0.01f)};
33  const unsigned int size_filter{
34  (unsigned int)std::ceil(sigma_use * kWidthSigma1) + 1u};
35  std::vector<float> mask(size_filter);
36  for (unsigned int i = 0; i < size_filter; i++) {
37  mask[i] = std::exp(-0.5*((i/sigma_use) * (i/sigma_use)));
38  }
39  NormalizeFilter(mask);
40 
41  return mask;
42 }
43 
44 //normalize filter mask so it integrates to one
45 void SmoothImage::NormalizeFilter(std::vector<float>& filter) const
46 {
47  float sum{0.0};
48  for (unsigned int i = 1; i < filter.size(); i++) {
49  sum += std::abs(filter[i]);
50  }
51  sum = 2*sum + std::abs(filter[0]);
52  for (unsigned int i = 0; i < filter.size(); i++) {
53  filter[i] /= sum;
54  }
55 }
Declares class for smoothing the images before running BP.
constexpr float kWidthSigma1
Definition: SmoothImage.h:42
std::vector< float > MakeFilter(float sigma) const
Create a Gaussian filter from a sigma value.
Definition: SmoothImage.cpp:30
T abs(const T &x)
Definition: misc.h:45