ObjectDataBox.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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() - 1;
  32. double d_spat = util::MyMath::EuclideanDistance(GetPosition(), other->GetPosition());
  33. double weight = d_temp * GetTemporalWeight() + d_spat * GetSpatialWeight();
  34. // std::cout << "box weight " << weight << ", t:" << d_temp << ", s:" << d_spat << std::endl;
  35. return weight;
  36. }
  37. bool ObjectDataBox::IsWithinConstraints(ObjectDataPtr obj,
  38. std::unordered_map<std::string, double> & constraints)
  39. const
  40. {
  41. if (!ObjectData2D::IsWithinConstraints(obj, constraints))
  42. return false;
  43. ObjectDataBoxPtr obj_box = std::static_pointer_cast<ObjectDataBox>(obj);
  44. if (constraints.count(CONSTRAINT_WIDTH_DIFFERENCE) > 0) {
  45. double width_difference = fabs(size_.x - obj_box->size_.x);
  46. if (width_difference > constraints[CONSTRAINT_WIDTH_DIFFERENCE])
  47. return false;
  48. }
  49. if (constraints.count(CONSTRAINT_HEIGHT_DIFFERENCE) > 0) {
  50. double height_difference = fabs(size_.y - obj_box->size_.y);
  51. if (height_difference > constraints[CONSTRAINT_HEIGHT_DIFFERENCE])
  52. return false;
  53. }
  54. return true;
  55. }
  56. ObjectDataPtr ObjectDataBox::Interpolate(ObjectDataPtr obj,
  57. double fraction) const
  58. {
  59. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  60. size_t frame = (size_t) fabs(util::MyMath::Lerp(GetFrameIndex(),
  61. other->GetFrameIndex(),
  62. fraction));
  63. double x = util::MyMath::Lerp(GetPosition().x, other->GetPosition().x, fraction);
  64. double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y, fraction);
  65. double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
  66. double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
  67. ObjectDataBoxPtr result(
  68. new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
  69. return result;
  70. }
  71. void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color) const
  72. {
  73. cv::Point2d center(GetPosition().x * image.cols, GetPosition().y * image.rows);
  74. cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
  75. cv::Point2d top_left = center - size * 0.5;
  76. cv::rectangle(image, top_left, top_left + size, color);
  77. }
  78. cv::Point2d ObjectDataBox::GetSize() const
  79. {
  80. return size_;
  81. }
  82. std::string ObjectDataBox::ToString(char delimiter) const
  83. {
  84. return ObjectData2D::ToString(delimiter) + delimiter +
  85. std::to_string(size_.x) + delimiter + std::to_string(size_.y);
  86. }
  87. }