00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef MLN_LABELING_N_MAX_HH
00027 # define MLN_LABELING_N_MAX_HH
00028
00029 # include <mln/core/concept/image.hh>
00030 # include <mln/accu/math/count.hh>
00031 # include <mln/labeling/compute.hh>
00032 # include <mln/util/array.hh>
00033
00044
00045
00046 namespace mln
00047 {
00048
00049 namespace labeling
00050 {
00051
00052 template <typename L, typename V>
00053 util::array<L>
00054 n_max(const util::array<V>& in_arr, unsigned n);
00055
00056
00057 template <typename L>
00058 util::array<mln_value(L)>
00059 n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n);
00060
00061
00062 # ifndef MLN_INCLUDE_ONLY
00063
00064 template <typename L, typename V>
00065 util::array<L>
00066 n_max(const util::array<V>& in_arr, unsigned n)
00067 {
00068 trace::entering("mln::labeling::n_max");
00069
00070 mln_precondition(n < in_arr.nelements());
00071
00072 util::array<L> output(n + 1, 0);
00073 for (unsigned i = 0; i < n + 1; ++i)
00074 output[i] = i;
00075 int swap = 0;
00076 for (int j = n - 1; j > 0; --j)
00077 {
00078 if (in_arr[output[j]] < in_arr[output[j + 1]])
00079 {
00080 swap = output[j];
00081 output[j] = output[j + 1];
00082 output[j + 1] = swap;
00083 }
00084 }
00085
00086 for (unsigned i = n; i < in_arr.nelements(); ++i)
00087 {
00088 if (in_arr[i] > in_arr[output[n]])
00089 {
00090 output[n] = i;
00091 for (unsigned j = n - 1; j > 0 && in_arr[output[j]] < in_arr[output[j + 1]]; --j)
00092 {
00093 swap = output[j];
00094 output[j] = output[j + 1];
00095 output[j + 1] = swap;
00096 }
00097 }
00098 }
00099
00100 trace::exiting("mln::labeling::n_max");
00101 return output;
00102 }
00103
00104
00105 template <typename L>
00106 util::array<mln_value(L)>
00107 n_max(const Image<L>& lbl, const mln_value(L)& nlabels, unsigned n)
00108 {
00109 mln_precondition(exact(lbl).is_valid());
00110
00111 typedef accu::math::count<mln_site(L)> accu_t;
00112 accu_t accu;
00113
00114 util::array<mln_result(accu_t)>
00115 counts = labeling::compute(accu, lbl, nlabels);
00116
00117 return n_max<mln_value(L)>(counts, n);
00118 }
00119
00120
00121 # endif // !MLN_INCLUDE_ONLY
00122
00123 }
00124
00125 }
00126
00127 #endif // ! MLN_LABELING_N_MAX_HH