Filter2D.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Created by wrede on 11.07.16.
  3. //
  4. #ifndef GBMOT_FILTER_H
  5. #define GBMOT_FILTER_H
  6. #include <vector>
  7. #include <string>
  8. namespace util
  9. {
  10. /**
  11. * Utility class for a two-dimensional filter.
  12. */
  13. class Filter2D
  14. {
  15. public:
  16. /**
  17. * Creates a new instance with an identity filter.
  18. */
  19. Filter2D();
  20. /**
  21. * Creates a new instance with the given multiplier and mask values.
  22. * To calculate the value all values are added with their weights from the mask and then
  23. * the value is multiplied by the multiplier. The multiplier may be used to ensure the
  24. * overall filter sum is equals one.
  25. *
  26. * @param multiplier The multiplier
  27. * @param mask The mask values
  28. */
  29. Filter2D(double multiplier, std::vector<double> mask);
  30. // Example:
  31. // mask_string = multiplier, m00, m01, m02, ..., m10, m11, m12, ..., mnn
  32. // delimiter = ,
  33. /**
  34. * Creates a new instance by parsing the specified mask string.
  35. * Values are separated by the specified delimiter.
  36. * Example: 'multiplier, m00, m01, m02, ..., m10, m11, m12, ..., mnn' as the mask string
  37. * and ',' as the delimiter.
  38. *
  39. * @param mask_string The string with the multiplier and the mask values to parse
  40. * @param delimiter The delimiter used for the parsing
  41. */
  42. Filter2D(std::string const & mask_string, char delimiter);
  43. /**
  44. * Gets the value in the mask at the specified position.
  45. *
  46. * @param x The horizontal position
  47. * @param y The vertical position
  48. * @return The value
  49. */
  50. double Get(int x, int y) const;
  51. /**
  52. * Gets the value in the mask at the specified position.
  53. *
  54. * @param i The index (row-major)
  55. * @return The value
  56. */
  57. double Get(int i) const;
  58. /**
  59. * Gets the multiplier.
  60. *
  61. * @return The multiplier
  62. */
  63. double GetMultiplier() const;
  64. /**
  65. * Gets the vicinity of the mask.
  66. * The vicinity is the based on the center and describes the maximum range of values
  67. * around it in manhattan distance.
  68. *
  69. * @return The vicinity
  70. */
  71. int GetVicinity() const;
  72. /**
  73. * Gets the dimension of the mask.
  74. *
  75. * @return The dimension
  76. */
  77. int GetDimension() const;
  78. /**
  79. * Normalizes the mask values.
  80. * The multiplier is equals one afterwards.
  81. */
  82. void Normalize();
  83. private:
  84. /**
  85. * Calculates the index for the specified x and y values.
  86. * The mask is stored in row-major order.
  87. *
  88. * @param x The horizontal value
  89. * @param y The vertical value
  90. * @return The index
  91. */
  92. int Index(int x, int y) const;
  93. /**
  94. * Calculates the vicinity for the specified size.
  95. * The vicinity is the based on the center and describes the maximum range of values
  96. * around it in manhattan distance.
  97. *
  98. * @param size The size
  99. * @return The vicinity
  100. */
  101. int Vicinity(int size) const;
  102. /**
  103. * Calculates the dimension for the specified size.
  104. *
  105. * @param size The size
  106. * @return The dimension
  107. */
  108. int Dimension(int size) const;
  109. /**
  110. * The mask values stored in row-major order
  111. */
  112. std::vector<double> mask_;
  113. /**
  114. * The multiplier
  115. */
  116. double multiplier_;
  117. /**
  118. * The count of the values in the mask
  119. */
  120. int dimension_;
  121. /**
  122. * The vicinity is the based on the center and describes the maximum range of values
  123. * around it in manhattan distance
  124. */
  125. int vicinity_;
  126. };
  127. }
  128. #endif //GBMOT_FILTER_H