segmentation.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 "opencv2/highgui.hpp"
  15. #include "opencv2/imgproc/imgproc.hpp"
  16. #include "ultra_infer/vision/visualize/segmentation_arm.h"
  17. #include "ultra_infer/vision/visualize/visualize.h"
  18. namespace ultra_infer {
  19. namespace vision {
  20. static cv::Mat VisSegmentationCommonCpu(const cv::Mat &im,
  21. const SegmentationResult &result,
  22. float weight) {
  23. // Use the native c++ version without any optimization.
  24. auto color_map = GenerateColorMap(1000);
  25. int64_t height = result.shape[0];
  26. int64_t width = result.shape[1];
  27. auto vis_img = cv::Mat(height, width, CV_8UC3);
  28. int64_t index = 0;
  29. for (int i = 0; i < height; i++) {
  30. for (int j = 0; j < width; j++) {
  31. int category_id = result.label_map[index++];
  32. if (category_id == 0) {
  33. vis_img.at<cv::Vec3b>(i, j)[0] = im.at<cv::Vec3b>(i, j)[0];
  34. vis_img.at<cv::Vec3b>(i, j)[1] = im.at<cv::Vec3b>(i, j)[1];
  35. vis_img.at<cv::Vec3b>(i, j)[2] = im.at<cv::Vec3b>(i, j)[2];
  36. } else {
  37. vis_img.at<cv::Vec3b>(i, j)[0] = color_map[3 * category_id + 0];
  38. vis_img.at<cv::Vec3b>(i, j)[1] = color_map[3 * category_id + 1];
  39. vis_img.at<cv::Vec3b>(i, j)[2] = color_map[3 * category_id + 2];
  40. }
  41. }
  42. }
  43. cv::addWeighted(im, 1.0 - weight, vis_img, weight, 0, vis_img);
  44. return vis_img;
  45. }
  46. cv::Mat VisSegmentation(const cv::Mat &im, const SegmentationResult &result,
  47. float weight) {
  48. // TODO: Support SSE/AVX on x86_64 platforms
  49. #ifdef __ARM_NEON
  50. return VisSegmentationNEON(im, result, weight, true);
  51. #else
  52. return VisSegmentationCommonCpu(im, result, weight);
  53. #endif
  54. }
  55. cv::Mat Visualize::VisSegmentation(const cv::Mat &im,
  56. const SegmentationResult &result) {
  57. FDWARNING << "DEPRECATED: ultra_infer::vision::Visualize::VisSegmentation is "
  58. "deprecated, please use ultra_infer::vision:VisSegmentation "
  59. "function instead."
  60. << std::endl;
  61. #ifdef __ARM_NEON
  62. return VisSegmentationNEON(im, result, 0.5f, true);
  63. #else
  64. return VisSegmentationCommonCpu(im, result, 0.5f);
  65. #endif
  66. }
  67. } // namespace vision
  68. } // namespace ultra_infer