Program Listing for File center_crop.hpp

Return to documentation for file (/workspace/amdinfer/src/amdinfer/pre_post/center_crop.hpp)

// Copyright 2022 Xilinx, Inc.
// Copyright 2022 Advanced Micro Devices, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef GUARD_AMDINFER_PRE_POST_CENTER_CROP
#define GUARD_AMDINFER_PRE_POST_CENTER_CROP

#include <opencv2/core.hpp>  // for cv::Mat

namespace amdinfer::pre_post {

inline cv::Mat centerCrop(cv::Mat img, int height, int width) {
  const int offset_width = (img.cols - width) / 2;
  const int offset_height = (img.rows - height) / 2;
  const cv::Rect roi(offset_width, offset_height, width, height);
  img = img(roi);
  return img;
}

}  // namespace amdinfer::pre_post

#endif  // GUARD_AMDINFER_PRE_POST_CENTER_CROP