ObjectData2D.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Created by wrede on 02.06.16.
  3. //
  4. #include <opencv2/imgproc.hpp>
  5. #include "ObjectData2D.h"
  6. #include "../util/MyMath.h"
  7. namespace core
  8. {
  9. ObjectData2D::ObjectData2D(size_t frame_index, cv::Point2d position)
  10. : ObjectData(frame_index),
  11. position_(position),
  12. temporal_weight_(1.0),
  13. spatial_weight_(1.0)
  14. {
  15. }
  16. void ObjectData2D::SetTemporalWeight(double weight)
  17. {
  18. temporal_weight_ = weight;
  19. }
  20. void ObjectData2D::SetSpatialWeight(double weight)
  21. {
  22. spatial_weight_ = weight;
  23. }
  24. cv::Point2d ObjectData2D::GetPosition() const
  25. {
  26. return position_;
  27. }
  28. double ObjectData2D::GetTemporalWeight() const
  29. {
  30. return temporal_weight_;
  31. }
  32. double ObjectData2D::GetSpatialWeight() const
  33. {
  34. return spatial_weight_;
  35. }
  36. double ObjectData2D::CompareTo(ObjectDataPtr obj) const
  37. {
  38. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  39. double d_temp = obj_2d->GetFrameIndex() - GetFrameIndex();
  40. double d_spat = util::MyMath::EuclideanDistance(position_, obj_2d->position_);
  41. return d_temp * temporal_weight_ + d_spat * spatial_weight_;
  42. }
  43. ObjectDataPtr ObjectData2D::Interpolate(ObjectDataPtr obj,
  44. double fraction) const
  45. {
  46. ObjectDataPtr obj_in = ObjectData::Interpolate(obj, fraction);
  47. ObjectData2DPtr obj_2d = std::static_pointer_cast<ObjectData2D>(obj);
  48. double x = util::MyMath::Lerp(position_.x, obj_2d->position_.x, fraction);
  49. double y = util::MyMath::Lerp(position_.y, obj_2d->position_.y, fraction);
  50. ObjectData2DPtr obj_out(
  51. new ObjectData2D(obj_in->GetFrameIndex(), cv::Point2d(x, y)));
  52. return obj_out;
  53. }
  54. void ObjectData2D::Print(std::ostream& os) const
  55. {
  56. os << "ObjectData2D{"
  57. << "frame: " << GetFrameIndex() << ","
  58. << "x: " << position_.x << ","
  59. << "y: " << position_.y << "}";
  60. }
  61. void ObjectData2D::Visualize(cv::Mat& image, cv::Scalar& color) const
  62. {
  63. double x = position_.x * image.cols;
  64. double y = position_.y * image.rows;
  65. int r = (int) (0.005 * (image.rows + image.cols) * 0.5);
  66. cv::circle(image, cv::Point2d(x, y), r, color);
  67. }
  68. }