Boost GIL


morphology.hpp
1//
2// Copyright 2021 Prathamesh Tagore <prathameshtagore@gmail.com>
3//
4// Use, modification and distribution are subject to the Boost Software License,
5// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8
9#ifndef BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
10#define BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
11
12#include <boost/gil/image_processing/kernel.hpp>
13#include <boost/gil/gray.hpp>
14#include <boost/gil/image_processing/threshold.hpp>
15
16namespace boost { namespace gil { namespace detail {
17
18enum class morphological_operation
19{
20 dilation,
21 erosion,
22};
23
26
39template <typename SrcView, typename DstView, typename Kernel>
40void morph_impl(SrcView const& src_view, DstView const& dst_view, Kernel const& kernel,
41 morphological_operation identifier)
42{
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)
46 {
47 for (std::ptrdiff_t view_col = 0; view_col < src_view.width(); ++view_col)
48 {
49 target_element = src_view(view_col, view_row);
50 for (std::size_t kernel_row = 0; kernel_row < kernel.size(); ++kernel_row)
51 {
52 flip_ker_row = kernel.size() - 1 - kernel_row; // row index of flipped kernel
53
54 for (std::size_t kernel_col = 0; kernel_col < kernel.size(); ++kernel_col)
55 {
56 flip_ker_col = kernel.size() - 1 - kernel_col; // column index of flipped kernel
57
58 // We ensure that we consider only those pixels which are overlapped
59 // on a non-zero kernel_element as
60 if (kernel.at(flip_ker_row, flip_ker_col) == 0)
61 {
62 continue;
63 }
64 // index of input signal, used for checking boundary
65 row_boundary = view_row + (kernel.center_y() - flip_ker_row);
66 col_boundary = view_col + (kernel.center_x() - flip_ker_col);
67
68 // ignore input samples which are out of bound
69 if (row_boundary >= 0 && row_boundary < src_view.height() &&
70 col_boundary >= 0 && col_boundary < src_view.width())
71 {
72
73 if (identifier == morphological_operation::dilation)
74 {
75 target_element =
76 (std::max)(src_view(col_boundary, row_boundary)[0], target_element);
77 }
78 else if (identifier == morphological_operation::erosion)
79 {
80 target_element =
81 (std::min)(src_view(col_boundary, row_boundary)[0], target_element);
82 }
83 }
84 }
85 }
86 dst_view(view_col, view_row) = target_element;
87 }
88 }
89}
90
102template <typename SrcView, typename DstView, typename Kernel>
103void morph(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat,
104 morphological_operation identifier)
105{
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>>();
109
110 gil_function_requires<ColorSpacesCompatibleConcept<typename color_space_type<SrcView>::type,
111 typename color_space_type<DstView>::type>>();
112
113 gil::image<typename DstView::value_type> intermediate_img(src_view.dimensions());
114
115 for (std::size_t i = 0; i < src_view.num_channels(); i++)
116 {
117 morph_impl(nth_channel_view(src_view, i), nth_channel_view(view(intermediate_img), i),
118 ker_mat, identifier);
119 }
120 copy_pixels(view(intermediate_img), dst_view);
121}
122
131template <typename SrcView, typename DiffView>
132void difference_impl(SrcView const& src_view1, SrcView const& src_view2, DiffView const& diff_view)
133{
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);
138}
139
147template <typename SrcView, typename DiffView>
148void difference(SrcView const& src_view1, SrcView const& src_view2, DiffView const& diff_view)
149{
150 gil_function_requires<ImageViewConcept<SrcView>>();
151 gil_function_requires<MutableImageViewConcept<DiffView>>();
152
153 gil_function_requires<ColorSpacesCompatibleConcept<
154 typename color_space_type<SrcView>::type, typename color_space_type<DiffView>::type>>();
155
156 for (std::size_t i = 0; i < src_view1.num_channels(); i++)
157 {
158 difference_impl(nth_channel_view(src_view1, i), nth_channel_view(src_view2, i),
159 nth_channel_view(diff_view, i));
160 }
161}
162} // namespace detail
163
176template <typename SrcView, typename IntOpView, typename Kernel>
177void dilate(SrcView const& src_view, IntOpView const& int_op_view, Kernel const& ker_mat,
178 int iterations)
179{
180 copy_pixels(src_view, int_op_view);
181 for (int i = 0; i < iterations; ++i)
182 morph(int_op_view, int_op_view, ker_mat, detail::morphological_operation::dilation);
183}
184
197template <typename SrcView, typename IntOpView, typename Kernel>
198void erode(SrcView const& src_view, IntOpView const& int_op_view, Kernel const& ker_mat,
199 int iterations)
200{
201 copy_pixels(src_view, int_op_view);
202 for (int i = 0; i < iterations; ++i)
203 morph(int_op_view, int_op_view, ker_mat, detail::morphological_operation::erosion);
204}
205
215template <typename SrcView, typename IntOpView, typename Kernel>
216void opening(SrcView const& src_view, IntOpView const& int_op_view, Kernel const& ker_mat)
217{
218 erode(src_view, int_op_view, ker_mat, 1);
219 dilate(int_op_view, int_op_view, ker_mat, 1);
220}
221
232template <typename SrcView, typename IntOpView, typename Kernel>
233void closing(SrcView const& src_view, IntOpView const& int_op_view, Kernel const& ker_mat)
234{
235 dilate(src_view, int_op_view, ker_mat, 1);
236 erode(int_op_view, int_op_view, ker_mat, 1);
237}
238
250template <typename SrcView, typename DstView, typename Kernel>
251void morphological_gradient(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
252{
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);
258 difference(view(int_dilate), view(int_erode), dst_view);
259}
260
270template <typename SrcView, typename DstView, typename Kernel>
271void top_hat(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
272{
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);
276 difference(src_view, view(int_opening), dst_view);
277}
278
288template <typename SrcView, typename DstView, typename Kernel>
289void black_hat(SrcView const& src_view, DstView const& dst_view, Kernel const& ker_mat)
290{
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);
294 difference(view(int_closing), src_view, dst_view);
295}
297}} // namespace boost::gil
298#endif // BOOST_GIL_IMAGE_PROCESSING_MORPHOLOGY_HPP
container interface over image view. Models ImageConcept, PixelBasedConcept
Definition image.hpp:43
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
void morph_impl(SrcView const &src_view, DstView const &dst_view, Kernel const &kernel, morphological_operation identifier)
Implements morphological operations at pixel level.This function compares neighbouring pixel values a...
Definition morphology.hpp:40
void morph(SrcView const &src_view, DstView const &dst_view, Kernel const &ker_mat, morphological_operation identifier)
Checks feasibility of the desired operation and passes parameter values to the function morph_impl al...
Definition morphology.hpp:103
void difference_impl(SrcView const &src_view1, SrcView const &src_view2, DiffView const &diff_view)
Calculates the difference between pixel values of first image_view and second image_view.
Definition morphology.hpp:132
void difference(SrcView const &src_view1, SrcView const &src_view2, DiffView const &diff_view)
Passes parameter values to the function 'difference_impl' alongwith individual channel views of input...
Definition morphology.hpp:148
BOOST_FORCEINLINE void copy_pixels(const View1 &src, const View2 &dst)
std::copy for image views
Definition algorithm.hpp:292
defined(BOOST_NO_CXX17_HDR_MEMORY_RESOURCE)
Definition algorithm.hpp:36
Two color spaces are compatible if they are the same.
Definition color.hpp:61
Definition color_convert.hpp:31