10 #ifndef BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP
11 #define BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP
13 #include <boost/gil/image_processing/kernel.hpp>
15 #include <boost/gil/image_processing/convolve.hpp>
17 #include <boost/gil/image.hpp>
18 #include <boost/gil/image_view.hpp>
19 #include <boost/gil/algorithm.hpp>
24 namespace boost {
namespace gil {
26 template <
typename SrcView,
typename DstView>
28 SrcView
const& src_view,
29 DstView
const& dst_view,
30 std::size_t kernel_size,
33 boundary_option option = boundary_option::extend_zero
36 gil_function_requires<ImageViewConcept<SrcView>>();
37 gil_function_requires<MutableImageViewConcept<DstView>>();
38 static_assert(color_spaces_are_compatible
40 typename color_space_type<SrcView>::type,
41 typename color_space_type<DstView>::type
42 >::value,
"Source and destination views must have pixels with the same color space");
44 std::vector<float> kernel_values;
45 if (normalize) { kernel_values.resize(kernel_size, 1.0f /
float(kernel_size)); }
46 else { kernel_values.resize(kernel_size, 1.0f); }
48 if (anchor == -1) anchor =
static_cast<int>(kernel_size / 2);
49 kernel_1d<float> kernel(kernel_values.begin(), kernel_size, anchor);
53 pixel<float, typename SrcView::value_type::layout_t>
54 >(src_view, kernel, dst_view, option);
57 template <
typename SrcView,
typename DstView>
59 SrcView
const& src_view,
60 DstView
const& dst_view,
61 std::size_t kernel_size,
63 boundary_option option = boundary_option::extend_zero
66 box_filter(src_view, dst_view, kernel_size, anchor,
true, option);
72 template <
typename SrcView,
typename DstView>
73 void filter_median_impl(SrcView
const& src_view, DstView
const& dst_view, std::size_t kernel_size)
75 std::size_t half_kernel_size = kernel_size / 2;
78 using src_channel_t =
typename channel_type<SrcView>::type;
80 std::vector<src_channel_t> values;
81 values.reserve(kernel_size * kernel_size);
83 for (std::ptrdiff_t y = 0; y < src_view.height(); y++)
85 typename DstView::x_iterator dst_it = dst_view.row_begin(y);
87 for (std::ptrdiff_t x = 0; x < src_view.width(); x++)
89 auto sub_view = subimage_view(
91 x - half_kernel_size, y - half_kernel_size,
95 values.assign(sub_view.begin(), sub_view.end());
97 std::nth_element(values.begin(), values.begin() + (values.size() / 2), values.end());
98 dst_it[x] = values[values.size() / 2];
104 template <
typename SrcView,
typename DstView>
105 void median_filter(SrcView
const& src_view, DstView
const& dst_view, std::size_t kernel_size)
107 static_assert(color_spaces_are_compatible
109 typename color_space_type<SrcView>::type,
110 typename color_space_type<DstView>::type
111 >::value,
"Source and destination views must have pixels with the same color space");
113 std::size_t half_kernel_size = kernel_size / 2;
114 auto extended_img = extend_boundary(
117 boundary_option::extend_constant
119 auto extended_view = subimage_view(
127 for (std::size_t channel = 0; channel < extended_view.num_channels(); channel++)
129 detail::filter_median_impl(
130 nth_channel_view(extended_view, channel),
131 nth_channel_view(dst_view, channel),
auto view(image< Pixel, IsPlanar, Alloc > &img) -> typename image< Pixel, IsPlanar, Alloc >::view_t const &
Returns the non-constant-pixel view of an image.
Definition: image.hpp:565
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition: algorithm.hpp:36