misc.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_EFIGI_MISC_HH
00029 # define OLENA_LRDE_EFIGI_MISC_HH
00030 
00031 #include <cmath>
00032 #include <cstdlib>
00033 
00034 #include <vector>
00035 #include <algorithm>
00036 #include <fstream>
00037 
00038 #include <oln/basics2d.hh>
00039 
00040 
00041 namespace oln
00042 {
00043 
00044   namespace efigi
00045   {
00046 
00047  
00048     void min_max(const image2d<float>& ima, float& min, float& max)
00049     {
00050       min = 1.0E30;
00051       max = -1.0E30;
00052       const int nrows = ima.nrows(), ncols = ima.ncols();
00053       for (int row = 0; row < nrows; ++row)
00054         for (int col = 0; col < ncols; ++col)
00055           {
00056             if (ima(row, col) < min)
00057               min = ima(row, col);
00058             if (ima(row, col) > max)
00059               max = ima(row, col);
00060           }
00061     }
00062 
00063 
00064 
00065     image2d<float> add_float_noise(const image2d<unsigned char>& input)
00066     {
00067       image2d<float> output(input.nrows(), input.ncols());
00068       for (int row = 0; row < input.nrows(); ++row)
00069         for (int col = 0; col < input.ncols(); ++col)
00070           output(row, col) = input(row, col) + float(rand() / RAND_MAX);
00071       return output;
00072     }
00073 
00074 
00075 
00076     bool diff(const image2d<float>& ima1, const image2d<float>& ima2)
00077     {
00078       const float err = 1E-5;
00079       assert(ima1.nrows() == ima2.nrows() && ima1.ncols() == ima2.ncols());
00080       for (int row = 0; row < ima1.nrows(); ++row)
00081         for (int col = 0; col < ima1.ncols(); ++col)
00082           {
00083             float sim =
00084               std::min(ima1(row, col), ima2(row, col))
00085               / std::max(ima1(row, col), ima2(row, col));
00086             if (sim < 1 - err)
00087               return true;
00088           }
00089       return false;
00090     }
00091 
00092 
00093 
00094     std::vector<unsigned> compute_histo(const image2d<unsigned char>& input)
00095     {
00096       std::vector<unsigned> h(256, 0);
00097       const int nrows = input.nrows(), ncols = input.ncols();
00098       for (int row = 0; row < nrows; ++row)
00099         for (int col = 0; col < ncols; ++col)
00100           ++h[input(row, col)];
00101       return h;
00102     }
00103 
00104     std::vector<unsigned> compute_histo(const image2d<unsigned short>& input)
00105     {
00106       std::vector<unsigned> h(65536, 0);
00107       const int nrows = input.nrows(), ncols = input.ncols();
00108       for (int row = 0; row < nrows; ++row)
00109         for (int col = 0; col < ncols; ++col)
00110           ++h[input(row, col)];
00111       return h;
00112     }
00113 
00114     void print_histo(std::vector<unsigned>& h,
00115                      std::ofstream& file)
00116     {
00117       for (unsigned i = 0; i < h.size(); ++i)
00118         file << i << ' ' << h[i] << std::endl;
00119     }
00120 
00121 
00122     struct pix
00123     {
00124       pix() {}
00125       pix(float val, int row, int col) : val(val), row(row), col(col) {}
00126       bool operator<(const pix& rhs) const {
00127         if (val < rhs.val)
00128           return true;
00129         if (val > rhs.val)
00130           return false;
00131         return row < rhs.row or (row == rhs.row and col < rhs.col);
00132       }
00133       float val;
00134       int row, col;
00135     };
00136 
00137 
00138     std::vector<pix>
00139     sort(const image2d<float>& input)
00140     {
00141       std::vector<pix> v;
00142 
00143       const int nrows = input.nrows(), ncols = input.ncols();
00144       v.reserve(nrows * ncols);
00145       for (int row = 0; row < nrows; ++row)
00146         for (int col = 0; col < ncols; ++col)
00147           v.push_back(pix(input(row, col), row, col));
00148       std::sort(v.begin(), v.end());
00149       return v;
00150     }
00151 
00152 
00153     // Crop a 2-D image.
00154     template <typename T>
00155     image2d<T>
00156     crop(const image2d<T>& input, coord x, coord y, coord dx, coord dy)
00157     {
00158       assertion (dx > 0);
00159       assertion (dy > 0);
00160       assertion(input.hold(point2d(x, y)));
00161       assertion(input.hold(point2d(x + dx, y + dy)));
00162 
00163       image2d<float> output(dx, dy);
00164       for (coord row = 0; row < dx; ++row)
00165         for (coord col = 0; col < dy; ++col)
00166           output(row, col) = input(x + row, y + col);
00167       return output;
00168     }
00169 
00170 
00171   } // end of namespace oln::efigi
00172 
00173 } // end of namespace oln
00174 
00175 
00176 #endif // ! OLENA_LRDE_EFIGI_MISC_HH

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