visualize.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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/visualize/visualize.h"
  15. namespace ultra_infer {
  16. namespace vision {
  17. static std::vector<int> global_fd_vis_color_map = std::vector<int>();
  18. std::vector<int> GenerateColorMap(int num_classes) {
  19. if (num_classes < 10) {
  20. num_classes = 10;
  21. }
  22. std::vector<int> color_map(num_classes * 3, 0);
  23. for (int i = 0; i < num_classes; ++i) {
  24. int j = 0;
  25. int lab = i;
  26. while (lab) {
  27. color_map[i * 3] |= (((lab >> 0) & 1) << (7 - j));
  28. color_map[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j));
  29. color_map[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j));
  30. ++j;
  31. lab >>= 3;
  32. }
  33. }
  34. return color_map;
  35. }
  36. // This class will deprecated, please not use it
  37. int Visualize::num_classes_ = 0;
  38. std::vector<int> Visualize::color_map_ = std::vector<int>();
  39. const std::vector<int> &Visualize::GetColorMap(int num_classes) {
  40. if (num_classes < num_classes_) {
  41. return color_map_;
  42. }
  43. num_classes_ = num_classes;
  44. std::vector<int>().swap(color_map_);
  45. color_map_.resize(3 * num_classes_, 0);
  46. for (int i = 0; i < num_classes_; ++i) {
  47. int j = 0;
  48. int lab = i;
  49. while (lab) {
  50. color_map_[i * 3] |= (((lab >> 0) & 1) << (7 - j));
  51. color_map_[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j));
  52. color_map_[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j));
  53. ++j;
  54. lab >>= 3;
  55. }
  56. }
  57. return color_map_;
  58. }
  59. } // namespace vision
  60. } // namespace ultra_infer