ObjectDataBox.cpp 2.6 KB

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