34 #ifndef _DENDROGRAM_HPP
35 #define _DENDROGRAM_HPP
37 #include "DendroNode.hpp"
46 template <
class MarkerLabelT = size_t,
class NodeT = size_t,
47 class ValGraphT =
size_t>
53 std::vector<DendroNode<MarkerLabelT> *> dendroNodes;
59 typedef std::map<NodeT, ValGraphT> NodeValuesType;
60 typedef std::vector<Edge<NodeT, ValGraphT>> EdgeListType;
63 Dendrogram() : nbrNodes(0), nbrMarkers(0), dendroNodes(0){};
67 nbrNodes = dendrogramToCopy
69 nbrMarkers = dendrogramToCopy.nbrMarkers;
70 std::vector<DendroNodeType *> dendroNodesToCopy =
71 dendrogramToCopy.dendroNodes;
72 for (
size_t i = 0; i < dendrogramToCopy.dendroNodes.size(); i++) {
75 for (
size_t i = 0; i < nbrNodes; i++) {
79 newNode.setLabel(curNode.getLabel());
80 newNode.setMarker(curNode.getMarker());
81 newNode.setNbMarkersUnder(curNode.getNbMarkersUnder());
82 newNode.setMarkersCount(curNode.getMarkersCount());
83 newNode.setValuation(curNode.getValuation());
84 newNode.setLeafValuation(curNode.getLeafValuation());
85 newNode.setInternalNodeValuationInitial(
87 newNode.setInternalNodeValuationFinal(
88 curNode.getInternalNodeValuationFinal());
89 newNode.setIsInternalNode(curNode.getIsInternalNode());
90 newNode.setLookupProgeny(curNode.getLookupProgeny());
91 newNode.setMoments(curNode.getMoments());
92 newNode.setContoursCount(curNode.getContoursCount());
93 newNode.setContoursSize(curNode.getContoursSize());
95 for (
size_t i = 0; i < nbrNodes; i++) {
97 MarkerLabelT researchedLabel = newNode.getLabel();
100 *dendrogramToCopy.researchLabel(researchedLabel);
101 MarkerLabelT fatherLabel = correspondingNode.getFather()->getLabel();
102 newNode.setFather(this->researchLabel(fatherLabel));
104 if (correspondingNode.getIsInternalNode() ==
true) {
105 MarkerLabelT childLeftLabel =
106 correspondingNode.getChildLeft()->getLabel();
107 MarkerLabelT childRightLabel =
108 correspondingNode.getChildRight()->getLabel();
109 MarkerLabelT neighborLeftLabel =
110 correspondingNode.getNeighborLeft()->getLabel();
111 MarkerLabelT neighborRightLabel =
112 correspondingNode.getNeighborRight()->getLabel();
113 newNode.setChildLeft(this->researchLabel(childLeftLabel));
114 newNode.setChildRight(this->researchLabel(childRightLabel));
115 newNode.setNeighborLeft(this->researchLabel(neighborLeftLabel));
116 newNode.setNeighborRight(this->researchLabel(neighborRightLabel));
127 : nbrNodes(0), nbrMarkers(0), dendroNodes(0)
135 EdgeListType & mstEdges = mst.
getEdges();
138 nbrNodes = leavesNbr + internalNodesNbr;
140 for (
size_t i = 0; i < nbrNodes; i++) {
142 dendroNodes.push_back(nNode);
145 for (
size_t i = 0; i < leavesNbr; i++) {
151 curNode.setLabel(mstNodes.find(i)->first);
152 curNode.setLeafValuation(mstNodes.find(i)->second);
153 curNode.setValuation(mstNodes.find(i)->second);
156 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
157 nLookupProgeny.at(mstNodes.find(i)->first) = 1;
158 curNode.setLookupProgeny(nLookupProgeny);
160 std::vector<MarkerLabelT> initContoursCount(leavesNbr, 0);
161 curNode.setContoursCount(initContoursCount);
165 for (
size_t i = 0; i < internalNodesNbr; i++) {
169 curNode.setIsInternalNode(1);
170 curNode.setLabel(leavesNbr + i);
171 curNode.setInternalNodeValuationInitial(mstEdges[i].weight);
172 MarkerLabelT NeighborLeftLabel =
173 min(mstEdges[i].source, mstEdges[i].target);
174 MarkerLabelT NeighborRightLabel =
175 max(mstEdges[i].source, mstEdges[i].target);
178 curNode.setNeighborLeft(dendroNodes[NeighborLeftLabel]);
180 curNode.setNeighborRight(dendroNodes[NeighborRightLabel]);
182 curNode.setChildLeft(curNode.getNeighborLeft()->getAncestor());
183 curNode.setChildRight(curNode.getNeighborRight()->getAncestor());
185 curNode.getChildLeft()->setFather(&curNode);
186 curNode.getChildRight()->setFather(&curNode);
188 std::vector<MarkerLabelT> lookupChildLeft =
189 curNode.getChildLeft()->getLookupProgeny();
190 std::vector<MarkerLabelT> lookupChildRight =
191 curNode.getChildRight()->getLookupProgeny();
192 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
193 for (MarkerLabelT i = 0; i < lookupChildLeft.size(); i++) {
194 nLookupProgeny.at(i) =
195 max(lookupChildRight.at(i), lookupChildLeft.at(i));
199 curNode.setLookupProgeny(nLookupProgeny);
202 DendroNodeType &lastNode = *dendroNodes[leavesNbr + internalNodesNbr - 1];
203 lastNode.setFather(&lastNode);
220 : nbrNodes(0), nbrMarkers(0), dendroNodes(0)
228 EdgeListType & mstEdges = mst.
getEdges();
231 nbrNodes = leavesNbr + internalNodesNbr;
233 for (
size_t i = 0; i < nbrNodes; i++) {
235 dendroNodes.push_back(nNode);
238 for (
size_t i = 0; i < leavesNbr; i++) {
243 curNode.setLabel(mstNodes.find(i)->first);
244 curNode.setLeafValuation(mstNodes.find(i)->second);
245 curNode.setValuation(mstNodes.find(i)->second);
247 std::vector<double> initMoments(nbOfMoments);
248 curNode.setMoments(initMoments);
251 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
252 nLookupProgeny.at(mstNodes.find(i)->first) = 1;
253 curNode.setLookupProgeny(nLookupProgeny);
255 std::vector<MarkerLabelT> initContoursCount(leavesNbr, 0);
256 curNode.setContoursCount(initContoursCount);
260 for (
size_t i = 0; i < internalNodesNbr; i++) {
264 curNode.setIsInternalNode(1);
265 curNode.setLabel(leavesNbr + i);
266 curNode.setInternalNodeValuationInitial(mstEdges[i].weight);
267 MarkerLabelT NeighborLeftLabel =
268 min(mstEdges[i].source, mstEdges[i].target);
269 MarkerLabelT NeighborRightLabel =
270 max(mstEdges[i].source, mstEdges[i].target);
273 curNode.setNeighborLeft(dendroNodes[NeighborLeftLabel]);
275 curNode.setNeighborRight(dendroNodes[NeighborRightLabel]);
277 curNode.setChildLeft(curNode.getNeighborLeft()->getAncestor());
278 curNode.setChildRight(curNode.getNeighborRight()->getAncestor());
280 curNode.getChildLeft()->setFather(&curNode);
281 curNode.getChildRight()->setFather(&curNode);
283 std::vector<MarkerLabelT> lookupChildLeft =
284 curNode.getChildLeft()->getLookupProgeny();
285 std::vector<MarkerLabelT> lookupChildRight =
286 curNode.getChildRight()->getLookupProgeny();
287 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
288 for (MarkerLabelT i = 0; i < lookupChildLeft.size(); i++) {
289 nLookupProgeny.at(i) =
290 max(lookupChildRight.at(i), lookupChildLeft.at(i));
294 curNode.setLookupProgeny(nLookupProgeny);
297 DendroNodeType &lastNode = *dendroNodes[leavesNbr + internalNodesNbr - 1];
299 lastNode.setFather(&lastNode);
302 this->setMomentsContours(imLabels, imIn, nbOfMoments);
308 for (
size_t i = 0; i < nbrNodes; i++) {
309 delete dendroNodes[i];
321 std::stable_sort(dendroNodes.begin(), dendroNodes.end(),
322 DendroNodeType::isInferior);
324 std::stable_sort(dendroNodes.begin(), dendroNodes.end(),
325 DendroNodeType::isSuperior);
330 std::stable_sort(dendroNodes.begin(), dendroNodes.end(),
331 DendroNodeType::isSuperior);
333 void addDendroNodes(DendroNodeType *dendroNode)
335 dendroNodes.push_back(dendroNode);
337 DendroNodeType *researchLabel(MarkerLabelT researchedLabel)
const
339 DendroNodeType *dendronodeToReturn(0);
340 for (
size_t i = 0; i < nbrNodes; i++) {
341 DendroNodeType &curNode = *dendroNodes[i];
342 if (curNode.getLabel() == researchedLabel) {
343 dendronodeToReturn = dendroNodes[i];
346 return dendronodeToReturn;
480 void setMomentsContours(Image<MarkerLabelT> &imLabels,
481 Image<MarkerLabelT> &imIn,
const size_t nbOfMoments)
483 size_t leavesNbr = nbrNodes / 2 + 1;
485 std::map<MarkerLabelT, std::vector<double>> lookupMapMoments;
486 std::map<MarkerLabelT, std::vector<MarkerLabelT>> lookupMapContours;
488 imLabels.getSize(sizeIm);
492 for (
size_t i = 0; i < sizeIm[0]; i++) {
493 for (
size_t j = 0; j < sizeIm[1]; j++) {
494 MarkerLabelT labelValue = imLabels.getPixel(i, j);
495 if (lookupMapMoments.count(labelValue) == 0) {
496 std::vector<double> initMoments(nbOfMoments);
497 lookupMapMoments[labelValue] = initMoments;
498 std::vector<MarkerLabelT> initContours(leavesNbr, 0);
499 lookupMapContours[labelValue] = initContours;
505 for (
size_t i = 0; i < sizeIm[0]; i++) {
506 for (
size_t j = 0; j < sizeIm[1]; j++) {
507 MarkerLabelT imValue = imIn.getPixel(i, j);
508 MarkerLabelT labelValue = imLabels.getPixel(i, j);
509 for (
size_t k = 0; k < nbOfMoments; k++) {
510 lookupMapMoments[labelValue][k] =
511 lookupMapMoments[labelValue][k] +
std::pow(imValue, k);
518 for (
size_t i = 1; i < sizeIm[0] - 1; i++) {
519 for (
size_t j = 1; j < sizeIm[1] - 1; j++) {
520 MarkerLabelT labelValue = imLabels.getPixel(i, j);
521 if (imLabels.getPixel(i - 1, j - 1) != labelValue) {
522 MarkerLabelT newLabel = imLabels.getPixel(i - 1, j - 1);
523 lookupMapContours[labelValue].at(newLabel) =
524 lookupMapContours[labelValue].at(newLabel) + 1;
525 }
else if (imLabels.getPixel(i - 1, j) != labelValue) {
526 MarkerLabelT newLabel = imLabels.getPixel(i - 1, j);
527 lookupMapContours[labelValue].at(newLabel) =
528 lookupMapContours[labelValue].at(newLabel) + 1;
529 }
else if (imLabels.getPixel(i - 1, j + 1) != labelValue) {
530 MarkerLabelT newLabel = imLabels.getPixel(i - 1, j + 1);
531 lookupMapContours[labelValue].at(newLabel) =
532 lookupMapContours[labelValue].at(newLabel) + 1;
533 }
else if (imLabels.getPixel(i, j - 1) != labelValue) {
534 MarkerLabelT newLabel = imLabels.getPixel(i, j - 1);
535 lookupMapContours[labelValue].at(newLabel) =
536 lookupMapContours[labelValue].at(newLabel) + 1;
537 }
else if (imLabels.getPixel(i, j) != labelValue) {
538 MarkerLabelT newLabel = imLabels.getPixel(i, j);
539 lookupMapContours[labelValue].at(newLabel) =
540 lookupMapContours[labelValue].at(newLabel) + 1;
541 }
else if (imLabels.getPixel(i, j + 1) != labelValue) {
542 MarkerLabelT newLabel = imLabels.getPixel(i, j + 1);
543 lookupMapContours[labelValue].at(newLabel) =
544 lookupMapContours[labelValue].at(newLabel) + 1;
545 }
else if (imLabels.getPixel(i + 1, j - 1) != labelValue) {
546 MarkerLabelT newLabel = imLabels.getPixel(i + 1, j - 1);
547 lookupMapContours[labelValue].at(newLabel) =
548 lookupMapContours[labelValue].at(newLabel) + 1;
549 }
else if (imLabels.getPixel(i + 1, j) != labelValue) {
550 MarkerLabelT newLabel = imLabels.getPixel(i + 1, j);
551 lookupMapContours[labelValue].at(newLabel) =
552 lookupMapContours[labelValue].at(newLabel) + 1;
553 }
else if (imLabels.getPixel(i + 1, j + 1) != labelValue) {
554 MarkerLabelT newLabel = imLabels.getPixel(i + 1, j + 1);
555 lookupMapContours[labelValue].at(newLabel) =
556 lookupMapContours[labelValue].at(newLabel) + 1;
562 for (
typename std::map<MarkerLabelT, std::vector<double>>::iterator iter =
563 lookupMapMoments.begin();
564 iter != lookupMapMoments.end(); ++iter) {
565 MarkerLabelT researchedLabel = iter->first;
566 std::vector<double> momentsValues = iter->second;
574 researchLabel(researchedLabel)->setMoments(momentsValues);
578 for (
typename std::map<MarkerLabelT, std::vector<MarkerLabelT>>::iterator
579 iter = lookupMapContours.begin();
580 iter != lookupMapContours.end(); ++iter) {
581 MarkerLabelT researchedLabel = iter->first;
582 std::vector<MarkerLabelT> contoursCountValues = iter->second;
590 researchLabel(researchedLabel)->setContoursCount(contoursCountValues);
597 size_t leavesNbr = nbrNodes / 2 + 1;
598 size_t internalNodesNbr = nbrNodes / 2;
600 for (
size_t i = 0; i < nbrNodes; i++) {
601 DendroNodeType &curNode = *dendroNodes[i];
602 curNode.setChildLeft(0);
603 curNode.setChildRight(0);
604 curNode.setFather(0);
605 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
606 curNode.setLookupProgeny(nLookupProgeny);
608 if (curNode.getIsInternalNode()) {
609 std::vector<MarkerLabelT> nMarkersCount(nbrMarkers, 0);
610 curNode.setMarkersCount(nMarkersCount);
614 for (
size_t i = 0; i < nbrNodes; i++) {
615 DendroNodeType &curNode = *dendroNodes[i];
617 if (!curNode.getIsInternalNode()) {
618 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
619 nLookupProgeny.at(curNode.getLabel()) = 1;
622 curNode.setLookupProgeny(nLookupProgeny);
628 for (
size_t i = 0; i < nbrNodes; i++) {
629 DendroNodeType &curNode = *dendroNodes[i];
630 if (curNode.getIsInternalNode()) {
631 curNode.setChildLeft(curNode.getNeighborLeft()->getAncestor());
632 curNode.setChildRight(curNode.getNeighborRight()->getAncestor());
634 curNode.getChildLeft()->setFather(&curNode);
635 curNode.getChildRight()->setFather(&curNode);
638 std::vector<MarkerLabelT> lookupChildLeft =
639 curNode.getChildLeft()->getLookupProgeny();
640 std::vector<MarkerLabelT> lookupChildRight =
641 curNode.getChildRight()->getLookupProgeny();
642 std::vector<MarkerLabelT> nLookupProgeny(leavesNbr, 0);
643 for (
size_t i = 0; i < lookupChildLeft.size(); i++) {
644 nLookupProgeny.at(i) =
645 max(lookupChildRight.at(i), lookupChildLeft.at(i));
649 curNode.setLookupProgeny(nLookupProgeny);
652 MarkerLabelT nbMarkersUnderLeft =
653 curNode.getChildLeft()->getNbMarkersUnder();
654 MarkerLabelT nbMarkersUnderRight =
655 curNode.getChildRight()->getNbMarkersUnder();
656 curNode.setNbMarkersUnder(nbMarkersUnderLeft + nbMarkersUnderRight);
659 std::vector<MarkerLabelT> markersCountLeft =
660 curNode.getChildLeft()->getMarkersCount();
661 std::vector<MarkerLabelT> markersCountRight =
662 curNode.getChildRight()->getMarkersCount();
663 std::vector<MarkerLabelT> nMarkersCount(nbrMarkers, 0);
664 for (
size_t i = 0; i < markersCountLeft.size(); i++) {
665 nMarkersCount.at(i) =
666 markersCountLeft.at(i) + markersCountRight.at(i);
668 curNode.setMarkersCount(nMarkersCount);
671 std::vector<double> momentsChildLeft =
672 curNode.getChildLeft()->getMoments();
673 std::vector<double> momentsChildRight =
674 curNode.getChildRight()->getMoments();
675 std::vector<double> nMoments(momentsChildLeft.size(), 0);
676 for (
size_t i = 0; i < momentsChildLeft.size(); i++) {
677 nMoments.at(i) = momentsChildLeft.at(i) + momentsChildRight.at(i);
680 curNode.setMoments(nMoments);
685 std::vector<MarkerLabelT> contoursCountLeft =
686 curNode.getChildLeft()->getContoursCount();
687 std::vector<MarkerLabelT> contoursCountRight =
688 curNode.getChildRight()->getContoursCount();
690 std::vector<MarkerLabelT> nContoursCount(leavesNbr, 0);
691 for (
size_t i = 0; i < contoursCountLeft.size(); i++) {
692 nContoursCount.at(i) =
693 contoursCountLeft.at(i) + contoursCountRight.at(i);
697 curNode.setContoursCount(nContoursCount);
699 double nContoursSize = 0;
700 for (
size_t i = 0; i < contoursCountLeft.size(); i++) {
701 nContoursSize = nContoursSize +
702 nContoursCount.at(i) * (1 - nLookupProgeny.at(i));
704 curNode.setContoursSize(nContoursSize);
707 else if (!curNode.getIsInternalNode()) {
708 std::vector<MarkerLabelT> lookupProgeny = curNode.getLookupProgeny();
709 std::vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
711 double nContoursSize = 0;
712 for (
size_t i = 0; i < contoursCount.size(); i++) {
714 nContoursSize + contoursCount.at(i) * (1 - lookupProgeny.at(i));
716 curNode.setContoursSize(nContoursSize);
720 DendroNodeType &lastNode = *dendroNodes[leavesNbr + internalNodesNbr - 1];
721 lastNode.setFather(&lastNode);
731 void normalize(
const std::string typeOfNormalization =
"reg")
734 size_t internalNodesNbr = nbrNodes / 2;
736 if (typeOfNormalization ==
"reg") {
741 for (
size_t i = 0; i < nbrNodes; i++) {
743 if (curNode.getIsInternalNode()) {
744 double newVal = counter / internalNodesNbr;
745 curNode.setInternalNodeValuationFinal(newVal);
746 counter = counter + 1;
749 this->putValuationsFinalInInitial();
751 }
else if (typeOfNormalization ==
"maxnorm") {
753 this->sortNodes(
true);
757 for (
size_t i = 0; i < nbrNodes; i++) {
766 for (
size_t i = 0; i < nbrNodes; i++) {
768 if (curNode.getIsInternalNode()) {
771 curNode.setInternalNodeValuationFinal(newVal);
774 this->putValuationsFinalInInitial();
777 std::cerr <<
"normalize(const std::string typeOfNormalization) -> "
778 "typeOfNormalization must be chosen within: "
779 <<
"reg, maxnorm" << endl;
787 for (
size_t i = 0; i < nbrNodes; i++) {
789 if (curNode.getIsInternalNode() == 1) {
790 double temp = curNode.getInternalNodeValuationFinal();
791 curNode.setInternalNodeValuationInitial(temp);
792 curNode.setInternalNodeValuationFinal(0);
796 void setValuationsToZero()
798 for (
size_t i = 0; i < nbrNodes; i++) {
799 DendroNodeType &curNode = *dendroNodes[i];
800 if (curNode.getIsInternalNode() == 1) {
801 curNode.setValuation(0);
803 if (curNode.getIsInternalNode() == 0) {
804 curNode.setValuation(curNode.getLeafValuation());
815 std::vector<DendroNodeType *> &dendroNodes = this->getDendroNodes();
816 size_t nodeNbr = this->getNbrNodes();
819 for (
size_t i = 0; i < nodeNbr; i++) {
822 curNode.getIsInternalNode() ==
true) {
823 MarkerLabelT srcToRemove = curNode.getNeighborLeft()->getLabel();
824 MarkerLabelT targetToRemove = curNode.getNeighborRight()->getLabel();
825 associated_mst.
removeEdge(srcToRemove, targetToRemove);
826 associated_mst.
removeEdge(targetToRemove, srcToRemove);
845 const std::string typeOfHierarchy,
const int nParam = 50,
847 const std::string typeOfTransform =
"erode",
848 const StrElt & se = DEFAULT_SE)
852 std::vector<DendroNodeType *> &dendroNodes = this->getDendroNodes();
853 if (typeOfHierarchy ==
"none") {
854 }
else if (typeOfHierarchy ==
"surfacic") {
857 for (
size_t i = 0; i < dendroNodes.size(); i++) {
860 if (curNode.getIsInternalNode() == 1) {
861 if (curNode.getValuation() == 0) {
862 double childLeftValuation =
863 curNode.getChildLeft()->getValuation();
864 double childRightValuation =
865 curNode.getChildRight()->getValuation();
866 curNode.setValuation(childLeftValuation + childRightValuation);
867 curNode.setInternalNodeValuationFinal(
868 fmin(childLeftValuation, childRightValuation));
870 curNode.setInternalNodeValuationFinal(
875 this->putValuationsFinalInInitial();
877 this->setValuationsToZero();
878 }
else if (typeOfHierarchy ==
"surfacicImageReturn") {
881 for (
size_t i = 0; i < dendroNodes.size(); i++) {
884 if (curNode.getIsInternalNode() == 1) {
885 std::vector<MarkerLabelT> lookupChildLeft =
886 curNode.getChildLeft()->getLookupProgeny();
887 std::vector<MarkerLabelT> lookupChildRight =
888 curNode.getChildRight()->getLookupProgeny();
890 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
891 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
892 lookupMapChildLeft[j] = lookupChildLeft.at(j);
895 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
896 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
897 lookupMapChildRight[j] = lookupChildRight.at(j);
903 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
904 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
906 if (typeOfTransform ==
"erode") {
907 erode(imTmpLeft, imTmpLeft, se);
908 erode(imTmpRight, imTmpRight, se);
909 }
else if (typeOfTransform ==
"dilate") {
910 dilate(imTmpLeft, imTmpLeft, se);
911 dilate(imTmpRight, imTmpRight, se);
912 }
else if (typeOfTransform ==
"open") {
913 open(imTmpLeft, imTmpLeft, se);
914 open(imTmpRight, imTmpRight, se);
915 }
else if (typeOfTransform ==
"close") {
916 close(imTmpLeft, imTmpLeft, se);
917 close(imTmpRight, imTmpRight, se);
919 cout <<
"Please choose typeOfTransform in the following: erode, "
920 "dilate, open, close"
923 double childLeftSurf =
area(imTmpLeft);
924 double childRightSurf =
area(imTmpRight);
926 double childLeftValuation = childLeftSurf;
927 double childRightValuation = childRightSurf;
928 curNode.setValuation(childLeftValuation + childRightValuation);
929 curNode.setInternalNodeValuationFinal(
930 fmin(childLeftValuation, childRightValuation));
933 this->putValuationsFinalInInitial();
935 this->setValuationsToZero();
936 }
else if (typeOfHierarchy ==
"stochasticSurfacic") {
938 for (
size_t i = 0; i < dendroNodes.size(); i++) {
941 if (curNode.getIsInternalNode() == 1) {
942 if (curNode.getValuation() == 0) {
943 double childLeftValuation =
944 curNode.getChildLeft()->getValuation();
945 double childRightValuation =
946 curNode.getChildRight()->getValuation();
947 curNode.setValuation(childLeftValuation + childRightValuation);
949 curNode.setInternalNodeValuationFinal(
957 double totalSurface = curNodeTmp.getAncestor()->getValuation();
960 for (
size_t i = 0; i < dendroNodes.size(); i++) {
963 if (curNode.getIsInternalNode() == 1) {
964 double childLeftSurf = curNode.getChildLeft()->getValuation();
965 double childRightSurf = curNode.getChildRight()->getValuation();
967 1 -
pow(1 - (childLeftSurf / totalSurface), nParam) -
968 pow(1 - (childRightSurf / totalSurface), nParam) +
969 pow(1 - ((childLeftSurf + childRightSurf) / totalSurface),
972 newVal = 0.000000001;
974 curNode.setInternalNodeValuationFinal(newVal);
977 this->putValuationsFinalInInitial();
979 this->setValuationsToZero();
980 }
else if (typeOfHierarchy ==
"stochasticExtinctionSurfacic") {
982 for (
size_t i = 0; i < dendroNodes.size(); i++) {
985 if (curNode.getIsInternalNode() == 1) {
986 if (curNode.getValuation() == 0) {
987 double childLeftValuation =
988 curNode.getChildLeft()->getValuation();
989 double childRightValuation =
990 curNode.getChildRight()->getValuation();
991 curNode.setValuation(childLeftValuation + childRightValuation);
993 curNode.setInternalNodeValuationFinal(
1001 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1004 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1007 if (curNode.getIsInternalNode() == 1) {
1008 double childLeftSurf = curNode.getChildLeft()->getValuation();
1009 double childRightSurf = curNode.getChildRight()->getValuation();
1012 pow(1 - std::min(childLeftSurf, childRightSurf) / totalSurface,
1014 curNode.setInternalNodeValuationFinal(newVal);
1017 this->putValuationsFinalInInitial();
1019 this->setValuationsToZero();
1020 }
else if (typeOfHierarchy ==
"stochasticSurfacicImageReturn") {
1022 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1025 if (curNode.getIsInternalNode() == 1) {
1026 if (curNode.getValuation() == 0) {
1027 double childLeftValuation =
1028 curNode.getChildLeft()->getValuation();
1029 double childRightValuation =
1030 curNode.getChildRight()->getValuation();
1031 curNode.setValuation(childLeftValuation + childRightValuation);
1038 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1041 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1044 if (curNode.getIsInternalNode() == 1) {
1045 std::vector<MarkerLabelT> lookupChildLeft =
1046 curNode.getChildLeft()->getLookupProgeny();
1047 std::vector<MarkerLabelT> lookupChildRight =
1048 curNode.getChildRight()->getLookupProgeny();
1050 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
1051 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
1052 lookupMapChildLeft[j] = lookupChildLeft.at(j);
1055 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
1056 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
1057 lookupMapChildRight[j] = lookupChildRight.at(j);
1063 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
1064 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
1066 if (typeOfTransform ==
"erode") {
1067 erode(imTmpLeft, imTmpLeft, se);
1068 erode(imTmpRight, imTmpRight, se);
1069 }
else if (typeOfTransform ==
"dilate") {
1070 dilate(imTmpLeft, imTmpLeft, se);
1071 dilate(imTmpRight, imTmpRight, se);
1072 }
else if (typeOfTransform ==
"open") {
1073 open(imTmpLeft, imTmpLeft, se);
1074 open(imTmpRight, imTmpRight, se);
1075 }
else if (typeOfTransform ==
"close") {
1076 close(imTmpLeft, imTmpLeft, se);
1077 close(imTmpRight, imTmpRight, se);
1079 cout <<
"Please choose typeOfTransform in the following: erode, "
1080 "dilate, open, close"
1083 double childLeftSurf =
area(imTmpLeft);
1084 double childRightSurf =
area(imTmpRight);
1087 1 -
pow(1 - (childLeftSurf / totalSurface), nParam) -
1088 pow(1 - (childRightSurf / totalSurface), nParam) +
1089 pow(1 - ((childLeftSurf + childRightSurf) / totalSurface),
1092 newVal = 0.000000001;
1094 curNode.setInternalNodeValuationFinal(newVal);
1097 this->putValuationsFinalInInitial();
1099 this->setValuationsToZero();
1100 }
else if (typeOfHierarchy ==
"stochasticExtinctionSurfacicImageReturn") {
1101 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1104 if (curNode.getIsInternalNode() == 1) {
1105 if (curNode.getValuation() == 0) {
1106 double childLeftValuation =
1107 curNode.getChildLeft()->getValuation();
1108 double childRightValuation =
1109 curNode.getChildRight()->getValuation();
1110 curNode.setValuation(childLeftValuation + childRightValuation);
1112 curNode.setInternalNodeValuationFinal(
1120 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1123 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1126 if (curNode.getIsInternalNode() == 1) {
1127 std::vector<MarkerLabelT> lookupChildLeft =
1128 curNode.getChildLeft()->getLookupProgeny();
1129 std::vector<MarkerLabelT> lookupChildRight =
1130 curNode.getChildRight()->getLookupProgeny();
1132 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
1133 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
1134 lookupMapChildLeft[j] = lookupChildLeft.at(j);
1137 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
1138 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
1139 lookupMapChildRight[j] = lookupChildRight.at(j);
1145 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
1146 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
1148 if (typeOfTransform ==
"erode") {
1149 erode(imTmpLeft, imTmpLeft, se);
1150 erode(imTmpRight, imTmpRight, se);
1151 }
else if (typeOfTransform ==
"dilate") {
1152 dilate(imTmpLeft, imTmpLeft, se);
1153 dilate(imTmpRight, imTmpRight, se);
1154 }
else if (typeOfTransform ==
"open") {
1155 open(imTmpLeft, imTmpLeft, se);
1156 open(imTmpRight, imTmpRight, se);
1157 }
else if (typeOfTransform ==
"close") {
1158 close(imTmpLeft, imTmpLeft, se);
1159 close(imTmpRight, imTmpRight, se);
1161 cout <<
"Please choose typeOfTransform in the following: erode, "
1162 "dilate, open, close"
1165 double childLeftSurf =
area(imTmpLeft);
1166 double childRightSurf =
area(imTmpRight);
1170 pow(1 - std::min(childLeftSurf, childRightSurf) / totalSurface,
1174 newVal = 0.000000001;
1176 curNode.setInternalNodeValuationFinal(newVal);
1179 this->putValuationsFinalInInitial();
1181 this->setValuationsToZero();
1182 }
else if (typeOfHierarchy ==
"stochasticSurfacicImageReturnNonPoint") {
1184 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1187 if (curNode.getIsInternalNode() == 1) {
1188 if (curNode.getValuation() == 0) {
1189 double childLeftValuation =
1190 curNode.getChildLeft()->getValuation();
1191 double childRightValuation =
1192 curNode.getChildRight()->getValuation();
1193 curNode.setValuation(childLeftValuation + childRightValuation);
1200 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1208 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1211 if (curNode.getIsInternalNode() == 1) {
1212 std::vector<MarkerLabelT> lookupChildLeft =
1213 curNode.getChildLeft()->getLookupProgeny();
1214 std::vector<MarkerLabelT> lookupChildRight =
1215 curNode.getChildRight()->getLookupProgeny();
1217 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
1218 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
1219 lookupMapChildLeft[j] = lookupChildLeft.at(j);
1222 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
1223 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
1224 lookupMapChildRight[j] = lookupChildRight.at(j);
1227 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
1228 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
1229 dilate(imTmpLeft, imTmpLeft, se);
1230 dilate(imTmpRight, imTmpRight, se);
1232 mul(imTmpLeft, imTmpRight, imTmpInter);
1233 double LRSurf =
area(imTmpInter);
1235 MarkerLabelT one = 1;
1236 MarkerLabelT zero = 0;
1238 sub(imTmpLeft, imTmpRight, imTmpInter);
1240 compare(imTmpInter,
"==", one, one, zero, imTmpInterbis);
1241 double L_RSurf =
area(imTmpInterbis);
1243 sub(imTmpRight, imTmpLeft, imTmpInter);
1244 compare(imTmpInter,
"==", one, one, zero, imTmpInterbis);
1245 double R_LSurf =
area(imTmpInterbis);
1248 pow(1 - (LRSurf / totalSurface), nParam) *
1249 (1 -
pow(1 - (L_RSurf / (totalSurface - LRSurf)), nParam) -
1250 pow(1 - (R_LSurf / (totalSurface - LRSurf)), nParam) +
1251 pow(1 - (L_RSurf + R_LSurf) / (totalSurface - LRSurf),
1255 newVal = 0.000000001;
1257 curNode.setInternalNodeValuationFinal(newVal);
1260 this->putValuationsFinalInInitial();
1262 this->setValuationsToZero();
1263 }
else if (typeOfHierarchy ==
"volumic") {
1265 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1268 if (curNode.getIsInternalNode() == 1) {
1269 if (curNode.getValuation() == 0) {
1270 double childLeftValuation =
1271 curNode.getChildLeft()->getValuation();
1272 double childRightValuation =
1273 curNode.getChildRight()->getValuation();
1274 curNode.setValuation(childLeftValuation + childRightValuation);
1279 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1282 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1285 double surface = curNode.getValuation();
1286 curNode.setValuation(height * surface);
1291 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1294 if (curNode.getIsInternalNode() == 1) {
1295 double childLeftValuation = curNode.getChildLeft()->getValuation();
1296 double childRightValuation =
1297 curNode.getChildRight()->getValuation();
1299 curNode.setInternalNodeValuationFinal(
1300 height * fmin(childLeftValuation, childRightValuation));
1303 this->putValuationsFinalInInitial();
1305 this->setValuationsToZero();
1306 }
else if (typeOfHierarchy ==
"volumicImageReturn") {
1308 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1311 if (curNode.getIsInternalNode() == 1) {
1312 std::vector<MarkerLabelT> lookupChildLeft =
1313 curNode.getChildLeft()->getLookupProgeny();
1314 std::vector<MarkerLabelT> lookupChildRight =
1315 curNode.getChildRight()->getLookupProgeny();
1317 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
1318 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
1319 lookupMapChildLeft[j] = lookupChildLeft.at(j);
1322 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
1323 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
1324 lookupMapChildRight[j] = lookupChildRight.at(j);
1330 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
1331 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
1333 if (typeOfTransform ==
"erode") {
1334 erode(imTmpLeft, imTmpLeft, se);
1335 erode(imTmpRight, imTmpRight, se);
1336 }
else if (typeOfTransform ==
"dilate") {
1337 dilate(imTmpLeft, imTmpLeft, se);
1338 dilate(imTmpRight, imTmpRight, se);
1339 }
else if (typeOfTransform ==
"open") {
1340 open(imTmpLeft, imTmpLeft, se);
1341 open(imTmpRight, imTmpRight, se);
1342 }
else if (typeOfTransform ==
"close") {
1343 close(imTmpLeft, imTmpLeft, se);
1344 close(imTmpRight, imTmpRight, se);
1346 cout <<
"Please choose typeOfTransform in the following: erode, "
1347 "dilate, open, close"
1350 double childLeftSurf =
area(imTmpLeft);
1351 double childRightSurf =
area(imTmpRight);
1353 curNode.setValuation(childLeftSurf + childRightSurf);
1357 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1360 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1363 double surface = curNode.getValuation();
1364 curNode.setValuation(height * surface);
1369 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1372 if (curNode.getIsInternalNode() == 1) {
1373 double childLeftValuation = curNode.getChildLeft()->getValuation();
1374 double childRightValuation =
1375 curNode.getChildRight()->getValuation();
1378 height * fmin(childLeftValuation, childRightValuation);
1381 newVal = 0.000000001;
1383 curNode.setInternalNodeValuationFinal(newVal);
1386 this->putValuationsFinalInInitial();
1388 this->setValuationsToZero();
1389 }
else if (typeOfHierarchy ==
"stochasticVolumic") {
1390 double totalSurface = 0;
1391 double totalDepth = 0;
1394 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1397 if (curNode.getIsInternalNode() == 1) {
1398 if (curNode.getValuation() == 0) {
1399 double childLeftValuation =
1400 curNode.getChildLeft()->getValuation();
1401 double childRightValuation =
1402 curNode.getChildRight()->getValuation();
1403 curNode.setValuation(childLeftValuation + childRightValuation);
1410 totalSurface = curNodeTmp.getAncestor()->getValuation();
1414 double totalVolume = totalSurface * totalDepth;
1416 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1419 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1422 double surface = curNode.getValuation();
1423 curNode.setValuation(height * surface);
1429 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1432 if (curNode.getIsInternalNode() == 1) {
1433 double childLeftVol = curNode.getChildLeft()->getValuation();
1434 double childRightVol = curNode.getChildRight()->getValuation();
1437 1 -
pow(1 - (childLeftVol / totalVolume), nParam) -
1438 pow(1 - (childRightVol / totalVolume), nParam) +
1439 pow(1 - (childLeftVol + childRightVol) / totalVolume, nParam);
1440 curNode.setInternalNodeValuationFinal(newVal);
1443 this->putValuationsFinalInInitial();
1445 this->setValuationsToZero();
1446 }
else if (typeOfHierarchy ==
"stochasticExtinctionVolumic") {
1447 double totalSurface = 0;
1448 double totalDepth = 0;
1451 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1454 if (curNode.getIsInternalNode() == 1) {
1455 if (curNode.getValuation() == 0) {
1456 double childLeftValuation =
1457 curNode.getChildLeft()->getValuation();
1458 double childRightValuation =
1459 curNode.getChildRight()->getValuation();
1460 curNode.setValuation(childLeftValuation + childRightValuation);
1467 totalSurface = curNodeTmp.getAncestor()->getValuation();
1472 double totalVolume = totalSurface * totalDepth;
1474 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1477 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1481 double surface = curNode.getValuation();
1482 curNode.setValuation(height * surface);
1486 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1489 if (curNode.getIsInternalNode() == 1) {
1490 double childLeftVol = curNode.getChildLeft()->getValuation();
1491 double childRightVol = curNode.getChildRight()->getValuation();
1494 1 -
pow(1 - std::min(childLeftVol, childRightVol) / totalVolume,
1498 newVal = 0.000000001;
1500 curNode.setInternalNodeValuationFinal(newVal);
1503 this->putValuationsFinalInInitial();
1505 this->setValuationsToZero();
1506 }
else if (typeOfHierarchy ==
"stochasticVolumicImageReturn") {
1507 double totalSurface = 0;
1508 double totalDepth = 0;
1511 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1514 if (curNode.getIsInternalNode() == 1) {
1515 std::vector<MarkerLabelT> lookupChildLeft =
1516 curNode.getChildLeft()->getLookupProgeny();
1517 std::vector<MarkerLabelT> lookupChildRight =
1518 curNode.getChildRight()->getLookupProgeny();
1520 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildLeft;
1521 for (MarkerLabelT j = 0; j < lookupChildLeft.size(); j++) {
1522 lookupMapChildLeft[j] = lookupChildLeft.at(j);
1525 std::map<MarkerLabelT, MarkerLabelT> lookupMapChildRight;
1526 for (MarkerLabelT j = 0; j < lookupChildRight.size(); j++) {
1527 lookupMapChildRight[j] = lookupChildRight.at(j);
1533 applyLookup(imMosa, lookupMapChildLeft, imTmpLeft);
1534 applyLookup(imMosa, lookupMapChildRight, imTmpRight);
1536 if (typeOfTransform ==
"erode") {
1537 erode(imTmpLeft, imTmpLeft, se);
1538 erode(imTmpRight, imTmpRight, se);
1539 }
else if (typeOfTransform ==
"dilate") {
1540 dilate(imTmpLeft, imTmpLeft, se);
1541 dilate(imTmpRight, imTmpRight, se);
1542 }
else if (typeOfTransform ==
"open") {
1543 open(imTmpLeft, imTmpLeft, se);
1544 open(imTmpRight, imTmpRight, se);
1545 }
else if (typeOfTransform ==
"close") {
1546 close(imTmpLeft, imTmpLeft, se);
1547 close(imTmpRight, imTmpRight, se);
1549 cout <<
"Please choose typeOfTransform in the following: erode, "
1550 "dilate, open, close"
1553 double childLeftSurf =
area(imTmpLeft);
1554 double childRightSurf =
area(imTmpRight);
1555 curNode.setValuation(childLeftSurf + childRightSurf);
1561 totalSurface = curNodeTmp.getAncestor()->getValuation();
1565 double totalVolume = totalSurface * totalDepth;
1567 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1570 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1573 double surface = curNode.getValuation();
1574 curNode.setValuation(height * surface);
1578 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1581 if (curNode.getIsInternalNode() == 1) {
1582 double childLeftVol = curNode.getChildLeft()->getValuation();
1583 double childRightVol = curNode.getChildRight()->getValuation();
1586 1 -
pow(1 - (childLeftVol / totalVolume), nParam) -
1587 pow(1 - (childRightVol / totalVolume), nParam) +
1588 pow(1 - (childLeftVol + childRightVol) / totalVolume, nParam);
1590 newVal = 0.000000001;
1592 curNode.setInternalNodeValuationFinal(newVal);
1595 this->putValuationsFinalInInitial();
1597 this->setValuationsToZero();
1598 }
else if (typeOfHierarchy ==
"marker") {
1600 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1603 if (curNode.getIsInternalNode() == 0) {
1604 if (curNode.getMarker() == 0) {
1605 curNode.setMarker(curNode.getValuation());
1610 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1613 if (curNode.getIsInternalNode() == 1) {
1614 double markerLeft = curNode.getChildLeft()->getMarker();
1615 double markerRight = curNode.getChildRight()->getMarker();
1616 curNode.setMarker(fmax(markerLeft, markerRight));
1621 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1623 if (curNode.getIsInternalNode() == 1) {
1624 double markerLeft = curNode.getChildLeft()->getMarker();
1625 double markerRight = curNode.getChildRight()->getMarker();
1626 curNode.setInternalNodeValuationFinal(
1627 fmin(markerLeft, markerRight));
1630 this->putValuationsFinalInInitial();
1632 this->setValuationsToZero();
1633 }
else if (typeOfHierarchy ==
"stochasticSurfacicCountMarkers") {
1635 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1637 curNode.setValuation(curNode.getNbMarkersUnder());
1642 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1646 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1649 if (curNode.getIsInternalNode() == 1) {
1650 double childLeftSurf = curNode.getChildLeft()->getValuation();
1651 double childRightSurf = curNode.getChildRight()->getValuation();
1653 1 -
pow(1 - (childLeftSurf / totalSurface), nParam) -
1654 pow(1 - (childRightSurf / totalSurface), nParam) +
1655 pow(1 - ((childLeftSurf + childRightSurf) / totalSurface),
1657 curNode.setInternalNodeValuationFinal(newVal);
1660 this->putValuationsFinalInInitial();
1662 this->setValuationsToZero();
1663 }
else if (typeOfHierarchy ==
"stochasticSurfacicSumMarkers") {
1665 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1667 curNode.setValuation(curNode.getNbMarkersUnder());
1672 double totalSurface = curNodeTmp.getAncestor()->getValuation();
1675 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1678 if (curNode.getIsInternalNode() == 1) {
1679 double childLeftSurf = curNode.getChildLeft()->getValuation();
1680 double childRightSurf = curNode.getChildRight()->getValuation();
1682 1 -
pow(1 - (childLeftSurf / totalSurface), nParam) -
1683 pow(1 - (childRightSurf / totalSurface), nParam) +
1684 pow(1 - ((childLeftSurf + childRightSurf) / totalSurface),
1686 curNode.setInternalNodeValuationFinal(newVal);
1689 this->putValuationsFinalInInitial();
1691 this->setValuationsToZero();
1692 }
else if (typeOfHierarchy ==
"stochasticVolumicCountMarkers") {
1694 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1696 curNode.setValuation(curNode.getNbMarkersUnder());
1698 double totalSurface = 0;
1701 totalSurface = curNodeTmp.getAncestor()->getValuation();
1703 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1706 if (curNode.getLabel() != curNode.getFather()->getLabel()) {
1711 double surface = curNode.getValuation();
1713 curNode.setValuation(height * surface);
1719 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1722 if (curNode.getIsInternalNode() == 1) {
1723 double childLeftSurf = curNode.getChildLeft()->getValuation();
1724 double childRightSurf = curNode.getChildRight()->getValuation();
1726 1 -
pow(1 - (childLeftSurf / totalSurface), nParam) -
1727 pow(1 - (childRightSurf / totalSurface), nParam) +
1728 pow(1 - ((childLeftSurf + childRightSurf) / totalSurface),
1730 curNode.setInternalNodeValuationFinal(newVal);
1733 this->putValuationsFinalInInitial();
1735 this->setValuationsToZero();
1736 }
else if (typeOfHierarchy ==
"waterfall") {
1738 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1741 if (curNode.getIsInternalNode() == 1) {
1742 if (curNode.getValuation() == 0) {
1743 double childLeftValuation =
1744 curNode.getChildLeft()->getValuation();
1745 double childRightValuation =
1746 curNode.getChildRight()->getValuation();
1747 double childLeftDiameter =
1748 curNode.getChildLeft()->getInternalNodeValuationFinal();
1749 double childRightDiameter =
1750 curNode.getChildRight()->getInternalNodeValuationFinal();
1752 double newVal = 1 + min(childLeftValuation, childRightValuation);
1753 curNode.setValuation(newVal);
1754 curNode.setInternalNodeValuationFinal(
1755 max(newVal, max(childLeftDiameter, childRightDiameter)));
1759 this->putValuationsFinalInInitial();
1761 this->setValuationsToZero();
1762 }
else if (typeOfHierarchy ==
"persistence") {
1763 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1766 if (curNode.getIsInternalNode() == 1) {
1767 double childLeftINValuation =
1769 double childRightINValuation =
1773 double newVal = INValuation - std::max(childLeftINValuation,
1774 childRightINValuation);
1775 curNode.setInternalNodeValuationFinal(newVal);
1778 this->putValuationsFinalInInitial();
1780 this->setValuationsToZero();
1784 "Dendrogram::HierarchicalConstruction(Dendrogram& "
1785 "dendrogram,std::string typeOfHierarchy) \n"
1786 <<
"Please choose one of the following hierarchies: surfacic, "
1787 "volumic, stochasticSurfacic, stochasticVolumic, "
1788 <<
"surfacicImageReturn, volumicImageReturn, "
1789 "stochasticSurfacicImageReturn, stochasticVolumicImageReturn "
1790 <<
",stochasticSurfacicCountMarkers,stochasticVolumicCountMarkers, "
1791 <<
"stochasticExtinctionSurfacic, stochasticExtinctionVolumic, "
1792 <<
"stochasticExtinctionSurfacicImageReturn, "
1793 <<
"waterfall,marker,persistence" << endl;
1804 std::vector<DendroNodeType *> &dendroNodes = this->getDendroNodes();
1806 if (typeOfHierarchy ==
"none") {
1807 }
else if (typeOfHierarchy ==
"simplifiedMumfordShah") {
1808 for (
size_t i = 0; i < dendroNodes.size();
1811 if (curNode.getIsInternalNode() == 1) {
1812 std::vector<double> childLeftMoments =
1813 curNode.getChildLeft()->getMoments();
1814 std::vector<double> childRightMoments =
1815 curNode.getChildRight()->getMoments();
1816 std::vector<double> moments = curNode.getMoments();
1818 double contoursValue = curNode.getContoursSize();
1819 double contoursValueLeft =
1820 curNode.getChildLeft()->getContoursSize();
1821 double contoursValueRight =
1822 curNode.getChildRight()->getContoursSize();
1824 double fidelityTerm =
1825 moments.at(2) - (moments.at(1) * moments.at(1)) / moments.at(0);
1826 double fidelityTermLeft =
1827 childLeftMoments.at(2) -
1828 (childLeftMoments.at(1) * childLeftMoments.at(1)) /
1829 childLeftMoments.at(0);
1830 double fidelityTermRight =
1831 childRightMoments.at(2) -
1832 (childRightMoments.at(1) * childRightMoments.at(1)) /
1833 childRightMoments.at(0);
1836 (fidelityTerm - fidelityTermLeft - fidelityTermRight) /
1837 (contoursValueLeft + contoursValueRight - contoursValue);
1839 curNode.setInternalNodeValuationFinal(newLamb);
1840 curNode.setValuation(newLamb);
1843 this->putValuationsFinalInInitial();
1851 this->sortNodes(
true);
1853 double contoursValue = 0.0;
1855 std::vector<double> momentsHighestNode = highestNode.getMoments();
1856 double fidelityValue =
1857 momentsHighestNode.at(2) -
1858 (momentsHighestNode.at(1) * momentsHighestNode.at(1)) /
1859 momentsHighestNode.at(0);
1862 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1865 if (curNode.getIsInternalNode() == 1) {
1866 std::vector<double> moments = curNode.getMoments();
1867 std::vector<double> momentsLeft =
1868 curNode.getChildLeft()->getMoments();
1869 std::vector<double> momentsRight =
1870 curNode.getChildRight()->getMoments();
1872 double fidelityTerm =
1873 moments.at(2) - (moments.at(1) * moments.at(1)) / moments.at(0);
1874 double fidelityTermLeft =
1876 (momentsLeft.at(1) * momentsLeft.at(1)) / momentsLeft.at(0);
1877 double fidelityTermRight =
1878 momentsRight.at(2) -
1879 (momentsRight.at(1) * momentsRight.at(1)) / momentsRight.at(0);
1883 contoursValue = contoursValue + curNode.getContoursSize();
1884 fidelityValue = fidelityValue - fidelityTerm + fidelityTermLeft +
1887 double newEnergy = fidelityValue + Lamb * contoursValue;
1888 curNode.setEnergy(newEnergy);
1892 this->setValuationsToZero();
1895 else if (typeOfHierarchy ==
"varianceEnergy") {
1896 for (
size_t i = 0; i < dendroNodes.size();
1899 if (curNode.getIsInternalNode() == 1) {
1900 std::vector<double> childLeftMoments =
1901 curNode.getChildLeft()->getMoments();
1902 std::vector<double> childRightMoments =
1903 curNode.getChildRight()->getMoments();
1904 std::vector<double> moments = curNode.getMoments();
1906 double fidelityTerm =
1907 moments.at(2) - (moments.at(1) * moments.at(1)) / moments.at(0);
1908 double fidelityTermLeft =
1909 childLeftMoments.at(2) -
1910 (childLeftMoments.at(1) * childLeftMoments.at(1)) /
1911 childLeftMoments.at(0);
1912 double fidelityTermRight =
1913 childRightMoments.at(2) -
1914 (childRightMoments.at(1) * childRightMoments.at(1)) /
1915 childRightMoments.at(0);
1918 (fidelityTerm - fidelityTermLeft - fidelityTermRight);
1920 curNode.setInternalNodeValuationFinal(newLamb);
1921 curNode.setValuation(newLamb);
1924 this->putValuationsFinalInInitial();
1932 this->sortNodes(
true);
1934 double contoursValue = 0.0;
1936 std::vector<double> momentsHighestNode = highestNode.getMoments();
1937 double fidelityValue =
1938 momentsHighestNode.at(2) -
1939 (momentsHighestNode.at(1) * momentsHighestNode.at(1)) /
1940 momentsHighestNode.at(0);
1943 for (
size_t i = 0; i < dendroNodes.size(); i++) {
1946 if (curNode.getIsInternalNode() == 1) {
1947 std::vector<double> moments = curNode.getMoments();
1948 std::vector<double> momentsLeft =
1949 curNode.getChildLeft()->getMoments();
1950 std::vector<double> momentsRight =
1951 curNode.getChildRight()->getMoments();
1953 double fidelityTerm =
1954 moments.at(2) - (moments.at(1) * moments.at(1)) / moments.at(0);
1955 double fidelityTermLeft =
1957 (momentsLeft.at(1) * momentsLeft.at(1)) / momentsLeft.at(0);
1958 double fidelityTermRight =
1959 momentsRight.at(2) -
1960 (momentsRight.at(1) * momentsRight.at(1)) / momentsRight.at(0);
1964 contoursValue = contoursValue + curNode.getContoursSize();
1965 fidelityValue = fidelityValue - fidelityTerm + fidelityTermLeft +
1968 double newEnergy = fidelityValue + Lamb * contoursValue;
1969 curNode.setEnergy(newEnergy);
1973 this->setValuationsToZero();
1977 cout <<
"void Dendrogram::EnergyConstruction(Dendrogram& "
1978 "dendrogram,std::string typeOfHierarchy) \n"
1979 <<
"Please choose one of the following hierarchies: "
1980 "simplifiedMumfordShah, varianceEnergy "
2003 void setNbrNodes(
size_t nNbrNodes)
2005 nbrNodes = nNbrNodes;
2007 size_t getNbrNodes()
2011 void setNbrMarkers(
size_t nNbrMarkers)
2013 nbrMarkers = nNbrMarkers;
2015 size_t getNbrMarkers()
2030 if (nameOfValueWanted ==
"valuation") {
2031 return curNode.getValuation();
2032 }
else if (nameOfValueWanted ==
"internalNodeValuationInitial") {
2034 }
else if (nameOfValueWanted ==
"internalNodeValuationFinal") {
2035 return curNode.getInternalNodeValuationFinal();
2036 }
else if (nameOfValueWanted ==
"label") {
2037 return curNode.getLabel();
2038 }
else if (nameOfValueWanted ==
"labelChildRight") {
2039 return curNode.getChildRight()->getLabel();
2040 }
else if (nameOfValueWanted ==
"labelChildLeft") {
2041 return curNode.getChildLeft()->getLabel();
2042 }
else if (nameOfValueWanted ==
"labelNeighborRight") {
2043 return curNode.getNeighborRight()->getLabel();
2044 }
else if (nameOfValueWanted ==
"labelNeighborLeft") {
2045 return curNode.getNeighborLeft()->getLabel();
2046 }
else if (nameOfValueWanted ==
"labelFather") {
2047 return curNode.getFather()->getLabel();
2048 }
else if (nameOfValueWanted ==
"isInternalNode") {
2049 return curNode.getIsInternalNode();
2050 }
else if (nameOfValueWanted ==
"marker") {
2051 return curNode.getMarker();
2052 }
else if (nameOfValueWanted ==
"energy") {
2053 return curNode.getEnergy();
2054 }
else if (nameOfValueWanted ==
"contours") {
2055 return curNode.getContoursSize();
2058 <<
"int Dendrogram::getNodeValue(int nodeIndex,string "
2059 "nameOfValueWanted) -> nameOfValueWanted must be chosen in this "
2060 "list: valuation, internalNodeValuationInitial "
2061 <<
", internalNodeValuationInitial, label, marker." << endl;
2074 void setNodeValue(
size_t nodeIndex,
string nameOfValueWanted,
double value)
2077 if (nameOfValueWanted ==
"valuation") {
2078 curNode.setValuation(value);
2079 }
else if (nameOfValueWanted ==
"internalNodeValuationInitial") {
2080 curNode.setInternalNodeValuationInitial(value);
2081 }
else if (nameOfValueWanted ==
"internalNodeValuationFinal") {
2082 curNode.setInternalNodeValuationFinal(value);
2083 }
else if (nameOfValueWanted ==
"label") {
2084 curNode.setLabel(value);
2085 }
else if (nameOfValueWanted ==
"labelChildRight") {
2086 curNode.getChildRight()->setLabel(value);
2087 }
else if (nameOfValueWanted ==
"labelChildLeft") {
2088 curNode.getChildLeft()->setLabel(value);
2089 }
else if (nameOfValueWanted ==
"labelNeighborRight") {
2090 curNode.getNeighborRight()->setLabel(value);
2091 }
else if (nameOfValueWanted ==
"labelNeighborLeft") {
2092 curNode.getNeighborLeft()->setLabel(value);
2093 }
else if (nameOfValueWanted ==
"labelFather") {
2094 curNode.getFather()->setLabel(value);
2095 }
else if (nameOfValueWanted ==
"isInternalNode") {
2096 curNode.setIsInternalNode(value);
2097 }
else if (nameOfValueWanted ==
"marker") {
2098 curNode.setMarker(value);
2100 cout <<
"int Dendrogram::setNodeValue(int nodeIndex,string "
2101 "nameOfValueWanted, double value) -> nameOfValueWanted must be "
2102 "chosen in this list: valuation, internalNodeValuationInitial "
2103 <<
", internalNodeValuationInitial, label, marker." << endl;
2115 string nameOfValueWanted)
2118 std::vector<MarkerLabelT> lookupToReturn;
2119 if (nameOfValueWanted ==
"current") {
2120 lookupToReturn = curNode.getLookupProgeny();
2121 }
else if (nameOfValueWanted ==
"childLeft") {
2122 lookupToReturn = curNode.getChildLeft()->getLookupProgeny();
2123 }
else if (nameOfValueWanted ==
"childRight") {
2124 lookupToReturn = curNode.getChildRight()->getLookupProgeny();
2126 cout <<
"std::vector<MarkerLabelT> getLookupProgeny(size_t "
2127 "nodeIndex,string nameOfValueWanted) -> nameOfValueWanted must "
2128 "be chosen in this list: current, "
2129 <<
", childLeft, childRight." << endl;
2131 return lookupToReturn;
2142 string nameOfValueWanted)
2144 std::vector<MarkerLabelT> lookupToReturn;
2148 if (nameOfValueWanted ==
"current") {
2149 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2152 lookupToReturn = curNode.getLookupProgeny();
2158 else if (nameOfValueWanted ==
"childLeft") {
2159 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2162 lookupToReturn = curNode.getChildLeft()->getLookupProgeny();
2168 else if (nameOfValueWanted ==
"childRight") {
2169 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2172 lookupToReturn = curNode.getChildRight()->getLookupProgeny();
2177 cout <<
"std::vector<MarkerLabelT> "
2178 "getThreshLookupProgeny(thresh,nameOfValueWanted) -> "
2179 "nameOfValueWanted must be chosen in this list: current, "
2180 <<
", childLeft, childRight." << endl;
2183 cout <<
"getThreshLookupProgeny(thresh,nameOfValueWanted) "
2184 <<
"-> the thresh specified must be higher than 0" << endl;
2187 return lookupToReturn;
2199 std::vector<std::vector<double>>
2201 string momentOfLife =
"death")
2203 size_t nbrRegions = nbrNodes / 2 + 1;
2205 std::vector<std::vector<double>> persistences(
2206 nbrRegions, vector<double>(nbrRegions, -1.0));
2208 if (typeOfMatrix ==
"sparse" && momentOfLife ==
"death") {
2212 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2215 if (curNode.getIsInternalNode() == 0) {
2216 MarkerLabelT label1 = curNode.getLabel();
2217 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2218 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2219 MarkerLabelT label2 = t;
2220 if (contoursCount.at(t) != 0) {
2221 if (persistences[label1][label2] == -1) {
2222 persistences[label1][label2] = -0.5;
2223 persistences[label2][label1] = -0.5;
2230 this->sortNodes(
true);
2233 MarkerLabelT count = 0;
2234 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2236 if (curNode.getIsInternalNode() == 1) {
2237 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2238 curNode.getChildLeft()->getLookupProgeny();
2239 std::vector<MarkerLabelT> lookupProgenyChildRight =
2240 curNode.getChildRight()->getLookupProgeny();
2241 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2242 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2244 if (lookupProgenyChildLeft.at(p) == 1 &&
2245 lookupProgenyChildRight.at(q) == 1 &&
2246 persistences[p][q] == -0.5) {
2247 persistences[p][q] = nbrRegions - count;
2248 persistences[q][p] = nbrRegions - count;
2256 for (
size_t i = 0; i < persistences.size(); i++) {
2257 for (
size_t j = 0; j < persistences[i].size(); j++)
2258 if (persistences[i][j] <= 0.0) {
2259 persistences[i][j] = 0.0;
2260 persistences[j][i] = 0.0;
2265 else if (typeOfMatrix ==
"full" && momentOfLife ==
"death") {
2266 persistences = std::vector<std::vector<double>>(
2267 nbrRegions, vector<double>(nbrRegions, -0.5));
2269 this->sortNodes(
true);
2272 MarkerLabelT count = 0;
2273 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2275 if (curNode.getIsInternalNode() == 1) {
2276 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2277 curNode.getChildLeft()->getLookupProgeny();
2278 std::vector<MarkerLabelT> lookupProgenyChildRight =
2279 curNode.getChildRight()->getLookupProgeny();
2280 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2281 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2283 if (lookupProgenyChildLeft.at(p) == 1 &&
2284 lookupProgenyChildRight.at(q) == 1 &&
2285 persistences[p][q] == -0.5) {
2286 persistences[p][q] = nbrRegions - count;
2287 persistences[q][p] = nbrRegions - count;
2295 for (
size_t i = 0; i < persistences.size(); i++) {
2296 for (
size_t j = 0; j < persistences[i].size(); j++)
2297 if (persistences[i][j] <= 0.0) {
2298 persistences[i][j] = 0.0;
2299 persistences[j][i] = 0.0;
2303 else if (typeOfMatrix ==
"sparse" && momentOfLife ==
"life") {
2307 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2309 if (curNode.getIsInternalNode() == 0) {
2310 MarkerLabelT label1 = curNode.getLabel();
2311 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2312 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2313 MarkerLabelT label2 = t;
2314 if (contoursCount.at(t) != 0) {
2315 if (persistences[label1][label2] == -1) {
2316 persistences[label1][label2] = 1;
2317 persistences[label2][label1] = 1;
2324 for (
size_t i = 0; i < persistences.size(); i++) {
2325 for (
size_t j = 0; j < persistences[i].size(); j++)
2326 if (persistences[i][j] <= 0.0) {
2327 persistences[i][j] = 0.0;
2328 persistences[j][i] = 0.0;
2332 else if (typeOfMatrix ==
"full" && momentOfLife ==
"life") {
2338 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2340 if (curNode.getIsInternalNode() == 0) {
2341 MarkerLabelT label1 = curNode.getLabel();
2342 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2343 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2344 MarkerLabelT label2 = t;
2345 if (contoursCount.at(t) != 0) {
2346 if (persistences[label1][label2] == -1) {
2347 persistences[label1][label2] = -0.5;
2348 persistences[label2][label1] = -0.5;
2355 this->sortNodes(
true);
2358 MarkerLabelT count = 0;
2359 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2361 if (curNode.getIsInternalNode() == 1) {
2362 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2363 curNode.getChildLeft()->getLookupProgeny();
2364 std::vector<MarkerLabelT> lookupProgenyChildRight =
2365 curNode.getChildRight()->getLookupProgeny();
2366 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2367 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2369 if (lookupProgenyChildLeft.at(p) == 1 &&
2370 lookupProgenyChildRight.at(q) == 1 &&
2371 persistences[p][q] == -0.5) {
2372 persistences[p][q] = nbrRegions - count;
2373 persistences[q][p] = nbrRegions - count;
2381 for (
size_t i = 0; i < persistences.size(); i++) {
2382 for (
size_t j = 0; j < persistences[i].size(); j++)
2383 if (persistences[i][j] <= 0.0) {
2384 persistences[i][j] = 0.0;
2385 persistences[j][i] = 0.0;
2392 std::vector<std::vector<double>> persistencesInit(persistences);
2393 std::vector<std::vector<double>> persistencesTmp(
2394 nbrRegions, vector<double>(nbrRegions, 0.0));
2398 while (persistences != persistencesTmp) {
2399 persistencesTmp = persistences;
2400 for (
size_t i = 0; i < nbrRegions; i++) {
2401 for (
size_t j = i; j < nbrRegions; j++) {
2402 std::vector<double> minVec(nbrRegions, 0.0);
2403 for (
size_t k = 0; k < nbrRegions; k++) {
2404 minVec[k] = min(persistences[i][k], persistencesInit[k][j]);
2406 persistences[i][j] = *max_element(minVec.begin(), minVec.end());
2407 persistences[j][i] = persistences[i][j];
2413 cout <<
"getPersistences(string typeOfMatrix = ''sparse'',string "
2414 "momentOfLife = ''death'') -> please choose "
2415 <<
"typeOfMatrix in the following : ''sparse'', ''full''; and "
2416 "momentOfLife in the following : ''death'',''life'' "
2420 return persistences;
2431 std::vector<std::vector<double>>
2432 getSaliences(
string typeOfMatrix =
"sparse",
string momentOfLife =
"death")
2434 size_t nbrRegions = nbrNodes / 2 + 1;
2436 std::vector<std::vector<double>> saliences(
2437 nbrRegions, vector<double>(nbrRegions, -1.0));
2439 if (typeOfMatrix ==
"sparse" && momentOfLife ==
"death") {
2442 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2444 if (curNode.getIsInternalNode() == 0) {
2445 MarkerLabelT label1 = curNode.getLabel();
2446 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2447 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2448 MarkerLabelT label2 = t;
2449 if (contoursCount.at(t) != 0) {
2450 if (saliences[label1][label2] == -1) {
2451 saliences[label1][label2] = -0.5;
2452 saliences[label2][label1] = -0.5;
2459 this->sortNodes(
true);
2462 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2464 if (curNode.getIsInternalNode() == 1) {
2465 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2466 curNode.getChildLeft()->getLookupProgeny();
2467 std::vector<MarkerLabelT> lookupProgenyChildRight =
2468 curNode.getChildRight()->getLookupProgeny();
2469 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2470 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2472 if (lookupProgenyChildLeft.at(p) == 1 &&
2473 lookupProgenyChildRight.at(q) == 1 &&
2474 saliences[p][q] == -0.5) {
2484 for (
size_t i = 0; i < saliences.size(); i++) {
2485 for (
size_t j = 0; j < saliences[i].size(); j++)
2486 if (saliences[i][j] <= 0.0) {
2487 saliences[i][j] = 0.0;
2488 saliences[j][i] = 0.0;
2492 else if (typeOfMatrix ==
"full" && momentOfLife ==
"death") {
2493 saliences = std::vector<std::vector<double>>(
2494 nbrRegions, vector<double>(nbrRegions, -0.5));
2497 this->sortNodes(
true);
2500 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2502 if (curNode.getIsInternalNode() == 1) {
2503 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2504 curNode.getChildLeft()->getLookupProgeny();
2505 std::vector<MarkerLabelT> lookupProgenyChildRight =
2506 curNode.getChildRight()->getLookupProgeny();
2507 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2508 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2510 if (lookupProgenyChildLeft.at(p) == 1 &&
2511 lookupProgenyChildRight.at(q) == 1 &&
2512 saliences[p][q] == -0.5) {
2522 for (
size_t i = 0; i < saliences.size(); i++) {
2523 for (
size_t j = 0; j < saliences[i].size(); j++)
2524 if (saliences[i][j] <= 0.0) {
2525 saliences[i][j] = 0.0;
2526 saliences[j][i] = 0.0;
2530 else if (typeOfMatrix ==
"sparse" && momentOfLife ==
"life") {
2533 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2535 if (curNode.getIsInternalNode() == 0) {
2536 MarkerLabelT label1 = curNode.getLabel();
2537 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2538 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2539 MarkerLabelT label2 = t;
2540 if (contoursCount.at(t) != 0) {
2541 if (saliences[label1][label2] == -1) {
2542 saliences[label1][label2] = 0.01;
2543 saliences[label2][label1] = 0.01;
2550 for (
size_t i = 0; i < saliences.size(); i++) {
2551 for (
size_t j = 0; j < saliences[i].size(); j++)
2552 if (saliences[i][j] <= 0.0) {
2553 saliences[i][j] = 0.0;
2554 saliences[j][i] = 0.0;
2559 else if (typeOfMatrix ==
"full" && momentOfLife ==
"life") {
2560 saliences = std::vector<std::vector<double>>(
2561 nbrRegions, vector<double>(nbrRegions, -0.5));
2567 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2569 if (curNode.getIsInternalNode() == 0) {
2570 MarkerLabelT label1 = curNode.getLabel();
2571 vector<MarkerLabelT> contoursCount = curNode.getContoursCount();
2572 for (MarkerLabelT t = 0; t < contoursCount.size(); t++) {
2573 MarkerLabelT label2 = t;
2574 if (contoursCount.at(t) != 0) {
2575 if (saliences[label1][label2] == -1) {
2576 saliences[label1][label2] = -0.5;
2577 saliences[label2][label1] = -0.5;
2585 this->sortNodes(
true);
2588 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2590 if (curNode.getIsInternalNode() == 1) {
2591 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2592 curNode.getChildLeft()->getLookupProgeny();
2593 std::vector<MarkerLabelT> lookupProgenyChildRight =
2594 curNode.getChildRight()->getLookupProgeny();
2595 for (MarkerLabelT p = 0; p < lookupProgenyChildLeft.size(); p++) {
2596 for (MarkerLabelT q = 0; q < lookupProgenyChildRight.size();
2598 if (lookupProgenyChildLeft.at(p) == 1 &&
2599 lookupProgenyChildRight.at(q) == 1 &&
2600 saliences[p][q] == -0.5) {
2609 for (
size_t i = 0; i < saliences.size(); i++) {
2610 for (
size_t j = 0; j < saliences[i].size(); j++)
2611 if (saliences[i][j] <= 0.0) {
2612 saliences[i][j] = 0.0;
2613 saliences[j][i] = 0.0;
2619 std::vector<std::vector<double>> saliencesInit(saliences);
2620 std::vector<std::vector<double>> saliencesTmp(
2621 nbrRegions, vector<double>(nbrRegions, 0.0));
2625 while (saliences != saliencesTmp) {
2626 saliencesTmp = saliences;
2627 for (
size_t i = 0; i < nbrRegions; i++) {
2628 for (
size_t j = i; j < nbrRegions; j++) {
2629 std::vector<double> minVec(nbrRegions, 0.0);
2630 for (
size_t k = 0; k < nbrRegions; k++) {
2631 minVec[k] = min(saliences[i][k], saliencesInit[k][j]);
2633 saliences[i][j] = *max_element(minVec.begin(), minVec.end());
2634 saliences[j][i] = saliences[i][j];
2640 cerr <<
"getSaliences(string typeOfMatrix = ''sparse'',momentOfLife = "
2641 "''death'') -> please choose typeOfMatrix"
2642 <<
" in the following : sparse, full; and please choose "
2643 "momentOfLife in the following : ''life'', ''death'' "
2659 double dasguptaCF = 0;
2662 this->sortNodes(
true);
2666 std::vector<Edge<NodeT, ValGraphT>> edges = completeGraph.
getEdges();
2668 std::vector<NodeT> labelsin = std::vector<NodeT>(edges.size(), 0);
2669 std::vector<NodeT> labelsout = std::vector<NodeT>(edges.size(), 0);
2670 std::vector<ValGraphT> weights = std::vector<ValGraphT>(edges.size(), 0);
2672 for (MarkerLabelT i = 0; i < edges.size(); i++) {
2673 labelsin.at(i) = edges.at(i).source;
2674 labelsout.at(i) = edges.at(i).source;
2675 weights.at(i) = edges.at(i).source;
2679 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2682 if (curNode.getIsInternalNode() == 1) {
2683 std::vector<MarkerLabelT> lookupProgeny = curNode.getLookupProgeny();
2684 std::vector<MarkerLabelT> lookupProgenyChildLeft =
2685 curNode.getChildLeft()->getLookupProgeny();
2686 std::vector<MarkerLabelT> lookupProgenyChildRight =
2687 curNode.getChildRight()->getLookupProgeny();
2689 double nbLeavesUnder =
2690 std::accumulate(lookupProgeny.begin(), lookupProgeny.end(), 0.);
2692 for (MarkerLabelT s = 0; s < edges.size(); s++) {
2693 MarkerLabelT labelIn = labelsin.at(s);
2694 MarkerLabelT labelOut = labelsout.at(s);
2695 double currentWeight = weights.at(s);
2697 if (lookupProgeny.at(labelIn) == lookupProgeny.at(labelOut) &&
2698 lookupProgenyChildLeft.at(labelIn) !=
2699 lookupProgenyChildRight.at(labelOut)) {
2700 dasguptaCF += nbLeavesUnder * currentWeight;
2717 double computeScoreInteractiveSeg()
2721 std::map<MarkerLabelT, std::vector<MarkerLabelT>> lookupMapMarkersLabels;
2722 std::map<std::pair<MarkerLabelT, MarkerLabelT>, std::vector<double>>
2723 lookupMapMarkersPairsValuations;
2724 std::map<std::pair<MarkerLabelT, MarkerLabelT>, std::vector<double>>
2725 lookupMapSameMarkersPairsValuations;
2729 for (MarkerLabelT i = 0; i < nbrNodes; i++) {
2730 DendroNodeType &curNode = *dendroNodes.at(i);
2731 if (curNode.getMarker() != 0) {
2732 MarkerLabelT markerValue = curNode.getMarker();
2733 MarkerLabelT labelValue = curNode.getLabel();
2734 lookupMapMarkersLabels[markerValue].push_back(labelValue);
2739 for (
typename std::map<MarkerLabelT, std::vector<MarkerLabelT>>::iterator
2740 iter = lookupMapMarkersLabels.begin();
2741 iter != lookupMapMarkersLabels.end(); ++iter) {
2742 typename std::map<MarkerLabelT, std::vector<MarkerLabelT>>::iterator
2745 for (; iter2 != lookupMapMarkersLabels.end(); ++iter2) {
2746 if (iter->first != iter2->first) {
2747 std::pair<MarkerLabelT, MarkerLabelT> pairMarkers =
2748 make_pair(iter->first, iter2->first);
2749 lookupMapMarkersPairsValuations[pairMarkers] =
2750 std::vector<double>();
2753 std::pair<MarkerLabelT, MarkerLabelT> pairSameMarkers =
2754 make_pair(iter->first, iter->first);
2755 lookupMapSameMarkersPairsValuations[pairSameMarkers] =
2756 std::vector<double>();
2761 for (
typename std::map<std::pair<MarkerLabelT, MarkerLabelT>,
2762 std::vector<double>>::iterator iter =
2763 lookupMapMarkersPairsValuations.begin();
2764 iter != lookupMapMarkersPairsValuations.end(); iter++) {
2765 MarkerLabelT marker1 = iter->first.first;
2766 MarkerLabelT marker2 = iter->first.second;
2767 std::vector<MarkerLabelT> labelsMarker1 =
2768 lookupMapMarkersLabels[marker1];
2769 std::vector<MarkerLabelT> labelsMarker2 =
2770 lookupMapMarkersLabels[marker2];
2772 for (
size_t i = 0; i < labelsMarker1.size(); i++) {
2773 for (
size_t j = 0; j < labelsMarker2.size(); j++) {
2774 double scoreTmp = 0;
2775 MarkerLabelT label1 = labelsMarker1[i];
2776 MarkerLabelT label2 = labelsMarker2[j];
2778 for (
size_t d = 0; d < nbrNodes; d++) {
2779 DendroNodeType & curNode = *dendroNodes.at(d);
2780 std::vector<MarkerLabelT> lookupProgenyd =
2781 curNode.getLookupProgeny();
2782 if (lookupProgenyd[label1] != 0 && lookupProgenyd[label2] != 0) {
2783 if (scoreTmp == 0) {
2784 scoreTmp = curNode.getInternalNodeValuationInitial();
2786 if (curNode.getInternalNodeValuationInitial() < scoreTmp) {
2787 scoreTmp = curNode.getInternalNodeValuationInitial();
2792 if (scoreTmp != 0) {
2793 (iter->second).push_back(scoreTmp);
2801 for (
typename std::map<std::pair<MarkerLabelT, MarkerLabelT>,
2802 std::vector<double>>::iterator iter =
2803 lookupMapSameMarkersPairsValuations.begin();
2804 iter != lookupMapSameMarkersPairsValuations.end(); iter++) {
2805 MarkerLabelT marker = iter->first.first;
2806 std::vector<MarkerLabelT> labelsMarker = lookupMapMarkersLabels[marker];
2807 for (
size_t i = 0; i < labelsMarker.size(); i++) {
2808 for (
size_t j = 0; j < labelsMarker.size(); j++) {
2809 double scoreTmp = 0;
2810 MarkerLabelT label1 = labelsMarker[i];
2811 MarkerLabelT label2 = labelsMarker[j];
2812 for (
size_t d = 0; d < nbrNodes; d++) {
2813 DendroNodeType & curNode = *dendroNodes.at(d);
2814 std::vector<MarkerLabelT> lookupProgenyd =
2815 curNode.getLookupProgeny();
2816 if (lookupProgenyd[label1] != 0 && lookupProgenyd[label2] != 0) {
2817 if (scoreTmp == 0) {
2818 scoreTmp = curNode.getInternalNodeValuationInitial();
2820 if (curNode.getInternalNodeValuationInitial() < scoreTmp) {
2821 scoreTmp = curNode.getInternalNodeValuationInitial();
2826 (iter->second).push_back(scoreTmp);
2845 double meanDiffMarkers = 0;
2846 double varDiffMarkers = 0;
2847 double sizeDiffMarkers = 0;
2848 for (
typename std::map<std::pair<MarkerLabelT, MarkerLabelT>,
2849 std::vector<double>>::iterator iter =
2850 lookupMapMarkersPairsValuations.begin();
2851 iter != lookupMapMarkersPairsValuations.end(); iter++) {
2852 sizeDiffMarkers = sizeDiffMarkers + 1;
2853 double mean_ind = 0;
2855 for (MarkerLabelT i = 0; i < (iter->second).size(); i++) {
2856 mean_ind = mean_ind + (iter->second)[i];
2858 mean_ind = mean_ind / (iter->second).size();
2859 for (MarkerLabelT i = 0; i < (iter->second).size(); i++) {
2860 var_ind = var_ind + (mean_ind - (iter->second)[i]) *
2861 (mean_ind - (iter->second)[i]);
2863 var_ind = var_ind / (iter->second).size();
2864 meanDiffMarkers = meanDiffMarkers + mean_ind;
2865 varDiffMarkers = varDiffMarkers + var_ind;
2867 meanDiffMarkers = meanDiffMarkers / sizeDiffMarkers;
2868 varDiffMarkers = varDiffMarkers / sizeDiffMarkers;
2871 double meanSameMarkers = 0;
2872 double varSameMarkers = 0;
2873 double sizeSameMarkers = 0;
2874 for (
typename std::map<std::pair<MarkerLabelT, MarkerLabelT>,
2875 std::vector<double>>::iterator iter =
2876 lookupMapSameMarkersPairsValuations.begin();
2877 iter != lookupMapSameMarkersPairsValuations.end(); iter++) {
2878 sizeSameMarkers = sizeSameMarkers + 1;
2879 double mean_ind = 0;
2881 for (MarkerLabelT i = 0; i < (iter->second).size(); i++) {
2882 mean_ind = mean_ind + (iter->second)[i];
2884 mean_ind = mean_ind / (iter->second).size();
2885 for (MarkerLabelT i = 0; i < (iter->second).size(); i++) {
2886 varSameMarkers = varSameMarkers + (mean_ind - (iter->second)[i]) *
2887 (mean_ind - (iter->second)[i]);
2889 var_ind = var_ind / (iter->second).size();
2890 meanSameMarkers = meanSameMarkers + mean_ind;
2891 varSameMarkers = varSameMarkers + var_ind;
2893 meanSameMarkers = meanSameMarkers / sizeSameMarkers;
2894 varSameMarkers = varSameMarkers / sizeSameMarkers;
2896 score = (varSameMarkers * varSameMarkers) /
2897 (std::abs(meanSameMarkers - meanDiffMarkers));
3008 template <
class MarkerLabelT,
class NodeT,
class ValGraphT>
3011 Dendrogram<MarkerLabelT, NodeT, ValGraphT> &dendrogram,
3012 Graph<NodeT, ValGraphT> & associated_mst,
3013 double desiredNbrRegions,
3016 Dendrogram<MarkerLabelT, NodeT, ValGraphT> dendrobis = dendrogram.clone();
3017 Graph<NodeT, ValGraphT> mstbis = associated_mst.clone();
3018 dendrobis.sortNodes(
true);
3020 MarkerLabelT desiredNbrRegionsbis = desiredNbrRegions;
3021 if (desiredNbrRegions <= 1) {
3022 desiredNbrRegionsbis =
3023 (dendrobis.getNbrNodes() / 2 + 1) * desiredNbrRegions;
3025 if ((desiredNbrRegionsbis > 1) &&
3026 (desiredNbrRegionsbis <= (dendrobis.getNbrNodes() / 2 + 1))) {
3027 double lamb = dendrobis.getNodeValue(desiredNbrRegionsbis - 2,
3028 "internalNodeValuationInitial");
3029 dendrobis.removeMSTEdgesDendrogram(mstbis, lamb);
3030 graphToMosaic(imLabels, mstbis, imOut);
3032 cerr <<
"getLevelHierarchySeg(imFinePartition,dendro,associated_mst,"
3033 "desiredNbrRegions,imOut) "
3034 <<
"-> the desiredNbrRegions specified is higher than the number of "
3048 template <
class MarkerLabelT,
class NodeT,
class ValGraphT>
3051 Dendrogram<MarkerLabelT, NodeT, ValGraphT> &dendrogram,
3052 Graph<NodeT, ValGraphT> &associated_mst,
double thresh,
3056 Dendrogram<MarkerLabelT, NodeT, ValGraphT> dendrobis = dendrogram.clone();
3057 Graph<NodeT, ValGraphT> mstbis = associated_mst.clone();
3058 dendrobis.sortNodes(
true);
3059 dendrobis.removeMSTEdgesDendrogram(mstbis, thresh);
3060 graphToMosaic(imLabels, mstbis, imOut);
3062 cout <<
"getLevelHierarchySeg(imFinePartition,dendro,associated_mst,"
3065 <<
"-> the thresh specified must be higher than 0" << endl;
DendroNode : node of a Dendrogram.
Definition: DendroNode.hpp:57
double getInternalNodeValuationInitial()
Setters and getters.
Definition: DendroNode.hpp:196
Dendrogram.
Definition: Dendrogram.hpp:49
Dendrogram(const Dendrogram &dendrogramToCopy)
Copy constructor.
Definition: Dendrogram.hpp:65
Dendrogram(Graph< NodeT, ValGraphT > &mst)
Dendrogram constructor from a MST graph.
Definition: Dendrogram.hpp:126
std::vector< MarkerLabelT > getLookupProgeny(size_t nodeIndex, string nameOfValueWanted)
Get the lookup of the progeny of a node.
Definition: Dendrogram.hpp:2114
Dendrogram()
Default constructor.
Definition: Dendrogram.hpp:63
void sortReverseNodes()
Reorganizes dendroNodes by decreasing valuationInitial.
Definition: Dendrogram.hpp:328
void normalize(const std::string typeOfNormalization="reg")
Dendrogram ultrametric values normalization.
Definition: Dendrogram.hpp:731
void removeMSTEdgesDendrogram(Graph< NodeT, ValGraphT > &associated_mst, double lbd)
Given an internal node index lambda of the dendrogram, remove corresponding edge in the associated MS...
Definition: Dendrogram.hpp:811
double getNodeValue(size_t nodeIndex, string nameOfValueWanted)
Access a value of a node in the dendrogram.
Definition: Dendrogram.hpp:2027
std::vector< std::vector< double > > getSaliences(string typeOfMatrix="sparse", string momentOfLife="death")
Compute saliences matrix (salience expresses ultrametric values)
Definition: Dendrogram.hpp:2432
std::vector< MarkerLabelT > getThreshLookupProgeny(double thresh, string nameOfValueWanted)
Get the lookup of the progeny below a certain ultrametric threshold.
Definition: Dendrogram.hpp:2141
void HierarchicalConstruction(const std::string typeOfHierarchy, const int nParam=50, const smil::Image< MarkerLabelT > &imMosa=smil::Image< MarkerLabelT >(), const std::string typeOfTransform="erode", const StrElt &se=DEFAULT_SE)
Computes a new hierarchy from a given dendrogram hierarchy.
Definition: Dendrogram.hpp:844
virtual ~Dendrogram()
Destructor.
Definition: Dendrogram.hpp:306
void setNodeValue(size_t nodeIndex, string nameOfValueWanted, double value)
Manually modify a value of a node in the dendrogram.
Definition: Dendrogram.hpp:2074
std::vector< std::vector< double > > getPersistences(string typeOfMatrix="sparse", string momentOfLife="death")
Compute persistences matrix (persistence measures only the order of fusion)
Definition: Dendrogram.hpp:2200
void EnergyConstruction(const std::string typeOfHierarchy)
Computes a new hierarchy from a given dendrogram hierarchy.
Definition: Dendrogram.hpp:1798
void putValuationsFinalInInitial()
Put the internalNodeValuationFinal in internalNodeValuationInitial and then set internalNodeValuation...
Definition: Dendrogram.hpp:785
double computeDasguptaCF(Graph< NodeT, ValGraphT > completeGraph)
Compute dasgupta score (paper "A cost function for similarity-based hierarchical clustering")
Definition: Dendrogram.hpp:2657
void sortNodes(bool reverse=false)
Reorganizes dendroNodes...
Definition: Dendrogram.hpp:318
Dendrogram(Graph< NodeT, ValGraphT > &mst, smil::Image< MarkerLabelT > &imLabels, smil::Image< MarkerLabelT > &imIn, const size_t nbOfMoments=5)
Constructor from a MST graph and labels/image to compute the moments.
Definition: Dendrogram.hpp:217
Dendrogram clone()
Clone Dendrogram.
Definition: Dendrogram.hpp:313
std::vector< DendroNodeType * > & getDendroNodes()
Setters and Getters.
Definition: Dendrogram.hpp:1986
Non-oriented graph.
Definition: DGraph.hpp:197
size_t getEdgeNbr()
getEdgeNbr() -
Definition: DGraph.hpp:371
size_t getNodeNbr()
getNodeNbr() -
Definition: DGraph.hpp:363
void removeEdge(const size_t index)
Remove an edge.
Definition: DGraph.hpp:425
EdgeListType & getEdges()
getEdges() - Get a vector containing the graph edges
Definition: DGraph.hpp:534
void sortEdges(bool reverse=false)
Sort edges (by weight as defined by the operator < of class Edge)
Definition: DGraph.hpp:334
NodeValuesType & getNodeValues()
getNodeValues() -
Definition: DGraph.hpp:542
Main Image class.
Definition: DImage.hpp:57
Base structuring element.
Definition: DStructuringElement.h:70
RES_T mul(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
mul() - Multiply two images
Definition: DImageArith.hpp:749
RES_T sub(const Image< T > &imIn1, const Image< T > &imIn2, Image< T > &imOut)
sub() - Subtraction between two images
Definition: DImageArith.hpp:160
RES_T pow(const Image< T1 > &imIn, Image< T2 > &imOut, double exponent=2)
pow() - power of an image
Definition: DImageArith.hpp:905
RES_T compare(const Image< T1 > &imIn1, const char *compareType, const Image< T1 > &imIn2, const Image< T2 > &trueIm, const Image< T2 > &falseIm, Image< T2 > &imOut)
compare() - Compare each pixel of two images (or an image and a value)
Definition: DImageArith.hpp:1244
RES_T applyLookup(const Image< T1 > &imIn, const mapT &_map, Image< T2 > &imOut, T2 defaultValue=T2(0))
applyLookup() - Apply a lookup map to a labeled image
Definition: DImageArith.hpp:1990
RES_T open(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
open() - Morphological grayscale opening
Definition: DMorphoFilter.hpp:123
RES_T close(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE)
close() - Morphological grayscale closing
Definition: DMorphoFilter.hpp:70
size_t area(const Image< T > &imIn)
area() - Area of an image
Definition: DMeasures.hpp:91
T maxVal(const Image< T > &imIn, bool onlyNonZero=false)
maxVal() - Max value of an image
Definition: DMeasures.hpp:348
RES_T erode(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE, const T borderVal=ImDtTypes< T >::max())
erode() - Morphological grayscale erosion
Definition: DMorphoBase.hpp:112
RES_T dilate(const Image< T > &imIn, Image< T > &imOut, const StrElt &se=DEFAULT_SE, const T borderVal=ImDtTypes< T >::min())
dilate() - Morphological grayscale dilation
Definition: DMorphoBase.hpp:65