00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef QVCOMPONENTTREE_H
00022 #define QVCOMPONENTTREE_H
00023
00027
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 #include <iostream>
00031
00032 #include <qvip.h>
00033
00034 #define NULL_NODE 256*256*256
00035
00103 class QVComponentTree
00104 {
00105 public:
00119 QVComponentTree(const QVImage<uChar,1> &image, bool inverseTree= false, bool useAlternative = false);
00120
00123 bool isInverseTree() const { return inverseTree; }
00124
00131 uInt & rootNode() { return rootNodeID; }
00132
00142 uInt & seedX(uInt index) { return nodes[index].seedX; }
00143
00153 uInt & seedY(uInt index) { return nodes[index].seedY; }
00154
00161 uChar & firstThreshold(uInt index) { return nodes[index].firstThreshold; }
00162
00169 uChar & lastThreshold(uInt index) { return nodes[index].lastThreshold; }
00170
00174 uInt & numChilds(uInt index) { return nodes[index].numChilds; }
00175
00179 uInt & firstChild(uInt index) { return nodes[index].child; }
00180
00184 uInt & nextSibling(uInt index) { return nodes[index].sibling; }
00185
00203 uInt *area(uInt index) { return nodes[index].area; }
00204
00207 uInt getNumNodes() const { return numNodes; }
00208
00211 uInt getLeafNodes() const { return leafNodes; }
00212
00217 uInt getTotalPoints() const { return totalPoints; }
00218
00219 private:
00220 void getComponentTree(const QVImage<uChar> &image);
00221
00222 uInt numNodes, freePoints, totalPoints, leafNodes, rootNodeID, maxNodes;
00223 bool inverseTree;
00224
00226 bool & closedNode(uInt index) { return nodes[index].closed; }
00227
00228 uInt newNode(uInt SeedX, uInt SeedY, uChar Threshold)
00229 {
00230 uInt newNodeID = this->numNodes++;
00231
00232 seedX(newNodeID) = SeedX;
00233 seedY(newNodeID) = SeedY;
00234 firstThreshold(newNodeID) = lastThreshold(newNodeID) = Threshold;
00235 firstChild(newNodeID) = nextSibling(newNodeID) = NULL_NODE;
00236 numChilds(newNodeID) = 0;
00237 area(newNodeID)[Threshold] = 0;
00238 closedNode(newNodeID) = false;
00239
00240 return newNodeID;
00241 }
00242
00243 void addChild(uInt ParentNodeID, uInt ChildNodeID)
00244 {
00245 nextSibling(ChildNodeID) = firstChild(ParentNodeID);
00246 firstChild(ParentNodeID) = ChildNodeID;
00247 numChilds(ParentNodeID)++;
00248 }
00249
00250 void mergeNodes(uInt actualNodeID, uInt vecinoNodeID)
00251 {
00252 uInt next, lastActualChildNodeID = firstChild(actualNodeID);
00253 while ((next=nextSibling(lastActualChildNodeID)) != NULL_NODE)
00254 lastActualChildNodeID = next;
00255
00256 numChilds(actualNodeID) += numChilds(vecinoNodeID);
00257 nextSibling(lastActualChildNodeID) = firstChild(vecinoNodeID);
00258 }
00259
00260 class QVComponentTreeNode
00261 {
00262 public:
00263 uInt seedX, seedY;
00264 uInt child, sibling, numChilds;
00265 uChar firstThreshold, lastThreshold;
00266 uInt area[256];
00267 bool closed;
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 };
00291
00292 QVector<QVComponentTreeNode> nodes;
00293
00294
00295 };
00296
00297 #endif //IFNDEF