ObjectDataBox.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Created by wrede on 09.06.16.
  3. //
  4. #include "ObjectDataBox.h"
  5. #include <math.h>
  6. #include "../util/MyMath.h"
  7. namespace core
  8. {
  9. const std::string ObjectDataBox::CONSTRAINT_WIDTH_DIFFERENCE = "width_difference";
  10. const std::string ObjectDataBox::CONSTRAINT_HEIGHT_DIFFERENCE = "height_difference";
  11. ObjectDataBox::ObjectDataBox(size_t frame_index, cv::Point2d center,
  12. cv::Point2d size)
  13. : ObjectData2D(frame_index, center),
  14. size_(size)
  15. {
  16. }
  17. void ObjectDataBox::Print(std::ostream& os) const
  18. {
  19. os << "ObjectDataBox{"
  20. << "frame: " << GetFrameIndex() << ","
  21. << "x: " << GetPosition().x << ","
  22. << "x: " << GetPosition().y << ","
  23. << "width: " << size_.x << ","
  24. << "height: " << size_.y << "}";
  25. }
  26. double ObjectDataBox::CompareTo(ObjectDataPtr obj) const
  27. {
  28. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  29. cv::Point2d this_center = GetPosition() + size_ * 0.5;
  30. cv::Point2d other_center = other->GetPosition() + other->size_ * 0.5;
  31. double d_temp = other->GetFrameIndex() - GetFrameIndex();
  32. double d_spat = util::MyMath::EuclideanDistance(this_center, other_center);
  33. return d_temp * GetTemporalWeight() + d_spat * GetSpatialWeight();
  34. }
  35. bool ObjectDataBox::IsWithinConstraints(ObjectDataPtr obj,
  36. std::unordered_map<std::string, double> & constraints)
  37. const
  38. {
  39. if (!ObjectData2D::IsWithinConstraints(obj, constraints))
  40. return false;
  41. ObjectDataBoxPtr obj_box = std::static_pointer_cast<ObjectDataBox>(obj);
  42. if (constraints.count(CONSTRAINT_WIDTH_DIFFERENCE) > 0) {
  43. double width_difference = fabs(size_.x - obj_box->size_.x);
  44. if (width_difference > constraints[CONSTRAINT_WIDTH_DIFFERENCE])
  45. return false;
  46. }
  47. if (constraints.count(CONSTRAINT_HEIGHT_DIFFERENCE) > 0) {
  48. double height_difference = fabs(size_.y - obj_box->size_.y);
  49. if (height_difference > constraints[CONSTRAINT_HEIGHT_DIFFERENCE])
  50. return false;
  51. }
  52. return true;
  53. }
  54. ObjectDataPtr ObjectDataBox::Interpolate(ObjectDataPtr obj,
  55. double fraction) const
  56. {
  57. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  58. size_t frame = (size_t) fabs(util::MyMath::Lerp(GetFrameIndex(),
  59. other->GetFrameIndex(),
  60. fraction));
  61. double x = util::MyMath::Lerp(GetPosition().x, other->GetPosition().x, fraction);
  62. double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y, fraction);
  63. double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
  64. double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
  65. ObjectDataBoxPtr result(
  66. new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
  67. return result;
  68. }
  69. void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color) const
  70. {
  71. cv::Point2d center(GetPosition().x * image.cols, GetPosition().y * image.rows);
  72. cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
  73. cv::Point2d top_left = center - size * 0.5;
  74. cv::rectangle(image, top_left, top_left + size, color);
  75. }
  76. cv::Point2d ObjectDataBox::GetSize() const
  77. {
  78. return size_;
  79. }
  80. std::string ObjectDataBox::ToString(char delimiter) const
  81. {
  82. return ObjectData2D::ToString(delimiter) + delimiter +
  83. std::to_string(size_.x) + delimiter + std::to_string(size_.y);
  84. }
  85. }