From 6ab85e4a168501ce6e532e3f54dc6cb411dd3205 Mon Sep 17 00:00:00 2001 From: yanghaku <1961882079@qq.com> Date: Sat, 15 Apr 2023 14:56:01 +0800 Subject: [PATCH] fix landmark_projection_calculator's projection function using normalized input when the rect's bound is out of image Signed-off-by: yanghaku <1961882079@qq.com> --- .../calculators/util/landmark_projection_calculator.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mediapipe/calculators/util/landmark_projection_calculator.cc b/mediapipe/calculators/util/landmark_projection_calculator.cc index 9f276da56..9647b0efc 100644 --- a/mediapipe/calculators/util/landmark_projection_calculator.cc +++ b/mediapipe/calculators/util/landmark_projection_calculator.cc @@ -168,8 +168,6 @@ class LandmarkProjectionCalculator : public CalculatorBase { cc->Options(); project_fn = [&input_rect, &options](const NormalizedLandmark& landmark, NormalizedLandmark* new_landmark) { - // TODO: fix projection or deprecate (current projection - // calculations are incorrect for general case). const float x = landmark.x() - 0.5f; const float y = landmark.y() - 0.5f; const float angle = @@ -177,8 +175,12 @@ class LandmarkProjectionCalculator : public CalculatorBase { float new_x = std::cos(angle) * x - std::sin(angle) * y; float new_y = std::sin(angle) * x + std::cos(angle) * y; - new_x = new_x * input_rect.width() + input_rect.x_center(); - new_y = new_y * input_rect.height() + input_rect.y_center(); + // input_rect's x_min and y_min may be smaller than 0, so limit the lower bound with 0 + const float rect_x_min = std::max(0.f, input_rect.x_center() - input_rect.width() * 0.5f); + const float rect_y_min = std::max(0.f, input_rect.y_center() - input_rect.height() * 0.5f); + + new_x = (new_x + 0.5f) * input_rect.width() + rect_x_min; + new_y = (new_y + 0.5f) * input_rect.height() + rect_y_min; const float new_z = landmark.z() * input_rect.width(); // Scale Z coordinate as X.