ObjectDataBox.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Created by wrede on 09.06.16.
  3. //
  4. #include "ObjectDataBox.h"
  5. #include "../util/MyMath.h"
  6. namespace core
  7. {
  8. ObjectDataBox::ObjectDataBox(size_t frame_index, cv::Point2d anchor,
  9. cv::Point2d size)
  10. : ObjectData2D(frame_index, anchor),
  11. size_(size)
  12. {
  13. }
  14. void ObjectDataBox::Print(std::ostream& os) const
  15. {
  16. os << "ObjectDataBox{"
  17. << "frame: " << GetFrameIndex() << ","
  18. << "x: " << GetPosition().x << ","
  19. << "x: " << GetPosition().y << ","
  20. << "width: " << size_.x << ","
  21. << "height: " << size_.y << "}";
  22. }
  23. double ObjectDataBox::CompareTo(ObjectDataPtr obj) const
  24. {
  25. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  26. cv::Point2d this_center = GetPosition() + size_ * 0.5;
  27. cv::Point2d other_center = other->GetPosition() + other->size_ * 0.5;
  28. double d_temp = other->GetFrameIndex() - GetFrameIndex();
  29. double d_spat = util::MyMath::EuclideanDistance(this_center,
  30. 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,
  41. fraction);
  42. double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y,
  43. fraction);
  44. double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
  45. double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
  46. ObjectDataBoxPtr result(
  47. new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
  48. return result;
  49. }
  50. void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color) const
  51. {
  52. cv::Point2d position(GetPosition().x * image.cols,
  53. GetPosition().y * image.rows);
  54. cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
  55. cv::rectangle(image, position, position + size, color);
  56. }
  57. cv::Point2d ObjectDataBox::GetSize() const
  58. {
  59. return size_;
  60. }
  61. }