9 #ifndef BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP 10 #define BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP 12 #include <boost/gil/extension/numeric/algorithm.hpp> 13 #include <boost/gil/extension/numeric/kernel.hpp> 14 #include <boost/gil/extension/numeric/convolve.hpp> 16 #include <boost/gil/image.hpp> 17 #include <boost/gil/image_view.hpp> 25 namespace boost {
namespace gil {
27 template <
typename SrcView,
typename DstView>
29 SrcView
const& src_view,
30 DstView
const& dst_view,
31 std::size_t kernel_size,
34 boundary_option option = boundary_option::extend_zero
37 gil_function_requires<ImageViewConcept<SrcView>>();
38 gil_function_requires<MutableImageViewConcept<DstView>>();
39 static_assert(color_spaces_are_compatible
41 typename color_space_type<SrcView>::type,
42 typename color_space_type<DstView>::type
43 >::value,
"Source and destination views must have pixels with the same color space");
45 std::vector<float> kernel_values;
46 if (normalize) { kernel_values.resize(kernel_size, 1.0f /
float(kernel_size)); }
47 else { kernel_values.resize(kernel_size, 1.0f); }
49 if (anchor == -1) anchor = static_cast<int>(kernel_size / 2);
50 kernel_1d<float> kernel(kernel_values.begin(), kernel_size, anchor);
54 pixel<float, typename SrcView::value_type::layout_t>
55 >(src_view, kernel, dst_view, option);
58 template <
typename SrcView,
typename DstView>
60 SrcView
const& src_view,
61 DstView
const& dst_view,
62 std::size_t kernel_size,
64 boundary_option option = boundary_option::extend_zero
67 box_filter(src_view, dst_view, kernel_size, anchor,
true, option);
73 template <
typename SrcView,
typename DstView>
74 void filter_median_impl(SrcView
const& src_view, DstView
const& dst_view, std::size_t kernel_size)
76 std::size_t half_kernel_size = kernel_size / 2;
79 using src_channel_t =
typename channel_type<SrcView>::type;
81 std::vector<src_channel_t> values;
82 values.reserve(kernel_size * kernel_size);
84 for (std::ptrdiff_t y = 0; y < src_view.height(); y++)
86 typename DstView::x_iterator dst_it = dst_view.row_begin(y);
88 for (std::ptrdiff_t x = 0; x < src_view.width(); x++)
90 auto sub_view = subimage_view(
92 x - half_kernel_size, y - half_kernel_size,
96 values.assign(sub_view.begin(), sub_view.end());
98 std::nth_element(values.begin(), values.begin() + (values.size() / 2), values.end());
99 dst_it[x] = values[values.size() / 2];
105 template <
typename SrcView,
typename DstView>
106 void median_filter(SrcView
const& src_view, DstView
const& dst_view, std::size_t kernel_size)
108 static_assert(color_spaces_are_compatible
110 typename color_space_type<SrcView>::type,
111 typename color_space_type<DstView>::type
112 >::value,
"Source and destination views must have pixels with the same color space");
114 std::size_t half_kernel_size = kernel_size / 2;
115 auto extended_img = extend_boundary(
118 boundary_option::extend_constant
120 auto extended_view = subimage_view(
128 for (std::size_t channel = 0; channel < extended_view.num_channels(); channel++)
130 detail::filter_median_impl(
131 nth_channel_view(extended_view, channel),
132 nth_channel_view(dst_view, channel),
140 #endif // !BOOST_GIL_IMAGE_PROCESSING_FILTER_HPP const image< Pixel, IsPlanar, Alloc >::view_t & view(image< Pixel, IsPlanar, Alloc > &img)
Returns the non-constant-pixel view of an image.
Definition: image.hpp:538