crop.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/crop.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. bool Crop::ImplByOpenCV(Mat *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_ + offset_h_ || width < width_ + offset_w_) {
  22. FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
  23. << "] from the input image [" << height << ", " << width
  24. << "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
  25. << std::endl;
  26. return false;
  27. }
  28. cv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
  29. cv::Mat new_im = (*im)(crop_roi).clone();
  30. mat->SetMat(new_im);
  31. mat->SetWidth(width_);
  32. mat->SetHeight(height_);
  33. return true;
  34. }
  35. #ifdef ENABLE_FLYCV
  36. bool Crop::ImplByFlyCV(Mat *mat) {
  37. fcv::Mat *im = mat->GetFlyCVMat();
  38. int height = static_cast<int>(im->height());
  39. int width = static_cast<int>(im->width());
  40. if (height < height_ + offset_h_ || width < width_ + offset_w_) {
  41. FDERROR << "[Crop] Cannot crop [" << height_ << ", " << width_
  42. << "] from the input image [" << height << ", " << width
  43. << "], with offset [" << offset_h_ << ", " << offset_w_ << "]."
  44. << std::endl;
  45. return false;
  46. }
  47. fcv::Rect crop_roi(offset_w_, offset_h_, width_, height_);
  48. fcv::Mat new_im;
  49. fcv::crop(*im, new_im, crop_roi);
  50. mat->SetMat(new_im);
  51. mat->SetWidth(width_);
  52. mat->SetHeight(height_);
  53. return true;
  54. }
  55. #endif
  56. bool Crop::Run(Mat *mat, int offset_w, int offset_h, int width, int height,
  57. ProcLib lib) {
  58. auto c = Crop(offset_w, offset_h, width, height);
  59. return c(mat, lib);
  60. }
  61. } // namespace vision
  62. } // namespace ultra_infer