gapi_render_tests.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2019 Intel Corporation
  6. #include "../test_precomp.hpp"
  7. #include "gapi_render_tests.hpp"
  8. namespace opencv_test
  9. {
  10. cv::Scalar cvtBGRToYUVC(const cv::Scalar& bgr)
  11. {
  12. double y = bgr[2] * 0.299000 + bgr[1] * 0.587000 + bgr[0] * 0.114000;
  13. double u = bgr[2] * -0.168736 + bgr[1] * -0.331264 + bgr[0] * 0.500000 + 128;
  14. double v = bgr[2] * 0.500000 + bgr[1] * -0.418688 + bgr[0] * -0.081312 + 128;
  15. return {y, u, v};
  16. }
  17. void drawMosaicRef(const cv::Mat& mat, const cv::Rect &rect, int cellSz)
  18. {
  19. cv::Rect mat_rect(0, 0, mat.cols, mat.rows);
  20. auto intersection = mat_rect & rect;
  21. cv::Mat msc_roi = mat(intersection);
  22. bool has_crop_x = false;
  23. bool has_crop_y = false;
  24. int cols = msc_roi.cols;
  25. int rows = msc_roi.rows;
  26. if (msc_roi.cols % cellSz != 0)
  27. {
  28. has_crop_x = true;
  29. cols -= msc_roi.cols % cellSz;
  30. }
  31. if (msc_roi.rows % cellSz != 0)
  32. {
  33. has_crop_y = true;
  34. rows -= msc_roi.rows % cellSz;
  35. }
  36. cv::Mat cell_roi;
  37. for(int i = 0; i < rows; i += cellSz )
  38. {
  39. for(int j = 0; j < cols; j += cellSz)
  40. {
  41. cell_roi = msc_roi(cv::Rect(j, i, cellSz, cellSz));
  42. cell_roi = cv::mean(cell_roi);
  43. }
  44. if (has_crop_x)
  45. {
  46. cell_roi = msc_roi(cv::Rect(cols, i, msc_roi.cols - cols, cellSz));
  47. cell_roi = cv::mean(cell_roi);
  48. }
  49. }
  50. if (has_crop_y)
  51. {
  52. for(int j = 0; j < cols; j += cellSz)
  53. {
  54. cell_roi = msc_roi(cv::Rect(j, rows, cellSz, msc_roi.rows - rows));
  55. cell_roi = cv::mean(cell_roi);
  56. }
  57. if (has_crop_x)
  58. {
  59. cell_roi = msc_roi(cv::Rect(cols, rows, msc_roi.cols - cols, msc_roi.rows - rows));
  60. cell_roi = cv::mean(cell_roi);
  61. }
  62. }
  63. }
  64. void blendImageRef(cv::Mat& mat, const cv::Point& org, const cv::Mat& img, const cv::Mat& alpha)
  65. {
  66. auto roi = mat(cv::Rect(org, img.size()));
  67. cv::Mat img32f_w;
  68. cv::merge(std::vector<cv::Mat>(3, alpha), img32f_w);
  69. cv::Mat roi32f_w(roi.size(), CV_32FC3, cv::Scalar::all(1.0));
  70. roi32f_w -= img32f_w;
  71. cv::Mat img32f, roi32f;
  72. img.convertTo(img32f, CV_32F, 1.0/255);
  73. roi.convertTo(roi32f, CV_32F, 1.0/255);
  74. cv::multiply(img32f, img32f_w, img32f);
  75. cv::multiply(roi32f, roi32f_w, roi32f);
  76. roi32f += img32f;
  77. roi32f.convertTo(roi, CV_8U, 255.0);
  78. };
  79. } // namespace opencv_test