RSMarkovCluster.h 7.0 KB

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