$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
n_max.hh
1 // Copyright (C) 2009, 2013 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_LABELING_N_MAX_HH
28 # define MLN_LABELING_N_MAX_HH
29 
30 # include <mln/core/concept/image.hh>
31 # include <mln/accu/math/count.hh>
32 # include <mln/labeling/compute.hh>
33 # include <mln/util/array.hh>
34 
39 
40 namespace mln
41 {
42 
43  namespace labeling
44  {
45 
57  template <typename L>
58  util::array<mln_value(L)>
59  n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64  namespace internal
65  {
66 
67  template <typename L, typename V>
68  util::array<L>
69  n_max(const util::array<V>& in_arr, unsigned n)
70  {
71  mln_trace("mln::labeling::n_max");
72 
73  mln_precondition(n < in_arr.nelements());
74 
75  util::array<L> output(n + 1, 0);
76  for (unsigned i = 0; i < n + 1; ++i)
77  output[i] = i;
78  int swap = 0;
79  for (int j = n - 1; j > 0; --j)
80  {
81  if (in_arr[output[j]] < in_arr[output[j + 1]])
82  {
83  swap = output[j];
84  output[j] = output[j + 1];
85  output[j + 1] = swap;
86  }
87  }
88 
89  for (unsigned i = n; i < in_arr.nelements(); ++i)
90  {
91  if (in_arr[i] > in_arr[output[n]])
92  {
93  output[n] = i;
94  for (unsigned j = n - 1; j > 0 && in_arr[output[j]] < in_arr[output[j + 1]]; --j)
95  {
96  swap = output[j];
97  output[j] = output[j + 1];
98  output[j + 1] = swap;
99  }
100  }
101  }
102 
103  return output;
104  }
105 
106  } // end of namespace mln::labeling::internal
107 
108 
109  template <typename L>
110  util::array<mln_value(L)>
111  n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n)
112  {
113  mln_precondition(exact(lbl).is_valid());
114 
115  typedef accu::math::count<mln_site(L)> accu_t;
116  accu_t accu;
117 
118  util::array<mln_result(accu_t)>
119  counts = labeling::compute(accu, lbl, nlabels);
120 
121  return n_max<mln_value(L)>(counts, n);
122  }
123 
124 
125 # endif // !MLN_INCLUDE_ONLY
126 
127  } // end of namespace mln::labeling
128 
129 } // end of namespace mln
130 
131 #endif // ! MLN_LABELING_N_MAX_HH