27 #ifndef MLN_LABELING_COLORIZE_HH
28 # define MLN_LABELING_COLORIZE_HH
34 # include <mln/core/concept/image.hh>
35 # include <mln/fun/i2v/array.hh>
36 # include <mln/value/rgb8.hh>
37 # include <mln/literal/black.hh>
38 # include <mln/data/transform.hh>
39 # include <mln/data/compute.hh>
40 # include <mln/accu/stat/max.hh>
41 # include <mln/util/array.hh>
42 # include <mln/util/set.hh>
43 # include <mln/value/next.hh>
74 template <
typename V,
typename L>
78 const mln_value(L)& nlabels);
89 template <typename V, typename L>
92 const
Image<L>& labeled_image);
101 template <typename L>
102 mln_ch_value(L, mln::value::
rgb8)
104 const mln_value(L)& nlabels);
107 # ifndef MLN_INCLUDE_ONLY
109 # ifndef MLN_WO_GLOBAL_VARS
117 # endif // ! MLN_WO_GLOBAL_VARS
123 unsigned random_number()
132 template <
typename V>
133 V random_color(
const V&);
136 template <
typename RGB>
138 random_color_rgb(
const RGB&)
142 static util::array<util::set<unsigned> >
146 unsigned red, green, blue;
151 red = random_number();
154 while (red_[red - colorize_::min_value].nelements() == nelements
155 && ntries < nelements);
157 if (ntries == nelements)
159 mln_trace_warning(
"labeling::colorize - Can't find a new unique color. Returning black.");
165 green = random_number();
166 while (red_[red - colorize_::min_value].
has(green)
167 || green_[green - colorize_::min_value].nelements() == nelements);
168 red_[red - colorize_::min_value].insert(green);
171 blue = random_number();
172 while (green_[green - colorize_::min_value].
has(blue));
173 green_[green - colorize_::min_value].insert(blue);
175 return RGB(red, green, blue);
178 template <
unsigned n>
182 return random_color_rgb(v);
186 # ifdef MLN_VALUE_QT_RGB32_HH
192 return random_color_rgb(v);
195 # endif // ! MLN_VALUE_QT_RGB32_HH
199 template <
typename V,
typename L>
203 const Image<L>& input,
204 const mln_value(L)& nlabels)
206 mln_trace(
"labeling::colorize");
207 mln_precondition(
exact(input).is_valid());
214 static fun::i2v::array<V> f(0);
215 int diff_size = f.size() - label_count;
219 unsigned i = f.size();
220 f.resize(label_count);
227 for (; i < f.size(); ++i)
228 f(i) = internal::random_color(value);
230 mln_assertion(f.size() >= (label_count));
236 template <typename V, typename L>
240 const Image<L>& input)
242 mln_trace(
"labeling::colorize");
243 mln_precondition(
exact(input).is_valid());
245 accu::stat::max<mln_value(L)> accu;
248 mln_ch_value(L,V) output =
colorize(value, input, nlabels);
254 template <typename L>
256 mln_ch_value(L, mln::value::rgb8)
258 const mln_value(L)& nlabels)
264 # endif // ! MLN_INCLUDE_ONLY
271 #endif // ! MLN_LABELING_COLORIZE_HH