visualize.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright (c) 2021 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 "model_deploy/utils/include/visualize.h"
  15. namespace PaddleDeploy {
  16. bool GenerateColorMap(const int &num_classes,
  17. std::vector<int> *color_map) {
  18. *color_map = std::vector<int>(3 * num_classes, 0);
  19. for (int i = 0; i < num_classes; ++i) {
  20. int j = 0;
  21. int lab = i;
  22. while (lab) {
  23. (*color_map)[i * 3] |= (((lab >> 0) & 1) << (7 - j));
  24. (*color_map)[i * 3 + 1] |= (((lab >> 1) & 1) << (7 - j));
  25. (*color_map)[i * 3 + 2] |= (((lab >> 2) & 1) << (7 - j));
  26. ++j;
  27. lab >>= 3;
  28. }
  29. }
  30. return true;
  31. }
  32. bool Visualize(const cv::Mat& img,
  33. const DetResult& result,
  34. cv::Mat* vis_img,
  35. const int& num_classes) {
  36. std::vector<int> color_map;
  37. GenerateColorMap(num_classes + 2, &color_map);
  38. *vis_img = img.clone();
  39. std::vector<Box> boxes = result.boxes;
  40. for (auto i = 0; i < boxes.size(); ++i) {
  41. cv::Rect roi = cv::Rect(boxes[i].coordinate[0],
  42. boxes[i].coordinate[1],
  43. boxes[i].coordinate[2],
  44. boxes[i].coordinate[3]);
  45. // draw box and title
  46. std::string text = boxes[i].category;
  47. int category_id = boxes[i].category_id + 2;
  48. int c1 = color_map[3 * category_id + 0];
  49. int c2 = color_map[3 * category_id + 1];
  50. int c3 = color_map[3 * category_id + 2];
  51. cv::Scalar roi_color = cv::Scalar(c1, c2, c3);
  52. text += std::to_string(static_cast<int>(boxes[i].score * 100)) + "%";
  53. int font_face = cv::FONT_HERSHEY_SIMPLEX;
  54. double font_scale = 0.5f;
  55. float thickness = 0.5;
  56. cv::Size text_size =
  57. cv::getTextSize(text, font_face, font_scale, thickness, nullptr);
  58. cv::Point origin;
  59. origin.x = roi.x;
  60. origin.y = roi.y;
  61. // background
  62. cv::Rect text_back = cv::Rect(boxes[i].coordinate[0],
  63. boxes[i].coordinate[1] - text_size.height,
  64. text_size.width,
  65. text_size.height);
  66. // draw
  67. cv::rectangle(*vis_img, roi, roi_color, 2);
  68. cv::rectangle(*vis_img, text_back, roi_color, -1);
  69. cv::putText(*vis_img,
  70. text,
  71. origin,
  72. font_face,
  73. font_scale,
  74. cv::Scalar(255, 255, 255),
  75. thickness);
  76. // mask
  77. if (boxes[i].mask.data.size() == 0) {
  78. continue;
  79. }
  80. cv::Mat full_mask(boxes[i].mask.shape[0],
  81. boxes[i].mask.shape[1],
  82. CV_8UC1,
  83. boxes[i].mask.data.data());
  84. cv::Mat mask_ch[3];
  85. mask_ch[0] = full_mask * c1;
  86. mask_ch[1] = full_mask * c2;
  87. mask_ch[2] = full_mask * c3;
  88. cv::Mat mask;
  89. cv::merge(mask_ch, 3, mask);
  90. cv::addWeighted(*vis_img, 1, mask, 0.5, 0, *vis_img);
  91. }
  92. return true;
  93. }
  94. bool Visualize(const cv::Mat& img,
  95. const SegResult& result,
  96. cv::Mat* vis_img,
  97. const int& num_classes) {
  98. std::vector<int> color_map;
  99. GenerateColorMap(num_classes, &color_map);
  100. std::vector<uint8_t> label_map(result.label_map.data.begin(),
  101. result.label_map.data.end());
  102. cv::Mat mask(result.label_map.shape[0],
  103. result.label_map.shape[1],
  104. CV_8UC1,
  105. label_map.data());
  106. *vis_img = cv::Mat::zeros(
  107. result.label_map.shape[0], result.label_map.shape[1], CV_8UC3);
  108. int rows = img.rows;
  109. int cols = img.cols;
  110. for (int i = 0; i < rows; i++) {
  111. for (int j = 0; j < cols; j++) {
  112. int category_id = static_cast<int>(mask.at<uchar>(i, j));
  113. vis_img->at<cv::Vec3b>(i, j)[0] = color_map[3 * category_id + 0];
  114. vis_img->at<cv::Vec3b>(i, j)[1] = color_map[3 * category_id + 1];
  115. vis_img->at<cv::Vec3b>(i, j)[2] = color_map[3 * category_id + 2];
  116. }
  117. }
  118. cv::addWeighted(img, .5, *vis_img, .5, 0, *vis_img);
  119. return true;
  120. }
  121. } // namespace PaddleDeploy