cls_process.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright (c) 2020 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 "cls_process.h" //NOLINT
  15. #include <algorithm>
  16. #include <memory>
  17. #include <string>
  18. const std::vector<int> rec_image_shape{3, 48, 192};
  19. cv::Mat ClsResizeImg(cv::Mat img) {
  20. int imgC, imgH, imgW;
  21. imgC = rec_image_shape[0];
  22. imgH = rec_image_shape[1];
  23. imgW = rec_image_shape[2];
  24. float ratio = static_cast<float>(img.cols) / static_cast<float>(img.rows);
  25. int resize_w, resize_h;
  26. if (ceilf(imgH * ratio) > imgW)
  27. resize_w = imgW;
  28. else
  29. resize_w = int(ceilf(imgH * ratio));
  30. cv::Mat resize_img;
  31. cv::resize(img, resize_img, cv::Size(resize_w, imgH), 0.f, 0.f,
  32. cv::INTER_LINEAR);
  33. if (resize_w < imgW) {
  34. cv::copyMakeBorder(resize_img, resize_img, 0, 0, 0, imgW - resize_w,
  35. cv::BORDER_CONSTANT, cv::Scalar(0, 0, 0));
  36. }
  37. return resize_img;
  38. }