Milena (Olena)
User documentation 2.0a Id
|
00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE) 00002 // 00003 // This file is part of Olena. 00004 // 00005 // Olena is free software: you can redistribute it and/or modify it under 00006 // the terms of the GNU General Public License as published by the Free 00007 // Software Foundation, version 2 of the License. 00008 // 00009 // Olena is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 // General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Olena. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free 00018 // software project without restriction. Specifically, if other files 00019 // instantiate templates or use macros or inline functions from this 00020 // file, or you compile this file and link it with other files to produce 00021 // an executable, this file does not by itself cause the resulting 00022 // executable to be covered by the GNU General Public License. This 00023 // exception does not however invalidate any other reasons why the 00024 // executable file might be covered by the GNU General Public License. 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 } // end of namespace mln::labeling 00124 00125 } // end of namespace mln 00126 00127 #endif // ! MLN_LABELING_N_MAX_HH