Filter2D.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /**
  31. * Creates a new instance by parsing the specified mask string.
  32. * Values are separated by the specified delimiter.
  33. * Example: 'multiplier, m00, m01, m02, ..., m10, m11, m12, ..., mnn' as the mask string
  34. * and ',' as the delimiter.
  35. *
  36. * @param mask_string The string with the multiplier and the mask values to parse
  37. * @param delimiter The delimiter used for the parsing
  38. */
  39. Filter2D(std::string const & mask_string, char delimiter);
  40. /**
  41. * Gets the value in the mask at the specified position.
  42. *
  43. * @param x The horizontal position
  44. * @param y The vertical position
  45. * @return The value
  46. */
  47. double Get(int x, int y) const;
  48. /**
  49. * Gets the value in the mask at the specified position.
  50. *
  51. * @param i The index (row-major)
  52. * @return The value
  53. */
  54. double Get(int i) const;
  55. /**
  56. * Gets the multiplier.
  57. *
  58. * @return The multiplier
  59. */
  60. double GetMultiplier() const;
  61. /**
  62. * Gets the vicinity of the mask.
  63. * The vicinity is the based on the center and describes the maximum range of values
  64. * around it in manhattan distance.
  65. *
  66. * @return The vicinity
  67. */
  68. int GetVicinity() const;
  69. /**
  70. * Gets the dimension of the mask.
  71. *
  72. * @return The dimension
  73. */
  74. int GetDimension() const;
  75. /**
  76. * Normalizes the mask values.
  77. * The multiplier is equals one afterwards.
  78. */
  79. void Normalize();
  80. private:
  81. /**
  82. * Calculates the index for the specified x and y values.
  83. * The mask is stored in row-major order.
  84. *
  85. * @param x The horizontal value
  86. * @param y The vertical value
  87. * @return The index
  88. */
  89. int Index(int x, int y) const;
  90. /**
  91. * Calculates the vicinity for the specified size.
  92. * The vicinity is the based on the center and describes the maximum range of values
  93. * around it in manhattan distance.
  94. *
  95. * @param size The size
  96. * @return The vicinity
  97. */
  98. int Vicinity(int size) const;
  99. /**
  100. * Calculates the dimension for the specified size.
  101. *
  102. * @param size The size
  103. * @return The dimension
  104. */
  105. int Dimension(int size) const;
  106. /**
  107. * The mask values stored in row-major order
  108. */
  109. std::vector<double> mask_;
  110. /**
  111. * The multiplier
  112. */
  113. double multiplier_;
  114. /**
  115. * The count of the values in the mask
  116. */
  117. int dimension_;
  118. /**
  119. * The vicinity is the based on the center and describes the maximum range of values
  120. * around it in manhattan distance
  121. */
  122. int vicinity_;
  123. };
  124. }
  125. #endif //GBMOT_FILTER_H