req.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_REQ_HH
00029 # define OLENA_LRDE_EFIGI_REQ_HH
00030 
00031 #include <iostream>
00032 #include <cmath>
00033 #include <map>
00034 #include <oln/basics2d.hh>
00035 
00036 #include <oln/lrde/efigi/misc.hh>
00037 
00038 
00039 namespace oln
00040 {
00041 
00042   namespace efigi
00043   {
00044 
00045 
00046     image2d<unsigned char>
00047     convert_linear_8(const image2d<float>& input,
00048                      float min, float max)
00049     {
00050       float coef = 256.f / (max - min);
00051       const int nrows = input.nrows(), ncols = input.ncols();
00052       image2d<unsigned char> output(nrows, ncols);
00053       for (int row = 0; row < nrows; ++row)
00054         for (int col = 0; col < ncols; ++col)
00055           {
00056             float dest = min + coef * input(row, col);
00057             if (dest < 0.)
00058               output(row, col) = 0;
00059             else if (dest > 255.)
00060               output(row, col) = 255;
00061             else
00062               output(row, col) = unsigned(dest);
00063           }
00064       return output;
00065     }
00066 
00067 
00068     image2d<unsigned short>
00069     convert_linear_16(const image2d<float>& input,
00070                       float min, float max)
00071     {
00072       float coef = 65536.f / (max - min);
00073       const int nrows = input.nrows(), ncols = input.ncols();
00074       image2d<unsigned short> output(nrows, ncols);
00075       for (int row = 0; row < nrows; ++row)
00076         for (int col = 0; col < ncols; ++col)
00077           {
00078             float dest = min + coef * input(row, col);
00079             if (dest < 0.)
00080               output(row, col) = 0;
00081             else if (dest > 65535.)
00082               output(row, col) = 65535;
00083             else
00084               output(row, col) = unsigned(dest);
00085           }
00086       return output;
00087     }
00088 
00089 
00090     float slog(float f, float sigma)
00091     {
00092       return f < 0 ? - std::log(1. - f / sigma) : std::log(1. + f / sigma);
00093     }
00094 
00095 
00096     image2d<unsigned char>
00097     convert_log(const image2d<float>& input,
00098                 float min, float max,
00099                 float sigma)
00100     {
00101       assert(sigma > 0);
00102       assert(max > min);
00103 
00104       float orig = slog(min, sigma);
00105       float L = (slog(max, sigma) - orig) / 255.9999f;
00106 
00107       unsigned too_low = 0, too_high = 0;
00108       const int nrows = input.nrows(), ncols = input.ncols();
00109       image2d<unsigned char> output(nrows, ncols);
00110       for (int row = 0; row < nrows; ++row)
00111         for (int col = 0; col < ncols; ++col)
00112           {
00113             float f = input(row, col), f_dyn = 0;
00114             if (f >= max)
00115               output(row, col) = 255;
00116             else
00117               if (f <= min)
00118                 output(row, col) = 0;
00119               else
00120                 // min < f < max
00121                 {
00122                   f_dyn = (slog(f, sigma) - orig) / L;
00123                   if (f_dyn < 0)
00124                     ++too_low;
00125                   if (f_dyn > 255)
00126                     ++too_high;
00127                 }
00128             output(row, col) = unsigned(f_dyn);
00129           }    
00130 
00131       if (too_low || too_high)
00132         std::cerr << "pb in efigi::convert_log: "
00133                   << too_low << ' ' << too_high << std::endl;
00134 
00135       return output;
00136     }
00137 
00138 
00139     image2d<unsigned char>
00140     equalize(const image2d<float>& input)
00141     {
00142       std::vector<pix> v = efigi::sort(input);
00143       const int nrows = input.nrows(), ncols = input.ncols();
00144       image2d<unsigned char> output(nrows, ncols);
00145 
00146       unsigned n = unsigned((nrows * ncols) / 256.f + 1.f);
00147       unsigned l = 0;
00148       for (;;)
00149         {
00150           for (unsigned i = 0; i < n; ++i)
00151             {
00152               unsigned j = l * n + i;
00153               if (j >= v.size())
00154                 return output;
00155               pix& p = v[l * n + i];
00156               output(p.row, p.col) = l;
00157             }
00158           ++l;
00159         }
00160       return output;
00161     }
00162 
00163     image2d<unsigned short>
00164     equalize_16(const image2d<float>& input)
00165     {
00166       std::vector<pix> v = efigi::sort(input);
00167       const int nrows = input.nrows(), ncols = input.ncols();
00168       image2d<unsigned short> output(nrows, ncols);
00169 
00170       unsigned n = unsigned((nrows * ncols) / 65536.f + 1.f);
00171       unsigned l = 0;
00172       for (;;)
00173         {
00174           for (unsigned i = 0; i < n; ++i)
00175             {
00176               unsigned j = l * n + i;
00177               if (j >= v.size())
00178                 return output;
00179               pix& p = v[l * n + i];
00180               output(p.row, p.col) = l;
00181             }
00182           ++l;
00183         }
00184       return output;
00185     }
00186 
00187     
00188     image2d<bool>
00189     lab2bin(const image2d<unsigned char>& input)
00190     {
00191       const int nrows = input.nrows(), ncols = input.ncols();
00192       image2d<bool> output(nrows, ncols);
00193       for (int row = 0; row < nrows; ++row)
00194         for (int col = 0; col < ncols; ++col)
00195           output(row, col) = (input(row, col) == 255);
00196       return output;
00197     }
00198 
00199 
00200   } // end of namespace oln::efigi
00201 
00202 } // end of namespace oln
00203 
00204 
00205 #endif // ! OLENA_LRDE_EFIGI_REQ_HH

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