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>
55 extern unsigned min_value;
56 extern unsigned max_value;
72 template <
typename V,
typename L>
75 const Image<L>& labeled_image,
76 const mln_value(L)& nlabels);
81 template <typename V, typename L>
84 const Image<L>& labeled_image);
90 mln_ch_value(L, mln::value::rgb8)
92 const mln_value(L)& nlabels);
95 # ifndef MLN_INCLUDE_ONLY
97 # ifndef MLN_WO_GLOBAL_VARS
101 unsigned min_value = 20;
102 unsigned max_value = 220;
105 # endif // ! MLN_WO_GLOBAL_VARS
111 unsigned random_number()
113 unsigned last = colorize_::min_value + (colorize_::max_value - colorize_::min_value + 1) * rand();
115 return math::min(colorize_::min_value + last % colorize_::max_value, colorize_::max_value);
120 template <
typename V>
121 V random_color(
const V&);
124 template <
typename RGB>
126 random_color_rgb(
const RGB&)
129 nelements = colorize_::max_value - colorize_::min_value + 1;
130 static util::array<util::set<unsigned> >
139 red = random_number();
142 while (red_[red - colorize_::min_value].nelements() == nelements
143 && ntries < nelements);
145 if (ntries == nelements)
147 trace::warning(
"labeling::colorize - Can't find a new unique color. Returning black.");
153 green = random_number();
154 while (red_[red - colorize_::min_value].
has(green)
155 || green_[green - colorize_::min_value].nelements() == nelements);
156 red_[red - colorize_::min_value].insert(green);
159 blue = random_number();
160 while (green_[green - colorize_::min_value].
has(blue));
161 green_[green - colorize_::min_value].insert(blue);
163 return RGB(red, green, blue);
166 template <
unsigned n>
170 return random_color_rgb(v);
174 # ifdef MLN_VALUE_QT_RGB32_HH
180 return random_color_rgb(v);
183 # endif // ! MLN_VALUE_QT_RGB32_HH
187 template <
typename V,
typename L>
191 const
Image<L>& input,
192 const mln_value(L)& nlabels)
194 trace::entering(
"labeling::colorize");
195 mln_precondition(exact(input).is_valid());
201 unsigned label_count = value::next(nlabels);
202 static fun::i2v::array<V> f(0);
203 int diff_size = f.size() - label_count;
207 f.resize(label_count);
208 unsigned i = f.size() + diff_size;
215 for (; i < f.size(); ++i)
216 f(i) = internal::random_color(value);
218 mln_assertion(f.size() >= (label_count));
221 trace::exiting(
"labeling::colorize");
225 template <
typename V,
typename L>
229 const
Image<L>& input)
231 trace::entering(
"labeling::colorize");
232 mln_precondition(exact(input).is_valid());
237 mln_ch_value(L,V) output =
colorize(value, input, nlabels);
239 trace::exiting(
"labeling::colorize");
244 template <
typename L>
248 const mln_value(L)& nlabels)
254 # endif // ! MLN_INCLUDE_ONLY
261 #endif // ! MLN_LABELING_COLORIZE_HH