ObjectDataBox.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. ObjectDataBox::ObjectDataBox(size_t frame_index, cv::Point2d center,
  10. cv::Point2d size)
  11. : ObjectData2D(frame_index, center),
  12. size_(size)
  13. {
  14. }
  15. void ObjectDataBox::Print(std::ostream& os) const
  16. {
  17. os << "ObjectDataBox{"
  18. << "frame: " << GetFrameIndex() << ","
  19. << "x: " << GetPosition().x << ","
  20. << "x: " << GetPosition().y << ","
  21. << "width: " << size_.x << ","
  22. << "height: " << size_.y << "}";
  23. }
  24. double ObjectDataBox::CompareTo(ObjectDataPtr obj) const
  25. {
  26. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  27. cv::Point2d this_center = GetPosition() + size_ * 0.5;
  28. cv::Point2d other_center = other->GetPosition() + other->size_ * 0.5;
  29. double d_temp = other->GetFrameIndex() - GetFrameIndex();
  30. double d_spat = util::MyMath::EuclideanDistance(this_center, other_center);
  31. return d_temp * GetTemporalWeight() + d_spat * GetSpatialWeight();
  32. }
  33. ObjectDataPtr ObjectDataBox::Interpolate(ObjectDataPtr obj,
  34. double fraction) const
  35. {
  36. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  37. size_t frame = (size_t) fabs(util::MyMath::Lerp(GetFrameIndex(),
  38. other->GetFrameIndex(),
  39. fraction));
  40. double x = util::MyMath::Lerp(GetPosition().x, other->GetPosition().x, fraction);
  41. double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y, fraction);
  42. double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
  43. double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
  44. ObjectDataBoxPtr result(
  45. new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
  46. return result;
  47. }
  48. void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color) const
  49. {
  50. cv::Point2d center(GetPosition().x * image.cols, GetPosition().y * image.rows);
  51. cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
  52. cv::Point2d top_left = center - size * 0.5;
  53. cv::rectangle(image, top_left, top_left + size, color);
  54. }
  55. cv::Point2d ObjectDataBox::GetSize() const
  56. {
  57. return size_;
  58. }
  59. std::string ObjectDataBox::ToString(char delimiter) const
  60. {
  61. return ObjectData2D::ToString(delimiter) + delimiter +
  62. std::to_string(size_.x) + delimiter + std::to_string(size_.y);
  63. }
  64. }