src/qvip/qvimagefeatures/qvcomponenttree.h

Go to the documentation of this file.
00001 /*
00002  *      Copyright (C) 2007, 2008. PARP Research Group.
00003  *      <http://perception.inf.um.es>
00004  *      University of Murcia, Spain.
00005  *
00006  *      This file is part of the QVision library.
00007  *
00008  *      QVision is free software: you can redistribute it and/or modify
00009  *      it under the terms of the GNU Lesser General Public License as
00010  *      published by the Free Software Foundation, version 3 of the License.
00011  *
00012  *      QVision is distributed in the hope that it will be useful,
00013  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *      GNU Lesser General Public License for more details.
00016  *
00017  *      You should have received a copy of the GNU Lesser General Public
00018  *      License along with QVision. If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 
00024 
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <iostream>
00028 
00029 #include <qvdta.h>
00030 
00031 #define  NULL_NODE      256*256*256
00032 
00101 class QVComponentTree
00102         {
00103         public:
00117                 QVComponentTree(const QVImage<uChar,1> &image, bool inverseTree= false, bool useAlternative = false);
00118 
00121                 bool isInverseTree()    const           { return inverseTree; }
00122 
00129                 uInt & rootNode()                       { return rootNodeID; }
00130 
00140                 uInt & seedX(uInt index)                { return nodes[index].seedX; }
00141 
00151                 uInt & seedY(uInt index)                { return nodes[index].seedY; }
00152 
00159                 uChar & firstThreshold(uInt index)      { return nodes[index].firstThreshold; }
00160 
00167                 uChar & lastThreshold(uInt index)       { return nodes[index].lastThreshold; }
00168 
00172                 uInt & numChilds(uInt index)            { return nodes[index].numChilds; }
00173 
00177                 uInt & firstChild(uInt index)           { return nodes[index].child; }
00178 
00182                 uInt & nextSibling(uInt index)          { return nodes[index].sibling; }
00183 
00201                 uInt *area(uInt index)                  { return nodes[index].area; }
00202 
00205                 uInt getNumNodes() const        { return numNodes; }
00206 
00209                 uInt getLeafNodes() const       { return leafNodes; }
00210 
00215                 uInt getTotalPoints() const     { return totalPoints; }
00216 
00217         private:
00218                 void getComponentTree(const QVImage<uChar> &image);
00219 
00220                 uInt numNodes, freePoints, totalPoints, leafNodes, rootNodeID, maxNodes;
00221                 bool inverseTree;
00222 
00224                 bool & closedNode(uInt index)           { return nodes[index].closed; }
00225 
00226                 uInt newNode(uInt SeedX, uInt SeedY, uChar Threshold)
00227                         {
00228                         uInt newNodeID = this->numNodes++;
00229 
00230                         seedX(newNodeID) = SeedX;
00231                         seedY(newNodeID) = SeedY;
00232                         firstThreshold(newNodeID) = lastThreshold(newNodeID) = Threshold;
00233                         firstChild(newNodeID) =  nextSibling(newNodeID) = NULL_NODE;
00234                         numChilds(newNodeID) = 0;
00235                         area(newNodeID)[Threshold] = 0;
00236                         closedNode(newNodeID) = false;
00237                         
00238                         return newNodeID;
00239                         }
00240 
00241                 void addChild(uInt ParentNodeID, uInt ChildNodeID)
00242                         {
00243                         nextSibling(ChildNodeID) = firstChild(ParentNodeID);
00244                         firstChild(ParentNodeID) = ChildNodeID;
00245                         numChilds(ParentNodeID)++;
00246                         }
00247 
00248                 void mergeNodes(uInt actualNodeID, uInt vecinoNodeID)
00249                         {
00250                         uInt next, lastActualChildNodeID = firstChild(actualNodeID);
00251                         while ((next=nextSibling(lastActualChildNodeID)) != NULL_NODE)
00252                                 lastActualChildNodeID = next;
00253 
00254                         numChilds(actualNodeID) += numChilds(vecinoNodeID);
00255                         nextSibling(lastActualChildNodeID) = firstChild(vecinoNodeID);
00256                         }
00257 
00258                 class QVComponentTreeNode
00259                         {
00260                         public:
00261                                 uInt seedX, seedY;
00262                                 uInt child, sibling, numChilds;
00263                                 uChar firstThreshold, lastThreshold;
00264                                 uInt area[256];
00265                                 bool closed;
00266 
00267                                 /*bool operator==(const QVComponentTreeNode &other) const
00268                                         {
00269                                         if (seedX != other.seedX)
00270                                                 return false;
00271                                         if (seedY != other.seedY)
00272                                                 return false;
00273                                         //if (child != other.child)
00274                                         //      return false;
00275                                         //if (sibling != other.sibling)
00276                                         //      return false;
00277                                         if (numChilds != other.numChilds)
00278                                                 return false;
00279                                         if (firstThreshold != other.firstThreshold)
00280                                                 return false;
00281                                         if (lastThreshold != other.lastThreshold)
00282                                                 return false;
00283                                         for (int i = firstThreshold; i < lastThreshold; i++)
00284                                                 if (area[i] != other.area[i])
00285                                                         return false;
00286                                         return true;
00287                                         }*/
00288                         };
00289 
00290                 QVector<QVComponentTreeNode> nodes;
00291 
00292                 //void testComponentTree(const QVImage<uChar,1> &image, QVDisjointSet &disjointSet);
00293         };
00294 
00295