|
| 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 | + |
| 15 | +#include "fastdeploy/vision/common/processors/limit_short.h" |
| 16 | + |
| 17 | +namespace fastdeploy { |
| 18 | +namespace vision { |
| 19 | + |
| 20 | +bool LimitShort::CpuRun(Mat* mat) { |
| 21 | + cv::Mat* im = mat->GetCpuMat(); |
| 22 | + int origin_w = im->cols; |
| 23 | + int origin_h = im->rows; |
| 24 | + int im_size_min = std::min(origin_w, origin_h); |
| 25 | + int target = im_size_min; |
| 26 | + if (max_short_ > 0 && im_size_min > max_short_) { |
| 27 | + target = max_short_; |
| 28 | + } else if (min_short_ > 0 && im_size_min < min_short_) { |
| 29 | + target = min_short_; |
| 30 | + } |
| 31 | + if (target != im_size_min) { |
| 32 | + double scale = |
| 33 | + static_cast<double>(target) / static_cast<double>(im_size_min); |
| 34 | + cv::resize(*im, *im, cv::Size(), scale, scale, interp_); |
| 35 | + mat->SetWidth(im->cols); |
| 36 | + mat->SetHeight(im->rows); |
| 37 | + } |
| 38 | + return true; |
| 39 | +} |
| 40 | + |
| 41 | +#ifdef ENABLE_OPENCV_CUDA |
| 42 | +bool LimitShort::GpuRun(Mat* mat) { |
| 43 | + cv::cuda::GpuMat* im = mat->GetGpuMat(); |
| 44 | + int origin_w = im->cols; |
| 45 | + int origin_h = im->rows; |
| 46 | + im->convertTo(*im, CV_32FC(im->channels())); |
| 47 | + int im_size_min = std::min(origin_w, origin_h); |
| 48 | + int target = im_size_min; |
| 49 | + if (max_short_ > 0 && im_size_min > max_short_) { |
| 50 | + target = max_short_; |
| 51 | + } else if (min_short_ > 0 && im_size_min < min_short_) { |
| 52 | + target = min_short_; |
| 53 | + } |
| 54 | + if (target != im_size_min) { |
| 55 | + double scale = |
| 56 | + static_cast<double>(target) / static_cast<double>(im_size_min); |
| 57 | + cv::cuda::resize(*im, *im, cv::Size(), scale, scale, interp_); |
| 58 | + mat->SetWidth(im->cols); |
| 59 | + mat->SetHeight(im->rows); |
| 60 | + } |
| 61 | + return true; |
| 62 | +} |
| 63 | +#endif |
| 64 | + |
| 65 | +bool LimitShort::Run(Mat* mat, int max_short, int min_short, ProcLib lib) { |
| 66 | + auto l = LimitShort(max_short, min_short); |
| 67 | + return l(mat, lib); |
| 68 | +} |
| 69 | +} // namespace vision |
| 70 | +} // namespace fastdeploy |
0 commit comments