From 27d9d53e6e7324fb17b3fc6ee73087a75738ca3c Mon Sep 17 00:00:00 2001 From: miral Date: Fri, 16 Aug 2019 05:16:57 +0530 Subject: [PATCH] implemented `box_filter` implemented `blur` function tests added for `box_filter` closes #382 --- include/boost/gil/image_processing/filter.hpp | 64 ++++++++++++++++++ test/core/image_processing/CMakeLists.txt | 3 +- test/core/image_processing/Jamfile | 1 + test/core/image_processing/box_filter.cpp | 66 +++++++++++++++++++ 4 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 include/boost/gil/image_processing/filter.hpp create mode 100644 test/core/image_processing/box_filter.cpp diff --git a/include/boost/gil/image_processing/filter.hpp b/include/boost/gil/image_processing/filter.hpp new file mode 100644 index 0000000000..b230c8fd4d --- /dev/null +++ b/include/boost/gil/image_processing/filter.hpp @@ -0,0 +1,64 @@ +// +// Copyright 2019 Miral Shah +// +// Use, modification and distribution are subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#ifndef BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP +#define BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP + +#include +#include + +#include +#include +#include +#include + +namespace boost { namespace gil { + +template +void box_filter( + SrcView const& src_view, + DstView const& dst_view, + std::size_t kernel_size, + long int anchor = -1, + bool normalize=true, + convolve_boundary_option option = convolve_option_extend_zero +) +{ + gil_function_requires>(); + gil_function_requires>(); + static_assert(color_spaces_are_compatible + < + typename color_space_type::type, + typename color_space_type::type + >::value, "Source and destination views must have pixels with the same color space"); + + std::vector kernel_values; + if (normalize) { kernel_values.resize(kernel_size, 1.0f / float(kernel_size)); } + else { kernel_values.resize(kernel_size, 1.0f); } + + if (anchor == -1) anchor = static_cast(kernel_size / 2); + kernel_1d kernel(kernel_values.begin(), kernel_size, anchor); + + convolve_1d>(src_view, kernel, dst_view, option); +} + +template +void blur( + SrcView const& src_view, + DstView const& dst_view, + std::size_t kernel_size, + long int anchor = -1, + convolve_boundary_option option = convolve_option_extend_zero +) +{ + box_filter(src_view, dst_view, kernel_size, anchor, true, option); +} + +}} //namespace boost::gil + +#endif // !BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP diff --git a/test/core/image_processing/CMakeLists.txt b/test/core/image_processing/CMakeLists.txt index 9e75ae4d2e..993361a620 100644 --- a/test/core/image_processing/CMakeLists.txt +++ b/test/core/image_processing/CMakeLists.txt @@ -31,7 +31,8 @@ foreach(_name lanczos_scaling simple_kernels harris - hessian) + hessian + box_filter) set(_test t_core_image_processing_${_name}) set(_target test_core_image_processing_${_name}) diff --git a/test/core/image_processing/Jamfile b/test/core/image_processing/Jamfile index f1c32fc0ac..09edec54d4 100644 --- a/test/core/image_processing/Jamfile +++ b/test/core/image_processing/Jamfile @@ -21,3 +21,4 @@ run lanczos_scaling.cpp ; run simple_kernels.cpp ; run harris.cpp ; run hessian.cpp ; +run box_filter.cpp /boost/test//boost_unit_test_framework : : : shared:BOOST_TEST_DYN_LINK=1 ; diff --git a/test/core/image_processing/box_filter.cpp b/test/core/image_processing/box_filter.cpp new file mode 100644 index 0000000000..9767a48039 --- /dev/null +++ b/test/core/image_processing/box_filter.cpp @@ -0,0 +1,66 @@ +// +// Copyright 2019 Miral Shah +// +// Use, modification and distribution are subject to the Boost Software License, +// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// + +#define BOOST_TEST_MODULE test_image_processing_box_filter +#include "unit_test.hpp" + +#include +#include +#include +#include + + +namespace gil = boost::gil; + +std::uint8_t img[] = +{ + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 255, 0, 0, 0, 255, 0, 0, + 0, 0, 0, 255, 0, 255, 0, 0, 0, + 0, 0, 0, 0, 255, 0, 0, 0, 0, + 0, 0, 0, 255, 0, 255, 0, 0, 0, + 0, 0, 255, 0, 0, 0, 255, 0, 0, + 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, + 0, 28, 28, 28, 0, 28, 28, 28, 0, + 0, 28, 56, 56, 56, 56, 56, 28, 0, + 0, 28, 56, 85, 85, 85, 56, 28, 0, + 0, 0, 56, 85, 141, 85, 56, 0, 0, + 0, 28, 56, 85, 85, 85, 56, 28, 0, + 0, 28, 56, 56, 56, 56, 56, 28, 0, + 0, 28, 28, 28, 0, 28, 28, 28, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +BOOST_AUTO_TEST_SUITE(filter) + +BOOST_AUTO_TEST_CASE(box_filter_with_default_parameters) +{ + 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); + + gil::box_filter(src_view, dst_view, 3); + + gil::gray8c_view_t out_view = + gil::interleaved_view(9, 9, reinterpret_cast(output), 9); + + + BOOST_TEST(gil::equal_pixels(out_view, dst_view)); +} + +BOOST_AUTO_TEST_SUITE_END()