diff --git a/include/boost/gil/extension/numeric/convolve.hpp b/include/boost/gil/extension/numeric/convolve.hpp index b73c8dfc7c..1331640792 100644 --- a/include/boost/gil/extension/numeric/convolve.hpp +++ b/include/boost/gil/extension/numeric/convolve.hpp @@ -375,6 +375,8 @@ void convolve_1d( /// \brief Provides functionality for performing correlation between the kernel and buffer. /// Kernel size is to be provided through constructor for all instances. /// This correlator is specifically used in 2D correlation. +/// \tparam PixelAccum Specifies tha data type which will be used for creating buffer container +/// utilized for holding source image pixels after applying appropriate boundary manipulations. template class correlator_n_2d { @@ -399,6 +401,9 @@ class correlator_n_2d /// \brief Provides functionality for performing correlation between the kernel and buffer. /// Kernel size is a template parameter and must be compulsorily specified while using. /// This correlator is specifically used in 2D correlation. +/// \tparam kernel_dimension Stores vertical/horizontal length of kernel used for correlation. +/// \tparam PixelAccum Specifies tha data type which will be used for creating buffer container +/// utilized for holding source image pixels after applying appropriate boundary manipulations. template struct correlator_k_2d { @@ -439,7 +444,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, long unsigned int const left_extrapolation_size = kernel.left_size(); long unsigned int const right_extrapolation_size = kernel.right_size(); - bool explicit_fill = 1; + bool explicit_fill = true; long unsigned int col = 0, row = 0; PixelAccum zero_pixel; pixel_zeros_t()(zero_pixel); @@ -456,7 +461,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, for (row = upper_extrapolation_size; row < src_view.height() - lower_extrapolation_size; ++row) { - if (row - upper_extrapolation_size) + if (row - upper_extrapolation_size > 0) { for (col = 0; col < src_view.width(); ++col) { @@ -567,7 +572,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, } else if (option == boundary_option::extend_reflection) { - explicit_fill = 0; + explicit_fill = false; long unsigned int row_bound = kernel.size() - upper_extrapolation_size > upper_extrapolation_size ? kernel.size() - upper_extrapolation_size : upper_extrapolation_size; @@ -660,7 +665,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, } } - if (explicit_fill) + if (explicit_fill == true) { for (col = 0; col < src_view.width(); ++col) { @@ -673,7 +678,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, for (row = 0; row < src_view.height() - lower_extrapolation_size; ++row) { - if (row) + if (row > 0) { for (long unsigned int temp_col = 0; temp_col < left_extrapolation_size; ++temp_col) { @@ -854,7 +859,7 @@ void correlate_2d_impl(SrcView src_view, Kernel kernel, DstView dst_view, template bool separate(Kernel const kernel, Container_1d& sep_ker_vertical, Container_1d& sep_ker_horizontal) { - bool is_rank_1 = 1; + bool is_rank_1 = true; sep_ker_vertical[0] = 1; for (std::size_t row = 1; row < kernel.size(); ++row) { @@ -867,14 +872,14 @@ bool separate(Kernel const kernel, Container_1d& sep_ker_vertical, Container_1d& auto transformed_elem = mul_factor * kernel.at(col, 0); if (transformed_elem != kernel.at(col, row)) { - is_rank_1 = 0; + is_rank_1 = false; break; } } - if (is_rank_1 == 0) + if (is_rank_1 == false) break; } - if (is_rank_1) + if (is_rank_1 == true) { for (std::size_t col = 0; col < kernel.size(); ++col) sep_ker_horizontal[col] = kernel.at(col, 0); @@ -913,7 +918,7 @@ void correlate_2d(SrcView src_view, Kernel kernel, DstView dst_view, std::vector sep_ker_vertical(kernel.size()); std::vector sep_ker_horizontal(kernel.size()); - if (detail::separate(kernel, sep_ker_vertical, sep_ker_horizontal)) + if (detail::separate(kernel, sep_ker_vertical, sep_ker_horizontal) == true) { kernel_1d ver_kernel(sep_ker_vertical.begin(), kernel.size(), kernel.center_y()); @@ -960,7 +965,7 @@ void correlate_2d_fixed(SrcView src_view, Kernel kernel, DstView dst_view, std::vector sep_ker_vertical(kernel.size()); std::vector sep_ker_horizontal(kernel.size()); - if (detail::separate(kernel, sep_ker_vertical, sep_ker_horizontal)) + if (detail::separate(kernel, sep_ker_vertical, sep_ker_horizontal) == true) { kernel_1d_fixed ver_kernel( sep_ker_vertical.begin(), kernel.center_y()); diff --git a/test/extension/numeric/convolve_2d.cpp b/test/extension/numeric/convolve_2d.cpp index 21033a83bc..fbce6d73fc 100644 --- a/test/extension/numeric/convolve_2d.cpp +++ b/test/extension/numeric/convolve_2d.cpp @@ -7,11 +7,13 @@ // #include #include + #include -#include + #include namespace gil = boost::gil; + std::uint8_t img[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -24,6 +26,7 @@ std::uint8_t img[] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; + std::uint8_t output[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, @@ -41,9 +44,11 @@ void test_convolve_2d_with_normalized_mean_filter() { gil::gray8c_view_t src_view = gil::interleaved_view(9, 9, reinterpret_cast(img), 9); + gil::image temp_img(src_view.width(), src_view.height()); typename gil::image::view_t temp_view = view(temp_img); gil::gray8_view_t dst_view(temp_view); + std::vector v(9, 1.0f / 9.0f); gil::detail::kernel_2d kernel(v.begin(), v.size(), 1, 1); @@ -51,333 +56,13 @@ void test_convolve_2d_with_normalized_mean_filter() gil::gray8c_view_t out_view = gil::interleaved_view(9, 9, reinterpret_cast(output), 9); - BOOST_TEST(gil::equal_pixels(out_view, dst_view)); -} - -template -void print_gil_gray_image(GrayImageView src_view) -{ - std::cout << "Gil Image = \n"; - for (std::ptrdiff_t row = 0; row < src_view.height(); ++row) - { - for (std::ptrdiff_t col = 0; col < src_view.width(); ++col) - std::cout << static_cast(static_cast(src_view(col, row))) << " "; - std::cout << "\n"; - } -} - -void test_boundary_option_extend_zero() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(9, 9), dst_rgb_fixed(9, 9), exp_dst_rgb(9, 9); - std::vector vec(25, 1.0f); - vec[12] = 0; - - gil::read_image("Correlation_Test_Images/Extend_Zero/in_extend_zero_gray.png", src_gray, gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Extend_Zero/in_extend_zero_rgb.png", src_rgb, gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray), - gil::boundary_option::extend_zero); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::extend_zero); - - gil::correlate_2d(gil::view(src_rgb), kernel, gil::view(dst_rgb), - gil::boundary_option::extend_zero); - gil::correlate_2d_fixed(gil::view(src_rgb), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::extend_zero); - - std::string exp_gray = "Correlation_Test_Images/Extend_Zero/exp_out_extend_zero_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Extend_Zero/exp_out_extend_zero_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_boundary_option_extend_constant() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(9, 9), dst_rgb_fixed(9, 9), exp_dst_rgb(9, 9); - std::vector vec(25, 1.0f); - vec[12] = 0; - - gil::read_image("Correlation_Test_Images/Extend_Constant/in_extend_constant_gray.png", src_gray, gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Extend_Constant/in_extend_constant_rgb.png", src_rgb, gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray), - gil::boundary_option::extend_constant); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::extend_constant); - - gil::correlate_2d(gil::view(src_rgb), kernel, gil::view(dst_rgb), - gil::boundary_option::extend_constant); - gil::correlate_2d_fixed(gil::view(src_rgb), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::extend_constant); - - std::string exp_gray = "Correlation_Test_Images/Extend_Constant/exp_out_extend_constant_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Extend_Constant/exp_out_extend_constant_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_boundary_option_extend_reflection() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(9, 9), dst_rgb_fixed(9, 9), exp_dst_rgb(9, 9); - std::vector vec(25, 1.0f); - vec[12] = 0; - - gil::read_image("Correlation_Test_Images/Extend_Reflection/in_extend_reflection_gray.png", src_gray, - gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Extend_Reflection/in_extend_reflection_rgb.png", src_rgb, - gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray), - gil::boundary_option::extend_reflection); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::extend_reflection); - - gil::correlate_2d(gil::view(src_rgb), kernel, gil::view(dst_rgb), - gil::boundary_option::extend_reflection); - gil::correlate_2d_fixed(gil::view(src_rgb), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::extend_reflection); - - std::string exp_gray = "Correlation_Test_Images/Extend_Reflection/exp_out_extend_reflection_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Extend_Reflection/exp_out_extend_reflection_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_boundary_option_extend_padded() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(5, 5), dst_gray_fixed(5, 5), exp_dst_gray(5, 5); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(5, 5), dst_rgb_fixed(5, 5), exp_dst_rgb(5, 5); - std::vector vec(9, 1.0f); - vec[4] = 0; - - gil::read_image("Correlation_Test_Images/Extend_Padded/in_extend_padded_gray.png", src_gray, - gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Extend_Padded/in_extend_padded_rgb.png", src_rgb, - gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 3; ++x) - { - for (std::ptrdiff_t y = 0; y < 3; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d( - gil::subimage_view(gil::view(src_gray), 5, 5, 5, 5), kernel, - gil::view(dst_gray), gil::boundary_option::extend_padded); - gil::correlate_2d_fixed( - gil::subimage_view(gil::view(src_gray), 5, 5, 5, 5), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::extend_padded); - - gil::correlate_2d( - gil::subimage_view(gil::view(src_rgb), 2, 2, 5, 5), kernel, - gil::view(dst_rgb), gil::boundary_option::extend_padded); - gil::correlate_2d_fixed( - gil::subimage_view(gil::view(src_rgb), 2, 2, 5, 5), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::extend_padded); - - std::string exp_gray = "Correlation_Test_Images/Extend_Padded/exp_out_extend_padded_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Extend_Padded/exp_out_extend_padded_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_boundary_option_output_zero() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(9, 9), dst_rgb_fixed(9, 9), exp_dst_rgb(9, 9); - std::vector vec(25, 1.0f); - vec[12] = 0; - - gil::read_image("Correlation_Test_Images/Output_Zero/in_output_zero_gray.png", src_gray, - gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Output_Zero/in_output_zero_rgb.png", src_rgb, - gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray), - gil::boundary_option::output_zero); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::output_zero); - - gil::correlate_2d(gil::view(src_rgb), kernel, gil::view(dst_rgb), - gil::boundary_option::output_zero); - gil::correlate_2d_fixed(gil::view(src_rgb), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::output_zero); - - std::string exp_gray = "Correlation_Test_Images/Output_Zero/exp_out_output_zero_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Output_Zero/exp_out_output_zero_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_boundary_option_output_ignore() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - gil::rgb8_image_t src_rgb(9, 9), dst_rgb(9, 9), dst_rgb_fixed(9, 9), exp_dst_rgb(9, 9); - std::vector vec(25, 1.0f); - vec[12] = 0; - - gil::read_image("Correlation_Test_Images/Output_Ignore/in_output_ignore_gray.png", src_gray, - gil::png_tag{}); - gil::read_image("Correlation_Test_Images/Output_Ignore/in_output_ignore_rgb.png", src_rgb, - gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray), - gil::boundary_option::output_ignore); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed), gil::boundary_option::output_ignore); - - gil::correlate_2d(gil::view(src_rgb), kernel, gil::view(dst_rgb), - gil::boundary_option::output_ignore); - gil::correlate_2d_fixed(gil::view(src_rgb), kernel_fixed, - gil::view(dst_rgb_fixed), gil::boundary_option::output_ignore); - - std::string exp_gray = "Correlation_Test_Images/Output_Ignore/exp_out_output_ignore_gray_"; - std::string exp_rgb = "Correlation_Test_Images/Output_Ignore/exp_out_output_ignore_rgb_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - gil::read_image(exp_rgb + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_rgb, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb), gil::view(exp_dst_rgb))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_rgb_fixed), gil::view(exp_dst_rgb))); - } - } -} - -void test_separable_correlation() -{ - gil::gray8_image_t src_gray(15, 15), dst_gray(15, 15), dst_gray_fixed(15, 15), exp_dst_gray(15, 15); - std::vector vec(25, 1.0f); - - gil::read_image("Correlation_Test_Images/Separable_Correlation/in_separable_correlation_gray.png", src_gray, - gil::png_tag{}); - - for (std::ptrdiff_t x = 0; x < 5; ++x) - { - for (std::ptrdiff_t y = 0; y < 5; ++y) - { - gil::detail::kernel_2d kernel(vec.begin(), vec.size(), y, x); - gil::detail::kernel_2d_fixed kernel_fixed(vec.cbegin(), y, x); - gil::correlate_2d(gil::view(src_gray), kernel, gil::view(dst_gray)); - gil::correlate_2d_fixed(gil::view(src_gray), kernel_fixed, - gil::view(dst_gray_fixed)); - - std::string exp_gray = - "Correlation_Test_Images/Separable_Correlation/exp_out_separable_correlation_gray_"; - - gil::read_image(exp_gray + (char)('0' + x) + '_' + (char)('0' + y) + ".png", exp_dst_gray, - gil::png_tag{}); - - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray), gil::view(exp_dst_gray))); - BOOST_TEST(gil::equal_pixels(gil::view(dst_gray_fixed), gil::view(exp_dst_gray))); - } - } + BOOST_TEST(gil::equal_pixels(out_view, dst_view)); } - int main() { test_convolve_2d_with_normalized_mean_filter(); - test_boundary_option_extend_zero(); - test_boundary_option_extend_constant(); - test_boundary_option_extend_reflection(); - test_boundary_option_extend_padded(); - test_boundary_option_output_zero(); - test_boundary_option_output_ignore(); - test_separable_correlation(); return ::boost::report_errors(); }