RSMarkovCluster.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #ifndef RSMARKOVCLUSTER
  2. #define RSMARKOVCLUSTER
  3. /**
  4. * @file RSMarkovCluster.h
  5. * @brief ...
  6. * @author Björn Fröhlich, Alexander Freytag
  7. * @date ???
  8. */
  9. #include <core/vector/VectorT.h>
  10. #include <core/basics/Config.h>
  11. #include <segmentation/math/NodeCentricRepMatrix.h>
  12. #include <core/vector/VVector.h>
  13. #include <segmentation/RegionSegmentationMethod.h>
  14. #include <core/imagedisplay/ImageDisplay.h>
  15. // std - includes
  16. #include <algorithm>
  17. #include <limits>
  18. #include <sys/types.h>
  19. namespace OBJREC
  20. {
  21. class RSMarkovCluster : public RegionSegmentationMethod
  22. {
  23. /* Typedefs und Memberklassen */
  24. public:
  25. /**
  26. * @brief Memberklasse um ein Koordinatenpaar zu repraesentieren.
  27. */
  28. class Coord : public std::pair< int, int >
  29. {
  30. public:
  31. //! Standard-Konstruktor
  32. Coord() : std::pair<int, int>() {};
  33. //! Initialisierungs-Konstruktor
  34. Coord ( const int y, const int x ) : std::pair<int, int> ( y, x ) {};
  35. //! Copy-Konstruktor
  36. Coord ( Coord const& coord ) : std::pair <int, int> ( coord ) {};
  37. //! Standard-Destruktor
  38. ~Coord() {};
  39. /**
  40. * @brief Einfache Methode zum berechnen der PNorm eines Koordinatenpaares.
  41. * @param p - Normart
  42. * @return p-Norm
  43. */
  44. inline double pnorm ( const uint p ) const
  45. {
  46. return pow ( pow ( abs ( std::pair< int, int >::first ), p ) + pow ( abs ( std::pair< int, int >::second ), p ), ( 1.0 / p ) );
  47. };
  48. /* Operatoren */
  49. //! =+ operator
  50. Coord& operator+= ( Coord const& rhs )
  51. {
  52. ( *this ).first += rhs.first;
  53. ( *this ).second += rhs.second;
  54. return ( *this );
  55. };
  56. //! + operator
  57. Coord operator+ ( Coord const& rhs )
  58. {
  59. Coord tmp ( ( *this ) );
  60. tmp += rhs;
  61. return tmp;
  62. };
  63. };
  64. private:
  65. //! Simpler Datentyp um die Offsets zu speichern.
  66. typedef std::vector< RSMarkovCluster::Coord > Offsets;
  67. //! Simpler Datentyp zum Speichern der Detours.
  68. typedef std::vector< std::vector< std::pair< uint, uint > > > Detours;
  69. /* Membermethoden und -variablen */
  70. public:
  71. ///////////////////// ///////////////////// /////////////////////
  72. // CONSTRUCTORS / DESTRUCTORS
  73. ///////////////////// ///////////////////// /////////////////////
  74. /**
  75. * @brief default constructor
  76. * @author Alexander Freytag
  77. * @date 08-02-2014 ( dd-mm-yyyy )
  78. */
  79. RSMarkovCluster();
  80. //! Standard-Konstruktor
  81. RSMarkovCluster ( const NICE::Config* conf );
  82. //! Standard-Destruktor
  83. virtual ~RSMarkovCluster();
  84. /**
  85. * @brief Setup internal variables and objects used
  86. * @author Alexander Freytag
  87. * @param conf Config file to specify variable settings
  88. * @param s_confSection
  89. * @date 08-02-2014 ( dd-mm-yyyy )
  90. */
  91. virtual void initFromConfig ( const NICE::Config * _conf, const std::string & _confSection = "RSMarkovCluster" );
  92. ///////////////////// ///////////////////// /////////////////////
  93. // SEGMENTATION STUFF
  94. ///////////////////// ///////////////////// //////////////////
  95. /* Ueberschriebene Methoden */
  96. /**
  97. * @brief Segmentierungsmethode.
  98. * @param Image - (Grauwert) Eingabebild
  99. * @param Matrix - Segmentierungsmaske
  100. */
  101. int segRegions ( const NICE::Image &img, NICE::Matrix &mask ) const;
  102. /**
  103. * @brief Segmentierungsmethode.
  104. * @param ColorImage - (Farbbild) Eingabebild
  105. * @param Matrix - Segmentierungsmaske
  106. */
  107. int segRegions ( const NICE::ColorImage &cimg, NICE::Matrix &mask ) const;
  108. private:
  109. int iterations;
  110. //! Clusterradius
  111. double r;
  112. //! Edgeweight Parameter
  113. double mu;
  114. //! Inflation Parameter
  115. double p;
  116. //! Chaosthreshold
  117. double chaosThreshold;
  118. /* */
  119. /**
  120. * @brief Vorausberechnung der Offsets
  121. */
  122. void precalcOffsets ( Offsets& offsets ) const;
  123. /**
  124. * @brief Vorausberechnung der Detours
  125. */
  126. void precalcDetours ( Detours& detours, const Offsets& offsets ) const;
  127. /**
  128. * @brief Finder Cluster und markieren derer in der mark Maske.
  129. */
  130. int findCluster ( const Offsets& offsets, OBJREC::NodeCentricRepMatrix& L, NICE::Matrix& mark ) const;
  131. /**
  132. * @brief Errechnen des Indexes eines Pixels anhand seiner Koordinaten.
  133. */
  134. inline uint indexOfPixel ( const Coord& yx, const uint xSize ) const;
  135. /**
  136. * @brief Normalisieren der L Matrix. Dh. alle abgehenenden Kanten werden zu 1 normiert.
  137. */
  138. void normalize ( OBJREC::NodeCentricRepMatrix& L ) const;
  139. /**
  140. * @brief Inflationoperator anwenden. Es gilt: Lout<--inf(Lin)
  141. */
  142. void inflation ( const OBJREC::NodeCentricRepMatrix& Lin, OBJREC::NodeCentricRepMatrix& Lout ) const;
  143. /**
  144. * @brief Expansionoperator anwenden. Es gilt: Lout<--exp(Lin)
  145. */
  146. void expansion ( const OBJREC::NodeCentricRepMatrix& Lin, OBJREC::NodeCentricRepMatrix& Lout, const Offsets& offsets, const Detours& detours ) const;
  147. /**
  148. * @brief Ueberprueft auf negativ NaN und gibt in dem Falle 0.0 zurueck, sonst val.
  149. */
  150. inline double checkForNAN ( const double val ) const;
  151. /**
  152. * @brief Anwenden des Clusterings auf die L1-Matrix.
  153. * @note Kovergenzkriterium ist die Veraenderung innerhalb der Matrix zwischen aufeinanderfolgenden Iterationen.
  154. */
  155. void runClustering ( const Offsets& offsets, const Detours& detours, OBJREC::NodeCentricRepMatrix& L1 ) const;
  156. /**
  157. * @brief Initialisierung der MarkovMatrix. Diese Methode ist anwendbar fuer Farb- und Grauwertbilder.
  158. */
  159. void initMarkovMatrix ( const uchar* imageData, const uint imageHeight, const uint imageWidth, const uint channelCount, const Offsets& offsets, OBJREC::NodeCentricRepMatrix& L ) const;
  160. /**
  161. * @brief Berechnung der Kantenstaerke fuer eine gegebene Farb- oder Grauwertdifferenz.
  162. */
  163. inline double edgeWeight ( const NICE::Vector& imageValueDiff ) const;
  164. /**
  165. * @brief Ausgabe der Matrix in eine Datei. VORSICHT: nur fuer sehr kleine Bilder anwenden.
  166. */
  167. void printMatrix ( const OBJREC::NodeCentricRepMatrix& L, const Offsets& offsets, const std::string& filename ) const;
  168. ///////////////////// INTERFACE PERSISTENT /////////////////////
  169. // interface specific methods for store and restore
  170. ///////////////////// INTERFACE PERSISTENT /////////////////////
  171. /**
  172. * @brief Load object from external file (stream)
  173. * @author Alexander Freytag
  174. * @date 08-02-2014 ( dd-mm-yyyy )
  175. */
  176. void restore ( std::istream & is, int format = 0 );
  177. /**
  178. * @brief Save object to external file (stream)
  179. * @author Alexander Freytag
  180. * @date 08-02-2014 ( dd-mm-yyyy )
  181. */
  182. void store ( std::ostream & os, int format = 0 ) const;
  183. /**
  184. * @brief Clear object
  185. * @author Alexander Freytag
  186. * @date 08-02-2014 ( dd-mm-yyyy )
  187. */
  188. void clear ();
  189. };
  190. }
  191. #endif