hessian.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <boost/gil/image.hpp>
  2. #include <boost/gil/image_view.hpp>
  3. #include <boost/gil/image_processing/numeric.hpp>
  4. #include <boost/gil/image_processing/hessian.hpp>
  5. #include <boost/gil/extension/io/png.hpp>
  6. #include <vector>
  7. #include <functional>
  8. #include <set>
  9. #include <iostream>
  10. #include <fstream>
  11. namespace gil = boost::gil;
  12. // some images might produce artifacts
  13. // when converted to grayscale,
  14. // which was previously observed on
  15. // canny edge detector for test input
  16. // used for this example.
  17. // the algorithm here follows sRGB gamma definition
  18. // taken from here (luminance calculation):
  19. // https://en.wikipedia.org/wiki/Grayscale
  20. gil::gray8_image_t to_grayscale(gil::rgb8_view_t original)
  21. {
  22. gil::gray8_image_t output_image(original.dimensions());
  23. auto output = gil::view(output_image);
  24. constexpr double max_channel_intensity = (std::numeric_limits<std::uint8_t>::max)();
  25. for (long int y = 0; y < original.height(); ++y)
  26. {
  27. for (long int x = 0; x < original.width(); ++x)
  28. {
  29. // scale the values into range [0, 1] and calculate linear intensity
  30. auto& p = original(x, y);
  31. double red_intensity = p.at(std::integral_constant<int, 0>{})
  32. / max_channel_intensity;
  33. double green_intensity = p.at(std::integral_constant<int, 1>{})
  34. / max_channel_intensity;
  35. double blue_intensity = p.at(std::integral_constant<int, 2>{})
  36. / max_channel_intensity;
  37. auto linear_luminosity = 0.2126 * red_intensity
  38. + 0.7152 * green_intensity
  39. + 0.0722 * blue_intensity;
  40. // perform gamma adjustment
  41. double gamma_compressed_luminosity = 0;
  42. if (linear_luminosity < 0.0031308)
  43. {
  44. gamma_compressed_luminosity = linear_luminosity * 12.92;
  45. } else
  46. {
  47. gamma_compressed_luminosity = 1.055 * std::pow(linear_luminosity, 1 / 2.4) - 0.055;
  48. }
  49. // since now it is scaled, descale it back
  50. output(x, y) = gamma_compressed_luminosity * max_channel_intensity;
  51. }
  52. }
  53. return output_image;
  54. }
  55. void apply_gaussian_blur(gil::gray8_view_t input_view, gil::gray8_view_t output_view)
  56. {
  57. constexpr static auto filter_height = 5ull;
  58. constexpr static auto filter_width = 5ull;
  59. constexpr static double filter[filter_height][filter_width] =
  60. {
  61. 2, 4, 6, 4, 2,
  62. 4, 9, 12, 9, 4,
  63. 5, 12, 15, 12, 5,
  64. 4, 9, 12, 9, 4,
  65. 2, 4, 5, 4, 2,
  66. };
  67. constexpr double factor = 1.0 / 159;
  68. constexpr double bias = 0.0;
  69. const auto height = input_view.height();
  70. const auto width = input_view.width();
  71. for (std::ptrdiff_t x = 0; x < width; ++x)
  72. {
  73. for (std::ptrdiff_t y = 0; y < height; ++y)
  74. {
  75. double intensity = 0.0;
  76. for (std::ptrdiff_t filter_y = 0; filter_y < filter_height; ++filter_y)
  77. {
  78. for (std::ptrdiff_t filter_x = 0; filter_x < filter_width; ++filter_x)
  79. {
  80. int image_x = x - filter_width / 2 + filter_x;
  81. int image_y = y - filter_height / 2 + filter_y;
  82. if (image_x >= input_view.width() || image_x < 0 ||
  83. image_y >= input_view.height() || image_y < 0)
  84. {
  85. continue;
  86. }
  87. const auto& pixel = input_view(image_x, image_y);
  88. intensity += pixel.at(std::integral_constant<int, 0>{})
  89. * filter[filter_y][filter_x];
  90. }
  91. }
  92. auto& pixel = output_view(gil::point_t(x, y));
  93. pixel = (std::min)((std::max)(int(factor * intensity + bias), 0), 255);
  94. }
  95. }
  96. }
  97. std::vector<gil::point_t> suppress(
  98. gil::gray32f_view_t harris_response,
  99. double harris_response_threshold)
  100. {
  101. std::vector<gil::point_t> corner_points;
  102. for (gil::gray32f_view_t::coord_t y = 1; y < harris_response.height() - 1; ++y)
  103. {
  104. for (gil::gray32f_view_t::coord_t x = 1; x < harris_response.width() - 1; ++x)
  105. {
  106. auto value = [](gil::gray32f_pixel_t pixel) {
  107. return pixel.at(std::integral_constant<int, 0>{});
  108. };
  109. double values[9] = {
  110. value(harris_response(x - 1, y - 1)),
  111. value(harris_response(x, y - 1)),
  112. value(harris_response(x + 1, y - 1)),
  113. value(harris_response(x - 1, y)),
  114. value(harris_response(x, y)),
  115. value(harris_response(x + 1, y)),
  116. value(harris_response(x - 1, y + 1)),
  117. value(harris_response(x, y + 1)),
  118. value(harris_response(x + 1, y + 1))
  119. };
  120. auto maxima = *std::max_element(
  121. values,
  122. values + 9,
  123. [](double lhs, double rhs)
  124. {
  125. return lhs < rhs;
  126. }
  127. );
  128. if (maxima == value(harris_response(x, y))
  129. && std::count(values, values + 9, maxima) == 1
  130. && maxima >= harris_response_threshold)
  131. {
  132. corner_points.emplace_back(x, y);
  133. }
  134. }
  135. }
  136. return corner_points;
  137. }
  138. int main(int argc, char* argv[]) {
  139. if (argc != 5)
  140. {
  141. std::cout << "usage: " << argv[0] << " <input.png> <odd-window-size>"
  142. " <hessian-response-threshold> <output.png>\n";
  143. return -1;
  144. }
  145. std::size_t window_size = std::stoul(argv[2]);
  146. long hessian_determinant_threshold = std::stol(argv[3]);
  147. gil::rgb8_image_t input_image;
  148. gil::read_image(argv[1], input_image, gil::png_tag{});
  149. auto input_view = gil::view(input_image);
  150. auto grayscaled = to_grayscale(input_view);
  151. gil::gray8_image_t smoothed_image(grayscaled.dimensions());
  152. auto smoothed = gil::view(smoothed_image);
  153. apply_gaussian_blur(gil::view(grayscaled), smoothed);
  154. gil::gray16s_image_t x_gradient_image(grayscaled.dimensions());
  155. gil::gray16s_image_t y_gradient_image(grayscaled.dimensions());
  156. auto x_gradient = gil::view(x_gradient_image);
  157. auto y_gradient = gil::view(y_gradient_image);
  158. auto scharr_x = gil::generate_dx_scharr();
  159. gil::detail::convolve_2d(smoothed, scharr_x, x_gradient);
  160. auto scharr_y = gil::generate_dy_scharr();
  161. gil::detail::convolve_2d(smoothed, scharr_y, y_gradient);
  162. gil::gray32f_image_t m11(x_gradient.dimensions());
  163. gil::gray32f_image_t m12_21(x_gradient.dimensions());
  164. gil::gray32f_image_t m22(x_gradient.dimensions());
  165. gil::compute_hessian_entries(
  166. x_gradient,
  167. y_gradient,
  168. gil::view(m11),
  169. gil::view(m12_21),
  170. gil::view(m22)
  171. );
  172. gil::gray32f_image_t hessian_response(x_gradient.dimensions());
  173. auto gaussian_kernel = gil::generate_gaussian_kernel(window_size, 0.84089642);
  174. gil::compute_hessian_responses(
  175. gil::view(m11),
  176. gil::view(m12_21),
  177. gil::view(m22),
  178. gaussian_kernel,
  179. gil::view(hessian_response)
  180. );
  181. auto corner_points = suppress(gil::view(hessian_response), hessian_determinant_threshold);
  182. for (auto point: corner_points) {
  183. input_view(point) = gil::rgb8_pixel_t(0, 0, 0);
  184. input_view(point).at(std::integral_constant<int, 1>{}) = 255;
  185. }
  186. gil::write_view(argv[4], input_view, gil::png_tag{});
  187. }