9 #ifndef BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
10 #define BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
12 #include <boost/gil/image_processing/kernel.hpp>
13 #include <boost/gil/gray.hpp>
14 #include <boost/gil/image_processing/threshold.hpp>
16 namespace boost {
namespace gil {
namespace detail {
18 enum class morphological_operation
39 template <
typename SrcView,
typename DstView,
typename Kernel>
40 void morph_impl(SrcView
const& src_view, DstView
const& dst_view, Kernel
const& kernel,
41 morphological_operation identifier)
43 std::ptrdiff_t flip_ker_row, flip_ker_col, row_boundary, col_boundary;
45 for (std::ptrdiff_t view_row = 0; view_row < src_view.height(); ++view_row)
47 for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
49 target_element = src_view(view_col, view_row);
50 for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row)
52 flip_ker_row = kernel.size() - 1 - kernel_row;
54 for (std::size_t kernel_col = 0; kernel_col < kernel.size(); ++kernel_col)
56 flip_ker_col = kernel.size() - 1 - kernel_col;
60 if (kernel.at(flip_ker_row, flip_ker_col) == 0)
65 row_boundary = view_row + (kernel.center_y() - flip_ker_row);
66 col_boundary = view_col + (kernel.center_x() - flip_ker_col);
69 if (row_boundary >= 0 && row_boundary < src_view.height() &&
70 col_boundary >= 0 && col_boundary < src_view.width())
73 if (identifier == morphological_operation::dilation)
76 (std::max)(src_view(col_boundary, row_boundary)[0], target_element);
78 else if (identifier == morphological_operation::erosion)
81 (std::min)(src_view(col_boundary, row_boundary)[0], target_element);
86 dst_view(view_col, view_row) = target_element;
102 template <
typename SrcView,
typename DstView,
typename Kernel>
103 void morph(SrcView
const& src_view, DstView
const& dst_view, Kernel
const& ker_mat,
104 morphological_operation identifier)
106 BOOST_ASSERT(ker_mat.size() != 0 && src_view.dimensions() == dst_view.dimensions());
107 gil_function_requires<ImageViewConcept<SrcView>>();
108 gil_function_requires<MutableImageViewConcept<DstView>>();
110 gil_function_requires<ColorSpacesCompatibleConcept<typename color_space_type<SrcView>::type,
111 typename color_space_type<DstView>::type>>();
115 for (std::size_t i = 0; i < src_view.num_channels(); i++)
118 ker_mat, identifier);
131 template <
typename SrcView,
typename DiffView>
132 void difference_impl(SrcView
const& src_view1, SrcView
const& src_view2, DiffView
const& diff_view)
134 for (std::ptrdiff_t view_row = 0; view_row < src_view1.height(); ++view_row)
135 for (std::ptrdiff_t view_col = 0; view_col < src_view1.width(); ++view_col)
136 diff_view(view_col, view_row) =
137 src_view1(view_col, view_row) - src_view2(view_col, view_row);
147 template <
typename SrcView,
typename DiffView>
148 void difference(SrcView
const& src_view1, SrcView
const& src_view2, DiffView
const& diff_view)
150 gil_function_requires<ImageViewConcept<SrcView>>();
151 gil_function_requires<MutableImageViewConcept<DiffView>>();
154 typename color_space_type<SrcView>::type,
typename color_space_type<DiffView>::type>>();
156 for (std::size_t i = 0; i < src_view1.num_channels(); i++)
176 template <
typename SrcView,
typename IntOpView,
typename Kernel>
177 void dilate(SrcView
const& src_view, IntOpView
const& int_op_view, Kernel
const& ker_mat,
181 for (
int i = 0; i < iterations; ++i)
182 morph(int_op_view, int_op_view, ker_mat, detail::morphological_operation::dilation);
197 template <
typename SrcView,
typename IntOpView,
typename Kernel>
198 void erode(SrcView
const& src_view, IntOpView
const& int_op_view, Kernel
const& ker_mat,
202 for (
int i = 0; i < iterations; ++i)
203 morph(int_op_view, int_op_view, ker_mat, detail::morphological_operation::erosion);
215 template <
typename SrcView,
typename IntOpView,
typename Kernel>
216 void opening(SrcView
const& src_view, IntOpView
const& int_op_view, Kernel
const& ker_mat)
218 erode(src_view, int_op_view, ker_mat, 1);
219 dilate(int_op_view, int_op_view, ker_mat, 1);
232 template <
typename SrcView,
typename IntOpView,
typename Kernel>
233 void closing(SrcView
const& src_view, IntOpView
const& int_op_view, Kernel
const& ker_mat)
235 dilate(src_view, int_op_view, ker_mat, 1);
236 erode(int_op_view, int_op_view, ker_mat, 1);
250 template <
typename SrcView,
typename DstView,
typename Kernel>
251 void morphological_gradient(SrcView
const& src_view, DstView
const& dst_view, Kernel
const& ker_mat)
253 using namespace boost::gil;
254 gil::image<typename DstView::value_type> int_dilate(src_view.dimensions()),
255 int_erode(src_view.dimensions());
256 dilate(src_view,
view(int_dilate), ker_mat, 1);
257 erode(src_view,
view(int_erode), ker_mat, 1);
270 template <
typename SrcView,
typename DstView,
typename Kernel>
271 void top_hat(SrcView
const& src_view, DstView
const& dst_view, Kernel
const& ker_mat)
273 using namespace boost::gil;
274 gil::image<typename DstView::value_type> int_opening(src_view.dimensions());
275 opening(src_view,
view(int_opening), ker_mat);
288 template <
typename SrcView,
typename DstView,
typename Kernel>
289 void black_hat(SrcView
const& src_view, DstView
const& dst_view, Kernel
const& ker_mat)
291 using namespace boost::gil;
292 gil::image<typename DstView::value_type> int_closing(src_view.dimensions());
293 closing(src_view,
view(int_closing), ker_mat);
298 #endif // BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP