Boost GIL


numeric.hpp
1 //
2 // Copyright 2019 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
3 //
4 // Use, modification and distribution are subject to the Boost Software License,
5 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8 #ifndef BOOST_GIL_IMAGE_PROCESSING_NUMERIC_HPP
9 #define BOOST_GIL_IMAGE_PROCESSING_NUMERIC_HPP
10 
11 #include <boost/gil/detail/math.hpp>
12 #include <cmath>
13 #include <boost/gil/image_view.hpp>
14 #include <boost/gil/typedefs.hpp>
15 
16 namespace boost { namespace gil {
17 
29 inline double normalized_sinc(double x)
30 {
31  return std::sin(x * boost::gil::pi) / (x * boost::gil::pi);
32 }
33 
41 inline double lanczos(double x, std::ptrdiff_t a)
42 {
43  // means == but <= avoids compiler warning
44  if (0 <= x && x <= 0)
45  return 1;
46 
47  if (-a < x && x < a)
48  return normalized_sinc(x) / normalized_sinc(x / static_cast<double>(a));
49 
50  return 0;
51 }
52 
59 inline void generate_normalized_mean(boost::gil::gray32f_view_t dst)
60 {
61  if (dst.width() != dst.height() || dst.width() % 2 != 1)
62  throw std::invalid_argument("kernel dimensions should be odd and equal");
63  const float entry = 1.0f / static_cast<float>(dst.size());
64 
65  for (auto& pixel: dst) {
66  pixel.at(std::integral_constant<int, 0>{}) = entry;
67  }
68 }
69 
74 inline void generate_unnormalized_mean(boost::gil::gray32f_view_t dst)
75 {
76  if (dst.width() != dst.height() || dst.width() % 2 != 1)
77  throw std::invalid_argument("kernel dimensions should be odd and equal");
78 
79  for (auto& pixel: dst) {
80  pixel.at(std::integral_constant<int, 0>{}) = 1.0f;
81  }
82 }
83 
89 inline void generate_gaussian_kernel(boost::gil::gray32f_view_t dst, double sigma)
90 {
91  if (dst.width() != dst.height() || dst.width() % 2 != 1)
92  throw std::invalid_argument("kernel dimensions should be odd and equal");
93 
94  const double denominator = 2 * boost::gil::pi * sigma * sigma;
95  const auto middle = boost::gil::point_t(dst.width() / 2, dst.height() / 2);
96  for (boost::gil::gray32f_view_t::coord_t y = 0; y < dst.height(); ++y)
97  {
98  for (boost::gil::gray32f_view_t::coord_t x = 0; x < dst.width(); ++x)
99  {
100  const auto delta_x = std::abs(middle.x - x);
101  const auto delta_y = std::abs(middle.y - y);
102  const double power = (delta_x * delta_x + delta_y * delta_y) / (2 * sigma * sigma);
103  const double nominator = std::exp(-power);
104  const float value = nominator / denominator;
105  dst(x, y).at(std::integral_constant<int, 0>{}) = value;
106  }
107  }
108 }
109 
110 }} // namespace boost::gil
111 
112 #endif
Definition: algorithm.hpp:30
Represents a pixel value (a container of channels). Models: HomogeneousColorBaseValueConcept, PixelValueConcept, HomogeneousPixelBasedConcept.
Definition: metafunctions.hpp:23
double lanczos(double x, std::ptrdiff_t a)
Lanczos response at point xLanczos response is defined as: x == 0: 1 -a < x && x < a: 0 otherwise: no...
Definition: numeric.hpp:41
void generate_normalized_mean(boost::gil::gray32f_view_t dst)
Generate mean kernelFills supplied view with normalized mean in which all entries will be equal to...
Definition: numeric.hpp:59
void generate_unnormalized_mean(boost::gil::gray32f_view_t dst)
Generate kernel with all 1sFills supplied view with 1s (ones)
Definition: numeric.hpp:74
void generate_gaussian_kernel(boost::gil::gray32f_view_t dst, double sigma)
Generate Gaussian kernelFills supplied view with values taken from Gaussian distribution. See https://en.wikipedia.org/wiki/Gaussian_blur.
Definition: numeric.hpp:89