8 #ifndef BOOST_GIL_IMAGE_PROCESSING_THRESHOLD_HPP 9 #define BOOST_GIL_IMAGE_PROCESSING_THRESHOLD_HPP 12 #include <boost/gil/image.hpp> 14 namespace boost {
namespace gil {
21 typename SourceChannelT,
22 typename ResultChannelT,
27 void threshold_impl(SrcView
const& src_view, DstView
const& dst_view, Operator
const& threshold_op)
29 gil_function_requires<ImageViewConcept<SrcView>>();
30 gil_function_requires<MutableImageViewConcept<DstView>>();
31 gil_function_requires<ColorSpacesCompatibleConcept
33 typename color_space_type<SrcView>::type,
34 typename color_space_type<DstView>::type>
36 static_assert(color_spaces_are_compatible
38 typename color_space_type<SrcView>::type,
39 typename color_space_type<DstView>::type
40 >::value,
"Source and destination views must have pixels with the same color space");
43 for (std::ptrdiff_t y = 0; y < src_view.height(); y++)
45 typename SrcView::x_iterator src_it = src_view.row_begin(y);
46 typename DstView::x_iterator dst_it = dst_view.row_begin(y);
48 for (std::ptrdiff_t x = 0; x < src_view.width(); x++)
50 static_transform(src_it[x], dst_it[x], threshold_op);
57 enum class threshold_direction { regular, inverse };
58 enum class threshold_optimal_value { otsu, triangle };
59 enum class threshold_truncate_mode { threshold, zero };
68 template <
typename SrcView,
typename DstView>
71 SrcView
const& src_view,
72 DstView
const& dst_view,
73 typename channel_type<DstView>::type threshold_value,
74 typename channel_type<DstView>::type max_value,
75 threshold_direction direction = threshold_direction::regular
79 typedef typename channel_type<SrcView>::type source_channel_t;
80 typedef typename channel_type<DstView>::type result_channel_t;
82 if (direction == threshold_direction::regular)
84 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
85 [threshold_value, max_value](source_channel_t px) -> result_channel_t {
86 return px > threshold_value ? max_value : 0;
91 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
92 [threshold_value, max_value](source_channel_t px) -> result_channel_t {
93 return px > threshold_value ? 0 : max_value;
105 template <
typename SrcView,
typename DstView>
106 void threshold_binary
108 SrcView
const& src_view,
109 DstView
const& dst_view,
110 typename channel_type<DstView>::type threshold_value,
111 threshold_direction direction = threshold_direction::regular
114 typedef typename channel_type<DstView>::type result_channel_t;
115 result_channel_t max_value = std::numeric_limits<result_channel_t>::max();
116 threshold_binary(src_view, dst_view, threshold_value, max_value, direction);
130 template <
typename SrcView,
typename DstView>
131 void threshold_truncate
133 SrcView
const& src_view,
134 DstView
const& dst_view,
135 typename channel_type<DstView>::type threshold_value,
136 threshold_truncate_mode mode = threshold_truncate_mode::threshold,
137 threshold_direction direction = threshold_direction::regular
141 typedef typename channel_type<SrcView>::type source_channel_t;
142 typedef typename channel_type<DstView>::type result_channel_t;
144 std::function<result_channel_t(source_channel_t)> threshold_logic;
146 if (mode == threshold_truncate_mode::threshold)
148 if (direction == threshold_direction::regular)
150 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
151 [threshold_value](source_channel_t px) -> result_channel_t {
152 return px > threshold_value ? threshold_value : px;
157 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
158 [threshold_value](source_channel_t px) -> result_channel_t {
159 return px > threshold_value ? px : threshold_value;
165 if (direction == threshold_direction::regular)
167 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
168 [threshold_value](source_channel_t px) -> result_channel_t {
169 return px > threshold_value ? px : 0;
174 detail::threshold_impl<source_channel_t, result_channel_t>(src_view, dst_view,
175 [threshold_value](source_channel_t px) -> result_channel_t {
176 return px > threshold_value ? 0 : px;
184 #endif //BOOST_GIL_IMAGE_PROCESSING_THRESHOLD_HPP Definition: algorithm.hpp:30