Project import generated by Copybara.

GitOrigin-RevId: 5cd50ad1abca6bd5d52832975bb60e6c0fc60a27
This commit is contained in:
MediaPipe Team 2020-08-13 04:01:00 -04:00 committed by chuoling
parent f57ff46845
commit 73f4475c17
5 changed files with 36 additions and 5 deletions

View File

@ -127,6 +127,10 @@ REGISTER_CALCULATOR(ImageCroppingCalculator);
}
options_ = cc->Options<mediapipe::ImageCroppingCalculatorOptions>();
output_max_width_ =
options_.has_output_max_width() ? options_.output_max_width() : FLT_MAX;
output_max_height_ =
options_.has_output_max_height() ? options_.output_max_height() : FLT_MAX;
if (use_gpu_) {
#if !defined(MEDIAPIPE_DISABLE_GPU)
@ -234,20 +238,27 @@ REGISTER_CALCULATOR(ImageCroppingCalculator);
cv::Mat src_points;
cv::boxPoints(min_rect, src_points);
float output_width = min_rect.size.width;
float output_height = min_rect.size.height;
float scale = std::min({1.0f, output_max_width_ / output_width,
output_max_height_ / output_height});
output_width *= scale;
output_height *= scale;
float dst_corners[8] = {0,
min_rect.size.height - 1,
output_height - 1,
0,
0,
min_rect.size.width - 1,
output_width - 1,
0,
min_rect.size.width - 1,
min_rect.size.height - 1};
output_width - 1,
output_height - 1};
cv::Mat dst_points = cv::Mat(4, 2, CV_32F, dst_corners);
cv::Mat projection_matrix =
cv::getPerspectiveTransform(src_points, dst_points);
cv::Mat cropped_image;
cv::warpPerspective(input_mat, cropped_image, projection_matrix,
cv::Size(min_rect.size.width, min_rect.size.height),
cv::Size(output_width, output_height),
/* flags = */ 0,
/* borderMode = */ border_mode);
@ -439,6 +450,12 @@ void ImageCroppingCalculator::GetOutputDimensions(CalculatorContext* cc,
int width = static_cast<int>(std::round((col_max - col_min) * src_width));
int height = static_cast<int>(std::round((row_max - row_min) * src_height));
float scale =
std::min({1.0f, output_max_width_ / width, output_max_height_ / height});
width *= scale;
height *= scale;
// Minimum output dimension 1x1 prevents creation of textures with 0x0.
*dst_width = std::max(1, width);
*dst_height = std::max(1, height);

View File

@ -1,6 +1,8 @@
#ifndef MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_CROPPING_CALCULATOR_H_
#define MEDIAPIPE_CALCULATORS_IMAGE_IMAGE_CROPPING_CALCULATOR_H_
#include <float.h>
#include "mediapipe/calculators/image/image_cropping_calculator.pb.h"
#include "mediapipe/framework/calculator_framework.h"
@ -80,6 +82,8 @@ class ImageCroppingCalculator : public CalculatorBase {
bool use_gpu_ = false;
// Output texture corners (4) after transoformation in normalized coordinates.
float transformed_points_[8];
float output_max_width_ = FLT_MAX;
float output_max_height_ = FLT_MAX;
#if !defined(MEDIAPIPE_DISABLE_GPU)
bool gpu_initialized_ = false;
mediapipe::GlCalculatorHelper gpu_helper_;

View File

@ -51,4 +51,10 @@ message ImageCroppingCalculatorOptions {
// Specifies behaviour for crops that go beyond image borders.
optional BorderMode border_mode = 8 [default = BORDER_ZERO];
// Specifies limits for the size of the output image. It will be scaled down,
// preserving ratio, to fit within. These do not change which area of the
// input is selected for cropping.
optional int32 output_max_width = 9;
optional int32 output_max_height = 10;
}

View File

@ -66,6 +66,8 @@ node {
options: {
[mediapipe.ImageCroppingCalculatorOptions.ext] {
border_mode: BORDER_REPLICATE
output_max_width: 256
output_max_height: 256
}
}
}

View File

@ -66,6 +66,8 @@ node {
options: {
[mediapipe.ImageCroppingCalculatorOptions.ext] {
border_mode: BORDER_REPLICATE
output_max_width: 256
output_max_height: 256
}
}
}