RSMarkovCluster.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #ifndef RSMARKOVCLUSTER
  2. #define RSMARKOVCLUSTER
  3. #include <core/vector/VectorT.h>
  4. #include <core/basics/Config.h>
  5. #include <segmentation/math/NodeCentricRepMatrix.h>
  6. #include <core/vector/VVector.h>
  7. #include <segmentation/RegionSegmentationMethod.h>
  8. #include <core/imagedisplay/ImageDisplay.h>
  9. // std - includes
  10. #include <algorithm>
  11. #include <limits>
  12. #include <sys/types.h>
  13. namespace OBJREC
  14. {
  15. class RSMarkovCluster : public RegionSegmentationMethod
  16. {
  17. /* Typedefs und Memberklassen */
  18. public:
  19. /**
  20. * @brief Memberklasse um ein Koordinatenpaar zu repraesentieren.
  21. */
  22. class Coord : public std::pair< int, int >
  23. {
  24. public:
  25. //! Standard-Konstruktor
  26. Coord() : std::pair<int, int>() {};
  27. //! Initialisierungs-Konstruktor
  28. Coord ( const int y, const int x ) : std::pair<int, int> ( y, x ) {};
  29. //! Copy-Konstruktor
  30. Coord ( Coord const& coord ) : std::pair <int, int> ( coord ) {};
  31. //! Standard-Destruktor
  32. ~Coord() {};
  33. /**
  34. * @brief Einfache Methode zum berechnen der PNorm eines Koordinatenpaares.
  35. * @param p - Normart
  36. * @return p-Norm
  37. */
  38. inline double pnorm ( const uint p ) const
  39. {
  40. return pow ( pow ( abs ( std::pair< int, int >::first ), p ) + pow ( abs ( std::pair< int, int >::second ), p ), ( 1.0 / p ) );
  41. };
  42. /* Operatoren */
  43. //! =+ operator
  44. Coord& operator+= ( Coord const& rhs )
  45. {
  46. ( *this ).first += rhs.first;
  47. ( *this ).second += rhs.second;
  48. return ( *this );
  49. };
  50. //! + operator
  51. Coord operator+ ( Coord const& rhs )
  52. {
  53. Coord tmp ( ( *this ) );
  54. tmp += rhs;
  55. return tmp;
  56. };
  57. };
  58. private:
  59. //! Simpler Datentyp um die Offsets zu speichern.
  60. typedef std::vector< RSMarkovCluster::Coord > Offsets;
  61. //! Simpler Datentyp zum Speichern der Detours.
  62. typedef std::vector< std::vector< std::pair< uint, uint > > > Detours;
  63. /* Membermethoden und -variablen */
  64. public:
  65. //! Standard-Konstruktor
  66. RSMarkovCluster ( const NICE::Config* conf );
  67. //! Standard-Destruktor
  68. ~RSMarkovCluster() {};
  69. /* Ueberschriebene Methoden */
  70. /**
  71. * @brief Segmentierungsmethode.
  72. * @param Image - (Grauwert) Eingabebild
  73. * @param Matrix - Segmentierungsmaske
  74. */
  75. int segRegions ( const NICE::Image &img, NICE::Matrix &mask ) const;
  76. /**
  77. * @brief Segmentierungsmethode.
  78. * @param ColorImage - (Farbbild) Eingabebild
  79. * @param Matrix - Segmentierungsmaske
  80. */
  81. int segRegions ( const NICE::ColorImage &cimg, NICE::Matrix &mask ) const;
  82. private:
  83. int iterations;
  84. //! Clusterradius
  85. double r;
  86. //! Edgeweight Parameter
  87. double mu;
  88. //! Inflation Parameter
  89. double p;
  90. //! Chaosthreshold
  91. double chaosThreshold;
  92. /* */
  93. /**
  94. * @brief Vorausberechnung der Offsets
  95. */
  96. void precalcOffsets ( Offsets& offsets ) const;
  97. /**
  98. * @brief Vorausberechnung der Detours
  99. */
  100. void precalcDetours ( Detours& detours, const Offsets& offsets ) const;
  101. /**
  102. * @brief Finder Cluster und markieren derer in der mark Maske.
  103. */
  104. int findCluster ( const Offsets& offsets, OBJREC::NodeCentricRepMatrix& L, NICE::Matrix& mark ) const;
  105. /**
  106. * @brief Errechnen des Indexes eines Pixels anhand seiner Koordinaten.
  107. */
  108. inline uint indexOfPixel ( const Coord& yx, const uint xSize ) const;
  109. /**
  110. * @brief Normalisieren der L Matrix. Dh. alle abgehenenden Kanten werden zu 1 normiert.
  111. */
  112. void normalize ( OBJREC::NodeCentricRepMatrix& L ) const;
  113. /**
  114. * @brief Inflationoperator anwenden. Es gilt: Lout<--inf(Lin)
  115. */
  116. void inflation ( const OBJREC::NodeCentricRepMatrix& Lin, OBJREC::NodeCentricRepMatrix& Lout ) const;
  117. /**
  118. * @brief Expansionoperator anwenden. Es gilt: Lout<--exp(Lin)
  119. */
  120. void expansion ( const OBJREC::NodeCentricRepMatrix& Lin, OBJREC::NodeCentricRepMatrix& Lout, const Offsets& offsets, const Detours& detours ) const;
  121. /**
  122. * @brief Ueberprueft auf negativ NaN und gibt in dem Falle 0.0 zurueck, sonst val.
  123. */
  124. inline double checkForNAN ( const double val ) const;
  125. /**
  126. * @brief Anwenden des Clusterings auf die L1-Matrix.
  127. * @note Kovergenzkriterium ist die Veraenderung innerhalb der Matrix zwischen aufeinanderfolgenden Iterationen.
  128. */
  129. void runClustering ( const Offsets& offsets, const Detours& detours, OBJREC::NodeCentricRepMatrix& L1 ) const;
  130. /**
  131. * @brief Initialisierung der MarkovMatrix. Diese Methode ist anwendbar fuer Farb- und Grauwertbilder.
  132. */
  133. void initMarkovMatrix ( const uchar* imageData, const uint imageHeight, const uint imageWidth, const uint channelCount, const Offsets& offsets, OBJREC::NodeCentricRepMatrix& L ) const;
  134. /**
  135. * @brief Berechnung der Kantenstaerke fuer eine gegebene Farb- oder Grauwertdifferenz.
  136. */
  137. inline double edgeWeight ( const NICE::Vector& imageValueDiff ) const;
  138. /**
  139. * @brief Ausgabe der Matrix in eine Datei. VORSICHT: nur fuer sehr kleine Bilder anwenden.
  140. */
  141. void printMatrix ( const OBJREC::NodeCentricRepMatrix& L, const Offsets& offsets, const std::string& filename ) const;
  142. };
  143. }
  144. #endif