Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:51:51

0001 //
0002 // Copyright 2019 Olzhas Zhumabek <anonymous.from.applecity@gmail.com>
0003 //
0004 // Use, modification and distribution are subject to the Boost Software License,
0005 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 #ifndef BOOST_GIL_IMAGE_PROCESSING_HARRIS_HPP
0009 #define BOOST_GIL_IMAGE_PROCESSING_HARRIS_HPP
0010 
0011 #include <boost/gil/image_view.hpp>
0012 #include <boost/gil/typedefs.hpp>
0013 #include <boost/gil/image_processing/kernel.hpp>
0014 
0015 namespace boost { namespace gil {
0016 /// \defgroup CornerDetectionAlgorithms
0017 /// \brief Algorithms that are used to find corners in an image
0018 ///
0019 /// These algorithms are used to find spots from which
0020 /// sliding the window will produce large intensity change
0021 
0022 
0023 /// \brief function to record Harris responses
0024 /// \ingroup CornerDetectionAlgorithms
0025 ///
0026 /// This algorithm computes Harris responses
0027 /// for structure tensor represented by m11, m12_21, m22 views.
0028 /// Note that m12_21 represents both entries (1, 2) and (2, 1).
0029 /// Window length represents size of a window which is slided around
0030 /// to compute sum of corresponding entries. k is a discrimination
0031 /// constant against edges (usually in range 0.04 to 0.06).
0032 /// harris_response is an out parameter that will contain the Harris responses.
0033 template <typename T, typename Allocator>
0034 void compute_harris_responses(
0035     boost::gil::gray32f_view_t m11,
0036     boost::gil::gray32f_view_t m12_21,
0037     boost::gil::gray32f_view_t m22,
0038     boost::gil::detail::kernel_2d<T, Allocator> weights,
0039     float k,
0040     boost::gil::gray32f_view_t harris_response)
0041 {
0042     if (m11.dimensions() != m12_21.dimensions() || m12_21.dimensions() != m22.dimensions()) {
0043         throw std::invalid_argument("m prefixed arguments must represent"
0044             " tensor from the same image");
0045     }
0046 
0047     std::ptrdiff_t const window_length = weights.size();
0048     auto const width = m11.width();
0049     auto const height = m11.height();
0050     auto const half_length = window_length / 2;
0051 
0052     for (auto y = half_length; y < height - half_length; ++y)
0053     {
0054         for (auto x = half_length; x < width - half_length; ++x)
0055         {
0056             float ddxx = 0;
0057             float dxdy = 0;
0058             float ddyy = 0;
0059             for (gil::gray32f_view_t::coord_t y_kernel = 0;
0060                 y_kernel < window_length;
0061                 ++y_kernel) {
0062                 for (gil::gray32f_view_t::coord_t x_kernel = 0;
0063                     x_kernel < window_length;
0064                     ++x_kernel) {
0065                     ddxx += m11(x + x_kernel - half_length, y + y_kernel - half_length)
0066                         .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
0067                     dxdy += m12_21(x + x_kernel - half_length, y + y_kernel - half_length)
0068                         .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
0069                     ddyy += m22(x + x_kernel - half_length, y + y_kernel - half_length)
0070                         .at(std::integral_constant<int, 0>{}) * weights.at(x_kernel, y_kernel);
0071                 }
0072             }
0073             auto det = (ddxx * ddyy) - dxdy * dxdy;
0074             auto trace = ddxx + ddyy;
0075             auto harris_value = det - k * trace * trace;
0076             harris_response(x, y).at(std::integral_constant<int, 0>{}) = harris_value;
0077         }
0078     }
0079 }
0080 
0081 }} //namespace boost::gil
0082 #endif