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 #ifndef __PRUNESMALLCCBINARYIMAGE_H_INCLUDED__ 00029 #define __PRUNESMALLCCBINARYIMAGE_H_INCLUDED__ 00030 00031 00032 /** 00033 * @file PruneSmallCCBinaryImage.H 00034 * @brief Header file of class qgar::PruneSmallCCBinaryImage. 00035 * 00036 * @author <a href="mailto:qgar-develop@loria.fr?subject=Qgar fwd Philippe Dosch">Philippe Dosch</a> 00037 * @date April 9, 2001 16:56 00038 * @since Qgar 2.0 00039 */ 00040 00041 00042 // For RCS/CVS use: Do not delete 00043 /* $Id: PruneSmallCCBinaryImage.H,v 1.3 2005/01/11 13:14:10 masini Exp $ */ 00044 00045 00046 00047 // STD 00048 #include <list> 00049 #include <set> 00050 #include <vector> 00051 // QGAR 00052 #include <qgarlib/GenImage.H> 00053 00054 00055 00056 namespace qgar 00057 { 00058 00059 /** 00060 * @class PruneSmallCCBinaryImage PruneSmallCCBinaryImage.H "qgarlib/PruneSmallCCBinaryImage.H" 00061 * @ingroup GRAPHPROC_CC 00062 * @brief Prune small connected components. 00063 * 00064 * This class provides a fast method to prune small connected components 00065 * of a binary image, without actually constructing any component. 00066 * For advanced operations on connected components, see class 00067 * qgar::ConnectedComponents. 00068 * 00069 * This class is based on the method described in 00070 * [<a href="Bibliography.html#Jain-et-al-1995">Jain et al, 1995</a>]. 00071 */ 00072 class PruneSmallCCBinaryImage 00073 00074 : public BinaryImage 00075 00076 { 00077 00078 // --------------------------------------------------------------------- 00079 // D A T A S T R U C T U R E S 00080 // --------------------------------------------------------------------- 00081 00082 /** 00083 * @class ComponentEntry PruneSmallCCBinaryImage.H 00084 * @internal 00085 */ 00086 class ComponentEntry 00087 { 00088 public: 00089 // Constructor 00090 ComponentEntry(int x, int y) 00091 00092 : _x(x), 00093 _y(y), 00094 _count(1), 00095 _valid(true) 00096 00097 { 00098 // VOID 00099 } 00100 00101 // Increments moment and update last point 00102 void inc(int x, int y, int i = 1) 00103 { 00104 _x = x; 00105 _y = y; 00106 _count += i; 00107 } 00108 00109 // Increments moment 00110 void inc(int i) 00111 { 00112 _count += i; 00113 } 00114 00115 // Last x-coordinate 00116 int x() const 00117 { 00118 return _x; 00119 } 00120 00121 // Last y-coordinate 00122 int y() const 00123 { 00124 return _y; 00125 } 00126 00127 // Return entry's moment 00128 int count() const 00129 { 00130 return _count; 00131 } 00132 00133 // Return validity 00134 bool valid() const 00135 { 00136 return _valid; 00137 } 00138 00139 // Set validity 00140 void setValid(bool state) 00141 { 00142 _valid = state; 00143 } 00144 00145 protected: 00146 int _x; 00147 int _y; 00148 int _count; 00149 bool _valid; 00150 }; 00151 00152 // ------------------------------------------------------------------- 00153 // P U B L I C M E M B E R S 00154 // ------------------------------------------------------------------- 00155 public: 00156 00157 /// Constructor. 00158 PruneSmallCCBinaryImage(const BinaryImage img, int size); 00159 00160 00161 // ------------------------------------------------------------------- 00162 // P R O T E C T E D M E M B E R S 00163 // ------------------------------------------------------------------- 00164 protected: 00165 00166 std::vector<ComponentEntry> _theEntries; 00167 std::list<int> _unusedEntries; 00168 std::list< std::set<int> > _equivTable; 00169 int* _lastRow; 00170 int* _curRow; 00171 int _size; 00172 00173 /// Process a black pixel. 00174 void processPixel(int i, int j); 00175 00176 /// Create a new component entry. 00177 int createNewEntry(int i, int j); 00178 00179 /// Attach a pixel to an existing entry. 00180 void attachPixel(int i, int j, int entry); 00181 00182 /// Attach a pixel to existing entries and mark these entries as unificable. 00183 void attachPixelTwoEntries(int i, int j, int e1, int e2); 00184 00185 /// Attach a pixel to existing entries and mark these entries as unificable. 00186 void attachPixelThreeEntries(int i, int j, int e1, int e2, int e3); 00187 00188 /// Process executed after an end of line. 00189 void endOfLineProcess(int j); 00190 00191 /// Add 2 entries in the equivalence table. 00192 void addEquivEntries(int e1, int e2); 00193 00194 /// Unify entries matching the same connected component. 00195 void unification(); 00196 00197 /// Search small connected components and remove them from the image. 00198 void searchAndRemoveSmallComponents(int j); 00199 00200 /// Remove a connected component recursively, by starting from one of its points. 00201 void removeComponent(int x, int y); 00202 00203 /// Return true if it exists a black neighbor connected to the argument point. 00204 bool isNeighbor(int i, int j, int * a, int * b); 00205 00206 // ------------------------------------------------------------------- 00207 }; // class PruneSmallCCBinaryImage 00208 00209 } // namespace qgar 00210 00211 00212 #endif /* __PRUNESMALLCCBINARYIMAGE_H_INCLUDED__ */