-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented `blur` function tests added for `box_filter` closes #382
- Loading branch information
1 parent
f6c502b
commit ec30419
Showing
3 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// Copyright 2019 Miral Shah <miralshah2211@gmail.com> | ||
// | ||
// 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 <cstddef> | ||
#include <vector> | ||
|
||
#include <boost/gil/image.hpp> | ||
#include <boost/gil/image_view.hpp> | ||
#include <boost/gil/extension/numeric/kernel.hpp> | ||
#include <boost/gil/extension/numeric/convolve.hpp> | ||
|
||
namespace boost { namespace gil { | ||
|
||
template <typename SrcView, typename DstView> | ||
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<ImageViewConcept<SrcView>>(); | ||
gil_function_requires<MutableImageViewConcept<DstView>>(); | ||
static_assert(color_spaces_are_compatible | ||
< | ||
typename color_space_type<SrcView>::type, | ||
typename color_space_type<DstView>::type | ||
>::value, "Source and destination views must have pixels with the same color space"); | ||
|
||
std::vector<float> 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 = kernel_size / 2; | ||
kernel_1d<float> kernel(kernel_values.begin(), kernel_size, anchor); | ||
|
||
convolve_1d<pixel<float, typename SrcView::value_type::layout_t>>(src_view, kernel, dst_view); | ||
} | ||
|
||
template <typename SrcView, typename DstView> | ||
void blur( | ||
SrcView const& src_view, | ||
DstView const& dst_view, | ||
std::size_t kernel_size, | ||
std::size_t anchor = kernel_size / 2, | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// | ||
// Copyright 2019 Miral Shah <miralshah2211@gmail.com> | ||
// | ||
// 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 <boost/gil/image_view.hpp> | ||
#include <boost/gil/algorithm.hpp> | ||
#include <boost/gil/gray.hpp> | ||
#include <boost/core/lightweight_test.hpp> | ||
#include <boost/gil/image_processing/filter.hpp> | ||
|
||
|
||
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<const gil::gray8_pixel_t*>(img), 9); | ||
|
||
gil::image<gil::gray8_pixel_t> temp_img(src_view.width(), src_view.height()); | ||
typename gil::image<gil::gray8_pixel_t>::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<const gil::gray8_pixel_t*>(output), 9); | ||
|
||
|
||
BOOST_TEST(gil::equal_pixels(out_view, dst_view)); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |