swap_background.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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/utils/utils.h"
  17. #include "ultra_infer/vision/visualize/swap_background_arm.h"
  18. #include "ultra_infer/vision/visualize/visualize.h"
  19. namespace ultra_infer {
  20. namespace vision {
  21. static cv::Mat SwapBackgroundCommonCpu(const cv::Mat &im,
  22. const cv::Mat &background,
  23. const MattingResult &result,
  24. bool remove_small_connected_area) {
  25. FDASSERT((!im.empty()), "Image can't be empty!");
  26. FDASSERT((im.channels() == 3), "Only support 3 channels image mat!");
  27. FDASSERT((!background.empty()), "Background image can't be empty!");
  28. FDASSERT((background.channels() == 3),
  29. "Only support 3 channels background image mat!");
  30. auto vis_img = im.clone();
  31. auto background_copy = background.clone();
  32. int out_h = static_cast<int>(result.shape[0]);
  33. int out_w = static_cast<int>(result.shape[1]);
  34. int height = im.rows;
  35. int width = im.cols;
  36. int bg_height = background.rows;
  37. int bg_width = background.cols;
  38. std::vector<float> alpha_copy;
  39. alpha_copy.assign(result.alpha.begin(), result.alpha.end());
  40. float *alpha_ptr = static_cast<float *>(alpha_copy.data());
  41. cv::Mat alpha(out_h, out_w, CV_32FC1, alpha_ptr);
  42. if (remove_small_connected_area) {
  43. alpha = Visualize::RemoveSmallConnectedArea(alpha, 0.05f);
  44. }
  45. if ((vis_img).type() != CV_8UC3) {
  46. (vis_img).convertTo((vis_img), CV_8UC3);
  47. }
  48. if ((background_copy).type() != CV_8UC3) {
  49. (background_copy).convertTo((background_copy), CV_8UC3);
  50. }
  51. if ((bg_height != height) || (bg_width != width)) {
  52. cv::resize(background, background_copy, cv::Size(width, height));
  53. }
  54. if ((out_h != height) || (out_w != width)) {
  55. cv::resize(alpha, alpha, cv::Size(width, height));
  56. }
  57. uchar *vis_data = static_cast<uchar *>(vis_img.data);
  58. uchar *background_data = static_cast<uchar *>(background_copy.data);
  59. uchar *im_data = static_cast<uchar *>(im.data);
  60. float *alpha_data = reinterpret_cast<float *>(alpha.data);
  61. for (size_t i = 0; i < height; ++i) {
  62. for (size_t j = 0; j < width; ++j) {
  63. float alpha_val = alpha_data[i * width + j];
  64. for (size_t c = 0; c < 3; ++c) {
  65. vis_data[i * width * 3 + j * 3 + c] = cv::saturate_cast<uchar>(
  66. static_cast<float>(im_data[i * width * 3 + j * 3 + c]) * alpha_val +
  67. (1.f - alpha_val) * background_data[i * width * 3 + j * 3 + c]);
  68. }
  69. }
  70. }
  71. return vis_img;
  72. }
  73. static cv::Mat SwapBackgroundCommonCpu(const cv::Mat &im,
  74. const cv::Mat &background,
  75. const SegmentationResult &result,
  76. int background_label) {
  77. FDASSERT((!im.empty()), "Image can't be empty!");
  78. FDASSERT((im.channels() == 3), "Only support 3 channels image mat!");
  79. FDASSERT((!background.empty()), "Background image can't be empty!");
  80. FDASSERT((background.channels() == 3),
  81. "Only support 3 channels background image mat!");
  82. auto vis_img = im.clone();
  83. auto background_copy = background.clone();
  84. int height = im.rows;
  85. int width = im.cols;
  86. int bg_height = background.rows;
  87. int bg_width = background.cols;
  88. if ((vis_img).type() != CV_8UC3) {
  89. (vis_img).convertTo((vis_img), CV_8UC3);
  90. }
  91. if ((background_copy).type() != CV_8UC3) {
  92. (background_copy).convertTo((background_copy), CV_8UC3);
  93. }
  94. if ((bg_height != height) || (bg_width != width)) {
  95. cv::resize(background, background_copy, cv::Size(width, height));
  96. }
  97. uchar *vis_data = static_cast<uchar *>(vis_img.data);
  98. uchar *background_data = static_cast<uchar *>(background_copy.data);
  99. uchar *im_data = static_cast<uchar *>(im.data);
  100. float keep_value = 0.f;
  101. for (size_t i = 0; i < height; ++i) {
  102. for (size_t j = 0; j < width; ++j) {
  103. int category_id = result.label_map[i * width + j];
  104. if (background_label != category_id) {
  105. keep_value = 1.0f;
  106. } else {
  107. keep_value = 0.f;
  108. }
  109. for (size_t c = 0; c < 3; ++c) {
  110. vis_data[i * width * 3 + j * 3 + c] = cv::saturate_cast<uchar>(
  111. static_cast<float>(im_data[i * width * 3 + j * 3 + c]) *
  112. keep_value +
  113. (1.f - keep_value) * background_data[i * width * 3 + j * 3 + c]);
  114. }
  115. }
  116. }
  117. return vis_img;
  118. }
  119. // Public interfaces for SwapBackground.
  120. cv::Mat SwapBackground(const cv::Mat &im, const cv::Mat &background,
  121. const MattingResult &result,
  122. bool remove_small_connected_area) {
  123. // TODO: Support SSE/AVX on x86_64 platforms
  124. #ifdef __ARM_NEON
  125. return SwapBackgroundNEON(im, background, result,
  126. remove_small_connected_area);
  127. #else
  128. return SwapBackgroundCommonCpu(im, background, result,
  129. remove_small_connected_area);
  130. #endif
  131. }
  132. cv::Mat SwapBackground(const cv::Mat &im, const cv::Mat &background,
  133. const SegmentationResult &result, int background_label) {
  134. // TODO: Support SSE/AVX on x86_64 platforms
  135. #ifdef __ARM_NEON
  136. // return SwapBackgroundNEON(im, background, result, background_label);
  137. return SwapBackgroundNEON(im, background, result, background_label);
  138. #else
  139. return SwapBackgroundCommonCpu(im, background, result, background_label);
  140. #endif
  141. }
  142. // DEPRECATED
  143. cv::Mat Visualize::SwapBackgroundMatting(const cv::Mat &im,
  144. const cv::Mat &background,
  145. const MattingResult &result,
  146. bool remove_small_connected_area) {
  147. // TODO: Support SSE/AVX on x86_64 platforms
  148. #ifdef __ARM_NEON
  149. return SwapBackgroundNEON(im, background, result,
  150. remove_small_connected_area);
  151. #else
  152. return SwapBackgroundCommonCpu(im, background, result,
  153. remove_small_connected_area);
  154. #endif
  155. }
  156. cv::Mat Visualize::SwapBackgroundSegmentation(
  157. const cv::Mat &im, const cv::Mat &background, int background_label,
  158. const SegmentationResult &result) {
  159. // TODO: Support SSE/AVX on x86_64 platforms
  160. #ifdef __ARM_NEON
  161. return SwapBackgroundNEON(im, background, result, background_label);
  162. #else
  163. return SwapBackgroundCommonCpu(im, background, result, background_label);
  164. #endif
  165. }
  166. } // namespace vision
  167. } // namespace ultra_infer