00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288 };
00289
00290 QVector<QVComponentTreeNode> nodes;
00291
00292
00293 };
00294
00295