ObjectDataBox.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 anchor,
  10. cv::Point2d size)
  11. : ObjectData2D(frame_index, anchor),
  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,
  31. other_center);
  32. return d_temp * GetTemporalWeight() + d_spat * GetSpatialWeight();
  33. }
  34. ObjectDataPtr ObjectDataBox::Interpolate(ObjectDataPtr obj,
  35. double fraction) const
  36. {
  37. ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
  38. size_t frame = (size_t) fabs(util::MyMath::Lerp(GetFrameIndex(),
  39. other->GetFrameIndex(),
  40. fraction));
  41. double x = util::MyMath::Lerp(GetPosition().x, other->GetPosition().x,
  42. fraction);
  43. double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y,
  44. fraction);
  45. double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
  46. double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
  47. ObjectDataBoxPtr result(
  48. new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
  49. return result;
  50. }
  51. void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color) const
  52. {
  53. cv::Point2d position(GetPosition().x * image.cols,
  54. GetPosition().y * image.rows);
  55. cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
  56. cv::rectangle(image, position, position + size, color);
  57. }
  58. cv::Point2d ObjectDataBox::GetSize() const
  59. {
  60. return size_;
  61. }
  62. }