Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

KanungoBinaryImage.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------*
00002  | Library QgarLib, graphics analysis and recognition                  |
00003  | Copyright (C) 2002  Qgar Project, LORIA                             |
00004  |                                                                     |
00005  | This library is free software; you can redistribute it and/or       |
00006  | modify it under the terms of the GNU Lesser General Public          |
00007  | License version 2.1, as published by the Free Software Foundation.  |
00008  |                                                                     |
00009  | This library 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.                |
00012  | See the GNU Lesser General Public License for more details.         |
00013  |                                                                     |
00014  | The GNU Lesser General Public License is included in the file       |
00015  | LICENSE.LGPL, in the root directory of the Qgar packaging. See      |
00016  | http://www.gnu.org/licenses/lgpl.html for the terms of the licence. |
00017  | To receive a paper copy, write to the Free Software Foundation,     |
00018  | Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.       |
00019  |                                                                     |
00020  | Contact Project Qgar for any information:                           |
00021  |   LORIA - équipe Qgar                                               |
00022  |   B.P. 239, 54506 Vandoeuvre-lès-Nancy Cedex, France                |
00023  |   email: qgar-contact@loria.fr                                      |
00024  |   http://www.qgar.org/                                              |
00025  *---------------------------------------------------------------------*/
00026 
00027 
00028 /**
00029  * @file  KanungoBinaryImage.C
00030  * @brief Implementation of class KanungoBinaryImage.
00031  *
00032  * See file KanungoBinaryImage.H for the interface.
00033  *
00034  * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Perrin & Kheder">Julien Perrin & Sami Kheder</a>
00035  * @date   March 24,  2003  16:16
00036  * @since  Qgar 2.1.1
00037  */
00038 
00039 
00040 
00041 // STD
00042 #include <cmath>
00043 #include <cstdlib>
00044 #include <sstream>
00045 #include <string>
00046 // QGAR
00047 #include <qgarlib/CloseBinaryImage.H>
00048 #include <qgarlib/Dist34Image.H>
00049 #include <qgarlib/GenImage.H>
00050 #include <qgarlib/KanungoBinaryImage.H>
00051 
00052 
00053 
00054 using namespace std;
00055 
00056 
00057 namespace qgar
00058 {
00059 
00060 // -------------------------------------------------------------------
00061 // C O N S T R U C T O R
00062 // -------------------------------------------------------------------
00063 
00064 
00065 KanungoBinaryImage::KanungoBinaryImage(BinaryImage& anImg, 
00066                                        double alpha0, 
00067                                        double alpha, 
00068                                        double beta0, 
00069                                        double beta, 
00070                                        double eta, 
00071                                        int structEltSize)
00072 
00073   : BinaryImage(anImg)
00074 
00075 {
00076   qgKanungoDegradation(*this, alpha0, alpha, beta0, beta, eta, structEltSize);
00077 }
00078 
00079 
00080 // -------------------------------------------------------------------
00081 // G L O B A L   F U N C T I O N S
00082 // -------------------------------------------------------------------
00083 
00084 
00085 void
00086 qgKanungoDegradation(BinaryImage& anImg, 
00087                      double alpha0, 
00088                      double alpha, 
00089                      double beta0, 
00090                      double beta, 
00091                      double eta, 
00092                      int structEltSize)
00093 
00094     throw(QgarErrorInvalidArg)
00095 
00096 {
00097   // CHECK ARGUMENTS
00098   // ===============
00099 
00100   if (structEltSize < 0)
00101     {
00102       std::ostringstream os;
00103       os << "Structural element size cannot be negative ("
00104          << structEltSize
00105          << ')';
00106       throw QgarErrorInvalidArg(__FILE__, __LINE__,
00107                                 "void qgar::qgKanungoDegradation(qgar::BinaryImage&, double, double, double, double, double, int)",
00108                                 os.str());
00109     }
00110 
00111   // DISTANCE TRANSFORM
00112   // ==================
00113 
00114   Dist34Image* tmpDistImg = new Dist34Image(anImg);
00115 
00116   // PROBABILISTIC TRANSFORM
00117   // =======================
00118 
00119   // Random number generator initialization
00120   std::srand(std::time(0) + getpid() * 1000);
00121 
00122   // Pointers to pixel maps of initial and distance images 
00123   BinaryImage::pointer pMapImg  = anImg.pPixMap();
00124   Dist34Image::pointer pMapDist = tmpDistImg->pPixMap();
00125 
00126   // Image size
00127   int size = (anImg.width()) * (anImg.height());
00128 
00129   // From each pixel (top to bottom, left to right)...
00130 
00131   for (int iCnt = 0 ; iCnt < size ; ++iCnt, ++pMapImg, ++pMapDist)
00132     { 
00133       double proba;
00134 
00135       if (*pMapImg == QGE_BW_WHITE)
00136         {
00137           // White pixel
00138           proba =
00139             (beta0 * exp(-beta * pow((((double) *pMapDist) / 3.), 2.)))
00140             + eta;
00141 
00142           // Random test
00143           if (((double) rand() / (double) RAND_MAX) < proba)
00144             {
00145               *pMapImg = QGE_BW_BLACK;  // Swap pixel value
00146             }
00147         }
00148       else 
00149         {
00150           // Black pixel
00151           proba =
00152             (alpha0 * exp(-alpha * pow((((double) *pMapDist) / 3.), 2.)))
00153             + eta;
00154 
00155           // Random test
00156           if (((double) rand() / (double) RAND_MAX) < proba)
00157             {
00158               *pMapImg = QGE_BW_WHITE;  // Swap pixel value
00159             }
00160         }
00161     } // END for iCnt
00162 
00163   // DELETE TEMPORARY IMAGE
00164   // ======================
00165 
00166   delete tmpDistImg;
00167 
00168   // CLOSING
00169   // =======
00170 
00171   if (structEltSize != 0) 
00172     {
00173       CloseBinaryImage::perform(&anImg, structEltSize);
00174     }
00175 }
00176 
00177 // ----------------------------------------------------------------------
00178 
00179 } // namespace qgar
00180 
00181 
00182 
00183 
00184 
00185 
00186 
00187 
00188