Boost GIL


harris.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_HARRIS_HPP
9 #define BOOST_GIL_IMAGE_PROCESSING_HARRIS_HPP
10 
11 #include <boost/gil/image_view.hpp>
12 #include <boost/gil/typedefs.hpp>
13 #include <boost/gil/image_processing/kernel.hpp>
14 
15 namespace boost { namespace gil {
21 
22 
33 template <typename T, typename Allocator>
35  boost::gil::gray32f_view_t m11,
36  boost::gil::gray32f_view_t m12_21,
37  boost::gil::gray32f_view_t m22,
39  float k,
40  boost::gil::gray32f_view_t harris_response)
41 {
42  if (m11.dimensions() != m12_21.dimensions() || m12_21.dimensions() != m22.dimensions()) {
43  throw std::invalid_argument("m prefixed arguments must represent"
44  " tensor from the same image");
45  }
46 
47  std::ptrdiff_t const window_length = weights.size();
48  auto const width = m11.width();
49  auto const height = m11.height();
50  auto const half_length = window_length / 2;
51 
52  for (auto y = half_length; y < height - half_length; ++y)
53  {
54  for (auto x = half_length; x < width - half_length; ++x)
55  {
56  float ddxx = 0;
57  float dxdy = 0;
58  float ddyy = 0;
59  for (gil::gray32f_view_t::coord_t y_kernel = 0;
60  y_kernel < window_length;
61  ++y_kernel) {
62  for (gil::gray32f_view_t::coord_t x_kernel = 0;
63  x_kernel < window_length;
64  ++x_kernel) {
65  ddxx += m11(x + x_kernel - half_length, y + y_kernel - half_length)
66  .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
67  dxdy += m12_21(x + x_kernel - half_length, y + y_kernel - half_length)
68  .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
69  ddyy += m22(x + x_kernel - half_length, y + y_kernel - half_length)
70  .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
71  }
72  }
73  auto det = (ddxx * ddyy) - dxdy * dxdy;
74  auto trace = ddxx + ddyy;
75  auto harris_value = det - k * trace * trace;
76  harris_response(x, y).at(std::integral_constant<int, 0>{}) = harris_value;
77  }
78  }
79 }
80 
81 }} //namespace boost::gil
82 #endif
variable-size kernel
Definition: kernel.hpp:273
void compute_harris_responses(boost::gil::gray32f_view_t m11, boost::gil::gray32f_view_t m12_21, boost::gil::gray32f_view_t m22, boost::gil::detail::kernel_2d< T, Allocator > weights, float k, boost::gil::gray32f_view_t harris_response)
function to record Harris responses
Definition: harris.hpp:34
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36