utils.hh

00001 // Copyright (C) 2006  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_LRDE_UFMT_UTILS_HH
00029 # define OLENA_LRDE_UFMT_UTILS_HH
00030 
00031 # include <cstdlib>
00032 # include <vector>
00033 
00034 # include <ntg/real/int_u.hh>
00035 # include <oln/core/image1d.hh>
00036 # include <oln/core/image2d.hh>
00037 # include <oln/core/image3d.hh>
00038 
00039 
00040 
00041 # define oln_neighborhood_type(I) \
00042 typename oln::lrde::ufmt::internal::neighborhood<I>::ret
00043 
00044 
00045 
00046 namespace oln
00047 {
00048 
00049 
00050   // Fwd decls.
00051   struct neighborhood1d;
00052   struct neighborhood2d;
00053   struct neighborhood3d;
00054   // end of fwd decls.
00055 
00056 
00057 
00058   namespace lrde
00059   {
00060 
00061     namespace ufmt
00062     {
00063 
00064 
00065       // Get the number of possible different unsigned integer values
00066       // of the input image data; for instance, when 'input' contains
00067       // int_u8 data, the result is 256.
00068 
00069       template <class I>
00070       size_t uint_nvalues(const abstract::image<I>& input)
00071       {
00072         typedef oln_value_type(I) T;
00073         typedef typename ntg_is_a(T, ntg::unsigned_integer)::ensure_type ensure_type;
00074         return size_t(ntg_max_val(T)) + 1;
00075       }
00076 
00077       template <class T>
00078       size_t uint_nvalues()
00079       {
00080         typedef typename ntg_is_a(T, ntg::unsigned_integer)::ensure_type ensure_type;
00081         return size_t(ntg_max_val(T)) + 1;
00082       }
00083 
00084 
00085       // Compute the input image histogram up to the Nth point of the
00086       // input image.
00087 
00088       template <class I>
00089       std::vector<size_t> histogram(const abstract::image<I>& input,
00090                                     size_t N)
00091       {
00092         unsigned nvalues = uint_nvalues(input);
00093         std::vector<size_t> H(nvalues, 0);
00094         size_t count = 0;
00095         oln_iter_type(I) p(input);
00096         for_all(p)
00097           {
00098             ++H[input[p]];
00099             if (++count == N)
00100               return H;
00101           }
00102         return H;
00103       }
00104 
00105 
00106       // Compute the input image histogram.
00107 
00108       template <class I>
00109       std::vector<size_t> histogram(const abstract::image<I>& input)
00110       {
00111         return histogram(input, input.npoints());
00112       }
00113 
00114 
00115       // FIXME: doc.
00116 
00117       template <class I>
00118       std::vector<oln_point_type(I)>
00119       histogram_reverse_sort_p(const abstract::image<I>& input,
00120                                size_t N,
00121                                std::vector<size_t>& H)
00122       {
00123         H = histogram(input, N);
00124 
00125         // preparing output data
00126 
00127         unsigned nvalues = uint_nvalues(input);
00128         std::vector<int> loc(nvalues);
00129         loc[nvalues - 1] = 0;
00130         for (int l = int(nvalues) - 2; l >= 0; --l)
00131           loc[l] = loc[l+1] + H[l+1];
00132 
00133         std::vector<oln_point_type(I)> vec(N);
00134 
00135         // storing output data
00136 
00137         oln_iter_type(I) p(input);
00138         size_t count = 0;
00139         for_all(p)
00140           {
00141             vec[loc[input[p]]++] = p;
00142             if (++count == N)
00143               return vec;
00144           }
00145         return vec;
00146       }
00147 
00148 
00149 
00150       template <class I>
00151       std::vector<oln_point_type(I)>
00152       histogram_reverse_sort_p(const abstract::image<I>& input,
00153                                std::vector<size_t>& H)
00154       {
00155         return histogram_reverse_sort_p(input, input.npoints(), H);
00156       }
00157 
00158 
00159       template <class I>
00160       std::vector<oln_point_type(I)>
00161       histogram_reverse_sort_p(const abstract::image<I>& input,
00162                                size_t N)
00163       {
00164         std::vector<size_t> H;
00165         return histogram_reverse_sort_p(input, N, H);
00166       }
00167 
00168 
00169       template <class I>
00170       std::vector<oln_point_type(I)>
00171       histogram_reverse_sort_p(const abstract::image<I>& input)
00172       {
00173         std::vector<size_t> H;
00174         return histogram_reverse_sort_p(input, input.npoints(), H);
00175       }
00176 
00177 
00178       // _i
00179 
00180       template <class I>
00181       std::vector<int>
00182       histogram_reverse_sort_i(const abstract::image<I>& input,
00183                                size_t N,
00184                                std::vector<size_t>& H)
00185       {
00186         H = histogram(input, N);
00187 
00188         // preparing output data
00189 
00190         unsigned nvalues = uint_nvalues(input);
00191         std::vector<int> loc(nvalues);
00192         loc[nvalues - 1] = 0;
00193         for (int l = int(nvalues) - 2; l >= 0; --l)
00194           loc[l] = loc[l+1] + H[l+1];
00195 
00196         std::vector<int> vec(N);
00197 
00198         // storing output data
00199 
00200         typedef oln_value_type(I) value_t;
00201         typedef oln_point_type(I) point_t;
00202 
00203         I& input_ = const_cast<I&>(input.exact());
00204         value_t* orig = &(input_[point_t()]);
00205         oln_iter_type(I) p(input);
00206         size_t count = 0;
00207         for_all(p)
00208           {
00209             vec[loc[input[p]]++] = int(&(input_[p]) - orig);
00210             if (++count == N)
00211               return vec;
00212           }
00213 
00214         return vec;
00215       }
00216 
00217 
00218       template <class I>
00219       std::vector<int>
00220       histogram_reverse_sort_i(const abstract::image<I>& input,
00221                                std::vector<size_t>& H)
00222       {
00223         return histogram_reverse_sort_i(input, input.npoints(), H);
00224       }
00225 
00226 
00227 
00228       // Extra traits to get the classical neighborhood types from
00229       // classical image types.
00230 
00231       namespace internal {
00232 
00233         template <class I>
00234         struct neighborhood;
00235 
00236         template <class T>
00237         struct neighborhood < image1d<T> > { typedef neighborhood1d ret; };
00238         template <class T>
00239         struct neighborhood < image2d<T> > { typedef neighborhood2d ret; };
00240         template <class T>
00241         struct neighborhood < image3d<T> > { typedef neighborhood3d ret; };
00242 
00243       } // end of namespace oln::lrde::ufmt::internal
00244 
00245 
00246 
00247       template <class I>
00248       unsigned pre(const oln_neighborhood_type(I)& nbh,
00249                    std::vector<oln_dpoint_type(I)>& pre)
00250       {
00251         for (unsigned i = 0; i < nbh.card(); ++i)
00252           {
00253             unsigned d;
00254             for (d = 0; d < I::dim; ++d)
00255               if (nbh.dp(i).nth(d) < 0) {
00256                 pre.push_back(nbh.dp(i));
00257                 break;
00258               }
00259               else if (nbh.dp(i).nth(d) > 0)
00260                 break;
00261           }
00262         assert(pre.size() == (nbh.card() / 2));
00263         return pre.size();
00264       }
00265 
00266 
00267       template <class I>
00268       unsigned split(const oln_neighborhood_type(I)& nbh,
00269                      std::vector<oln_dpoint_type(I)>& pre,
00270                      std::vector<oln_dpoint_type(I)>& post)
00271       {
00272         for (unsigned i = 0; i < nbh.card(); ++i)
00273           {
00274             unsigned d;
00275             bool is_post = true;
00276             for (d = 0; d < I::dim; ++d)
00277               if (nbh.dp(i).nth(d) < 0) {
00278                 pre.push_back(nbh.dp(i));
00279                 is_post = false;
00280                 break;
00281               }
00282               else if (nbh.dp(i).nth(d) > 0)
00283                 break;
00284             if (is_post)
00285               post.push_back(nbh.dp(i));
00286           }
00287         if (not ((pre.size() + post.size()) == nbh.card())
00288             or not (pre.size() == post.size()))
00289           {
00290             std::cerr << "pb in oln::lrde::ufmt::split (utils.hh) so abort!" << std::endl;
00291             abort();
00292           }
00293         assert((pre.size() + post.size()) == nbh.card());
00294         assert(pre.size() == post.size());
00295         return pre.size();
00296       }
00297 
00298 
00299       
00300       template <class I>
00301       typename mute<I, float>::ret
00302       to_float_with_noise(const abstract::image<I>& input)
00303       {
00304         const float max = float(RAND_MAX);
00305         typename mute<I, float>::ret output(input.size());
00306         oln_iter_type(I) p(input);
00307         for_all(p)
00308           output[p] = float(input[p]) + float(rand()) / max;
00309         return output;
00310       }
00311 
00312 
00313       template <typename I>
00314       struct rev_sort
00315       {
00316         typedef oln_point_type(I) point;
00317         const I& f;
00318 
00319         rev_sort(const I& f) :
00320           f(f)
00321         {}
00322 
00323         bool operator()(const point& lhs, const point& rhs) const
00324         {
00325           return f[lhs] > f[rhs];
00326         }
00327       };
00328 
00329 
00330     } // end of namespace oln::lrde::ufmt
00331 
00332   } // end of namespace oln::lrde
00333 
00334 } // end of namespace oln
00335 
00336 
00337 
00338 
00339 #endif // ! OLENA_LRDE_UFMT_UTILS_HH

Generated on Tue Feb 20 20:21:13 2007 for Olena by  doxygen 1.5.1