[ImageTransformationCalculator]Add option to specify padding color

PiperOrigin-RevId: 510541473
This commit is contained in:
MediaPipe Team 2023-02-17 15:55:24 -08:00 committed by Copybara-Service
parent 37a825c98d
commit 68ba9a6ebf
2 changed files with 17 additions and 1 deletions

View File

@ -207,6 +207,7 @@ class ImageTransformationCalculator : public CalculatorBase {
bool flip_vertically_ = false; bool flip_vertically_ = false;
bool use_gpu_ = false; bool use_gpu_ = false;
cv::Scalar padding_color_;
#if !MEDIAPIPE_DISABLE_GPU #if !MEDIAPIPE_DISABLE_GPU
GlCalculatorHelper gpu_helper_; GlCalculatorHelper gpu_helper_;
std::unique_ptr<QuadRenderer> rgb_renderer_; std::unique_ptr<QuadRenderer> rgb_renderer_;
@ -338,6 +339,9 @@ absl::Status ImageTransformationCalculator::Open(CalculatorContext* cc) {
} }
scale_mode_ = ParseScaleMode(options_.scale_mode(), DEFAULT_SCALE_MODE); scale_mode_ = ParseScaleMode(options_.scale_mode(), DEFAULT_SCALE_MODE);
padding_color_ = cv::Scalar(options_.padding_color().red(),
options_.padding_color().green(),
options_.padding_color().blue());
if (use_gpu_) { if (use_gpu_) {
#if !MEDIAPIPE_DISABLE_GPU #if !MEDIAPIPE_DISABLE_GPU
@ -480,7 +484,8 @@ absl::Status ImageTransformationCalculator::RenderCpu(CalculatorContext* cc) {
cv::copyMakeBorder(intermediate_mat, scaled_mat, top, bottom, left, cv::copyMakeBorder(intermediate_mat, scaled_mat, top, bottom, left,
right, right,
options_.constant_padding() ? cv::BORDER_CONSTANT options_.constant_padding() ? cv::BORDER_CONSTANT
: cv::BORDER_REPLICATE); : cv::BORDER_REPLICATE,
padding_color_);
} else { } else {
cv::resize(input_mat, scaled_mat, cv::Size(target_width, target_height), cv::resize(input_mat, scaled_mat, cv::Size(target_width, target_height),
0, 0, scale_flag); 0, 0, scale_flag);

View File

@ -25,6 +25,13 @@ message ImageTransformationCalculatorOptions {
optional ImageTransformationCalculatorOptions ext = 251952830; optional ImageTransformationCalculatorOptions ext = 251952830;
} }
// RGB values in range of 0 - 255
message Color {
optional int32 red = 1 [default = 0];
optional int32 green = 2 [default = 0];
optional int32 blue = 3 [default = 0];
}
// Output dimensions. Set to 0 if they should be the same as the input. // Output dimensions. Set to 0 if they should be the same as the input.
optional int32 output_width = 1 [default = 0]; optional int32 output_width = 1 [default = 0];
optional int32 output_height = 2 [default = 0]; optional int32 output_height = 2 [default = 0];
@ -40,4 +47,8 @@ message ImageTransformationCalculatorOptions {
// Default is to use BORDER_CONSTANT. If set to false, it will use // Default is to use BORDER_CONSTANT. If set to false, it will use
// BORDER_REPLICATE instead. // BORDER_REPLICATE instead.
optional bool constant_padding = 7 [default = true]; optional bool constant_padding = 7 [default = true];
// The color for the padding. This option is only used when the scale mode is
// FIT. Default is black. This is for CPU only.
optional Color padding_color = 8;
} }