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 <qvip.h>
00030
00031 #define NULL_NODE 256*256*256
00032
00100 class QVComponentTree
00101 {
00102 public:
00116 QVComponentTree(const QVImage<uChar,1> &image, bool inverseTree= false, bool useAlternative = false);
00117
00120 bool isInverseTree() const { return inverseTree; }
00121
00128 uInt & rootNode() { return rootNodeID; }
00129
00139 uInt & seedX(uInt index) { return nodes[index].seedX; }
00140
00150 uInt & seedY(uInt index) { return nodes[index].seedY; }
00151
00158 uChar & firstThreshold(uInt index) { return nodes[index].firstThreshold; }
00159
00166 uChar & lastThreshold(uInt index) { return nodes[index].lastThreshold; }
00167
00171 uInt & numChilds(uInt index) { return nodes[index].numChilds; }
00172
00176 uInt & firstChild(uInt index) { return nodes[index].child; }
00177
00181 uInt & nextSibling(uInt index) { return nodes[index].sibling; }
00182
00200 uInt *area(uInt index) { return nodes[index].area; }
00201
00204 uInt getNumNodes() const { return numNodes; }
00205
00208 uInt getLeafNodes() const { return leafNodes; }
00209
00214 uInt getTotalPoints() const { return totalPoints; }
00215
00216 private:
00217 void getComponentTree(const QVImage<uChar> &image);
00218
00219 uInt numNodes, freePoints, totalPoints, leafNodes, rootNodeID, maxNodes;
00220 bool inverseTree;
00221
00223 bool & closedNode(uInt index) { return nodes[index].closed; }
00224
00225 uInt newNode(uInt SeedX, uInt SeedY, uChar Threshold)
00226 {
00227 uInt newNodeID = this->numNodes++;
00228
00229 seedX(newNodeID) = SeedX;
00230 seedY(newNodeID) = SeedY;
00231 firstThreshold(newNodeID) = lastThreshold(newNodeID) = Threshold;
00232 firstChild(newNodeID) = nextSibling(newNodeID) = NULL_NODE;
00233 numChilds(newNodeID) = 0;
00234 area(newNodeID)[Threshold] = 0;
00235 closedNode(newNodeID) = false;
00236
00237 return newNodeID;
00238 }
00239
00240 void addChild(uInt ParentNodeID, uInt ChildNodeID)
00241 {
00242 nextSibling(ChildNodeID) = firstChild(ParentNodeID);
00243 firstChild(ParentNodeID) = ChildNodeID;
00244 numChilds(ParentNodeID)++;
00245 }
00246
00247 void mergeNodes(uInt actualNodeID, uInt vecinoNodeID)
00248 {
00249 uInt next, lastActualChildNodeID = firstChild(actualNodeID);
00250 while ((next=nextSibling(lastActualChildNodeID)) != NULL_NODE)
00251 lastActualChildNodeID = next;
00252
00253 numChilds(actualNodeID) += numChilds(vecinoNodeID);
00254 nextSibling(lastActualChildNodeID) = firstChild(vecinoNodeID);
00255 }
00256
00257 class QVComponentTreeNode
00258 {
00259 public:
00260 uInt seedX, seedY;
00261 uInt child, sibling, numChilds;
00262 uChar firstThreshold, lastThreshold;
00263 uInt area[256];
00264 bool closed;
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287 };
00288
00289 QVector<QVComponentTreeNode> nodes;
00290
00291
00292 };
00293
00294