center_crop.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/vision/common/processors/center_crop.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. bool CenterCrop::ImplByOpenCV(FDMat *mat) {
  18. cv::Mat *im = mat->GetOpenCVMat();
  19. int height = static_cast<int>(im->rows);
  20. int width = static_cast<int>(im->cols);
  21. if (height < height_ || width < width_) {
  22. FDERROR << "[CenterCrop] Image size less than crop size" << std::endl;
  23. return false;
  24. }
  25. int offset_x = static_cast<int>((width - width_) / 2);
  26. int offset_y = static_cast<int>((height - height_) / 2);
  27. cv::Rect crop_roi(offset_x, offset_y, width_, height_);
  28. cv::Mat new_im = (*im)(crop_roi).clone();
  29. mat->SetMat(new_im);
  30. mat->SetWidth(width_);
  31. mat->SetHeight(height_);
  32. return true;
  33. }
  34. #ifdef ENABLE_FLYCV
  35. bool CenterCrop::ImplByFlyCV(FDMat *mat) {
  36. fcv::Mat *im = mat->GetFlyCVMat();
  37. int height = static_cast<int>(im->height());
  38. int width = static_cast<int>(im->width());
  39. if (height < height_ || width < width_) {
  40. FDERROR << "[CenterCrop] Image size less than crop size" << std::endl;
  41. return false;
  42. }
  43. int offset_x = static_cast<int>((width - width_) / 2);
  44. int offset_y = static_cast<int>((height - height_) / 2);
  45. fcv::Rect crop_roi(offset_x, offset_y, width_, height_);
  46. fcv::Mat new_im;
  47. fcv::crop(*im, new_im, crop_roi);
  48. mat->SetMat(new_im);
  49. mat->SetWidth(width_);
  50. mat->SetHeight(height_);
  51. return true;
  52. }
  53. #endif
  54. #ifdef ENABLE_CVCUDA
  55. bool CenterCrop::ImplByCvCuda(FDMat *mat) {
  56. // Prepare input tensor
  57. FDTensor *src = CreateCachedGpuInputTensor(mat);
  58. auto src_tensor = CreateCvCudaTensorWrapData(*src);
  59. // Prepare output tensor
  60. mat->output_cache->Resize({height_, width_, mat->Channels()}, src->Dtype(),
  61. "output_cache", Device::GPU);
  62. auto dst_tensor = CreateCvCudaTensorWrapData(*(mat->output_cache));
  63. int offset_x = static_cast<int>((mat->Width() - width_) / 2);
  64. int offset_y = static_cast<int>((mat->Height() - height_) / 2);
  65. NVCVRectI crop_roi = {offset_x, offset_y, width_, height_};
  66. cvcuda_crop_op_(mat->Stream(), *src_tensor, *dst_tensor, crop_roi);
  67. mat->SetTensor(mat->output_cache);
  68. mat->SetWidth(width_);
  69. mat->SetHeight(height_);
  70. mat->device = Device::GPU;
  71. mat->mat_type = ProcLib::CVCUDA;
  72. return true;
  73. }
  74. bool CenterCrop::ImplByCvCuda(FDMatBatch *mat_batch) {
  75. for (size_t i = 0; i < mat_batch->mats->size(); ++i) {
  76. if (ImplByCvCuda(&((*(mat_batch->mats))[i])) != true) {
  77. return false;
  78. }
  79. }
  80. mat_batch->device = Device::GPU;
  81. mat_batch->mat_type = ProcLib::CVCUDA;
  82. return true;
  83. }
  84. #endif
  85. bool CenterCrop::Run(FDMat *mat, const int &width, const int &height,
  86. ProcLib lib) {
  87. auto c = CenterCrop(width, height);
  88. return c(mat, lib);
  89. }
  90. } // namespace vision
  91. } // namespace ultra_infer