123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- //
- // Created by wrede on 09.06.16.
- //
- #include "ObjectDataBox.h"
- #include <math.h>
- #include "../util/MyMath.h"
- namespace core
- {
- const std::string ObjectDataBox::CONSTRAINT_WIDTH_DIFFERENCE = "width_difference";
- const std::string ObjectDataBox::CONSTRAINT_HEIGHT_DIFFERENCE = "height_difference";
- ObjectDataBox::ObjectDataBox(size_t frame_index, cv::Point2d center,
- cv::Point2d size)
- : ObjectData2D(frame_index, center),
- size_(size)
- {
- }
- void ObjectDataBox::Print(std::ostream& os) const
- {
- os << "ObjectDataBox{"
- << "frame: " << GetFrameIndex() << ","
- << "x: " << GetPosition().x << ","
- << "x: " << GetPosition().y << ","
- << "width: " << size_.x << ","
- << "height: " << size_.y << "}";
- }
- double ObjectDataBox::CompareTo(ObjectDataPtr obj) const
- {
- ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
- // cv::Point2d this_center = GetPosition() + size_ * 0.5;
- // cv::Point2d other_center = other->GetPosition() + other->size_ * 0.5;
- double d_temp = other->GetFrameIndex() - GetFrameIndex() - 1;
- double d_spat = util::MyMath::EuclideanDistance(GetPosition(), other->GetPosition());
- double weight = d_temp * GetTemporalWeight() + d_spat * GetSpatialWeight();
- // std::cout << "box weight " << weight << ", t:" << d_temp << ", s:" << d_spat << std::endl;
- return weight;
- }
- bool ObjectDataBox::IsWithinConstraints(ObjectDataPtr obj,
- std::unordered_map<std::string, double> & constraints)
- const
- {
- if (!ObjectData2D::IsWithinConstraints(obj, constraints))
- return false;
- ObjectDataBoxPtr obj_box = std::static_pointer_cast<ObjectDataBox>(obj);
- if (constraints.count(CONSTRAINT_WIDTH_DIFFERENCE) > 0) {
- double width_difference = fabs(size_.x - obj_box->size_.x);
- if (width_difference > constraints[CONSTRAINT_WIDTH_DIFFERENCE])
- return false;
- }
- if (constraints.count(CONSTRAINT_HEIGHT_DIFFERENCE) > 0) {
- double height_difference = fabs(size_.y - obj_box->size_.y);
- if (height_difference > constraints[CONSTRAINT_HEIGHT_DIFFERENCE])
- return false;
- }
- return true;
- }
- ObjectDataPtr ObjectDataBox::Interpolate(ObjectDataPtr obj,
- double fraction) const
- {
- ObjectDataBoxPtr other = std::static_pointer_cast<ObjectDataBox>(obj);
- size_t frame = (size_t) fabs(util::MyMath::Lerp(GetFrameIndex(),
- other->GetFrameIndex(),
- fraction));
- double x = util::MyMath::Lerp(GetPosition().x, other->GetPosition().x, fraction);
- double y = util::MyMath::Lerp(GetPosition().y, other->GetPosition().y, fraction);
- double w = util::MyMath::Lerp(size_.x, other->size_.x, fraction);
- double h = util::MyMath::Lerp(size_.y, other->size_.y, fraction);
- ObjectDataBoxPtr result(
- new ObjectDataBox(frame, cv::Point2d(x, y), cv::Point2d(w, h)));
- return result;
- }
- void ObjectDataBox::Visualize(cv::Mat& image, cv::Scalar& color, double alpha) const
- {
- cv::Point2d center(GetPosition().x * image.cols, GetPosition().y * image.rows);
- cv::Point2d size(size_.x * image.cols, size_.y * image.rows);
- cv::Point2d top_left = center - size * 0.5;
- cv::rectangle(image, top_left, top_left + size, color);
- }
- cv::Point2d ObjectDataBox::GetSize() const
- {
- return size_;
- }
- std::string ObjectDataBox::ToString(char delimiter) const
- {
- return ObjectData2D::ToString(delimiter) + delimiter +
- std::to_string(size_.x) + delimiter + std::to_string(size_.y);
- }
- }
|