Skip to content

Commit

Permalink
Project import generated by Copybara.
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 5cd50ad1abca6bd5d52832975bb60e6c0fc60a27
  • Loading branch information
MediaPipe Team authored and chuoling committed Aug 13, 2020
1 parent f57ff46 commit 73f4475
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
27 changes: 22 additions & 5 deletions mediapipe/calculators/image/image_cropping_calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ ::mediapipe::Status ImageCroppingCalculator::Open(CalculatorContext* cc) {
}

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)
Expand Down Expand Up @@ -234,20 +238,27 @@ ::mediapipe::Status ImageCroppingCalculator::RenderCpu(CalculatorContext* cc) {
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);

Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions mediapipe/calculators/image/image_cropping_calculator.h
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -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_;
Expand Down
6 changes: 6 additions & 0 deletions mediapipe/calculators/image/image_cropping_calculator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ node {
options: {
[mediapipe.ImageCroppingCalculatorOptions.ext] {
border_mode: BORDER_REPLICATE
output_max_width: 256
output_max_height: 256
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ node {
options: {
[mediapipe.ImageCroppingCalculatorOptions.ext] {
border_mode: BORDER_REPLICATE
output_max_width: 256
output_max_height: 256
}
}
}
Expand Down

0 comments on commit 73f4475

Please sign in to comment.