gapi_fluid_roi_test.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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) 2018 Intel Corporation
  6. #include "test_precomp.hpp"
  7. #include "gapi_fluid_test_kernels.hpp"
  8. namespace opencv_test
  9. {
  10. using namespace cv::gapi_test_kernels;
  11. struct PartialComputation : public TestWithParam <std::tuple<cv::Rect>> {};
  12. TEST_P(PartialComputation, Test)
  13. {
  14. cv::Rect roi;
  15. std::tie(roi) = GetParam();
  16. int borderType = BORDER_REPLICATE;
  17. int kernelSize = 3;
  18. cv::Point anchor = {-1, -1};
  19. cv::GMat in;
  20. cv::GMat out = TBlur3x3::on(in, borderType, {});
  21. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  22. const auto sz = cv::Size(8, 10);
  23. cv::Mat in_mat(sz, CV_8UC1);
  24. cv::Scalar mean = cv::Scalar(127.0f);
  25. cv::Scalar stddev = cv::Scalar(40.f);
  26. cv::randn(in_mat, mean, stddev);
  27. cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);
  28. cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);
  29. // Run G-API
  30. auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{roi}}));
  31. cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
  32. // Check with OpenCV
  33. if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};
  34. cv::blur(in_mat(roi), out_mat_ocv(roi), {kernelSize, kernelSize}, anchor, borderType);
  35. EXPECT_EQ(0, cvtest::norm(out_mat_gapi, out_mat_ocv, NORM_INF));
  36. }
  37. INSTANTIATE_TEST_CASE_P(Fluid, PartialComputation,
  38. Values(cv::Rect{}, cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},
  39. cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));
  40. struct PartialComputationAddC : public TestWithParam <std::tuple<cv::Rect>> {};
  41. TEST_P(PartialComputationAddC, Test)
  42. {
  43. cv::Rect roi;
  44. std::tie(roi) = GetParam();
  45. cv::GMat in;
  46. cv::GMat out = TAddCSimple::on(in, 1);
  47. cv::GComputation c(cv::GIn(in), cv::GOut(out));
  48. const auto sz = cv::Size(8, 10);
  49. cv::Mat in_mat(sz, CV_8UC1);
  50. cv::Scalar mean = cv::Scalar(127.0f);
  51. cv::Scalar stddev = cv::Scalar(40.f);
  52. cv::randn(in_mat, mean, stddev);
  53. cv::Mat out_mat_gapi = cv::Mat::zeros(sz, CV_8UC1);
  54. cv::Mat out_mat_ocv = cv::Mat::zeros(sz, CV_8UC1);
  55. // Run G-API
  56. auto cc = c.compile(cv::descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{roi}}));
  57. cc(cv::gin(in_mat), cv::gout(out_mat_gapi));
  58. // Check with OpenCV
  59. if (roi == cv::Rect{}) roi = cv::Rect{0,0,sz.width,sz.height};
  60. out_mat_ocv(roi) = in_mat(roi) + 1;
  61. EXPECT_EQ(0, cvtest::norm(out_mat_gapi, out_mat_ocv, NORM_INF));
  62. }
  63. INSTANTIATE_TEST_CASE_P(FluidRoi, PartialComputationAddC,
  64. Values(cv::Rect{}, cv::Rect{0,0,8,6}, cv::Rect{0,1,8,3},
  65. cv::Rect{0,2,8,3}, cv::Rect{0,3,8,5}, cv::Rect{0,4,8,6}));
  66. struct SequenceOfBlursRoiTest : public TestWithParam <std::tuple<int, cv::Rect>> {};
  67. TEST_P(SequenceOfBlursRoiTest, Test)
  68. {
  69. cv::Size sz_in = { 320, 240 };
  70. int borderType = 0;
  71. cv::Rect roi;
  72. std::tie(borderType, roi) = GetParam();
  73. cv::Mat in_mat(sz_in, CV_8UC1);
  74. cv::Scalar mean = cv::Scalar(127.0f);
  75. cv::Scalar stddev = cv::Scalar(40.f);
  76. cv::randn(in_mat, mean, stddev);
  77. cv::Point anchor = {-1, -1};
  78. cv::Scalar borderValue(0);
  79. GMat in;
  80. auto mid = TBlur3x3::on(in, borderType, borderValue);
  81. auto out = TBlur5x5::on(mid, borderType, borderValue);
  82. Mat out_mat_gapi = Mat::zeros(sz_in, CV_8UC1);
  83. GComputation c(GIn(in), GOut(out));
  84. auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{roi}}));
  85. cc(gin(in_mat), gout(out_mat_gapi));
  86. cv::Mat mid_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
  87. cv::Mat out_mat_ocv = Mat::zeros(sz_in, CV_8UC1);
  88. cv::blur(in_mat, mid_mat_ocv, {3,3}, anchor, borderType);
  89. if (roi == cv::Rect{})
  90. {
  91. roi = cv::Rect{0, 0, sz_in.width, sz_in.height};
  92. }
  93. cv::blur(mid_mat_ocv(roi), out_mat_ocv(roi), {5,5}, anchor, borderType);
  94. EXPECT_EQ(0, cvtest::norm(out_mat_ocv, out_mat_gapi, NORM_INF));
  95. }
  96. INSTANTIATE_TEST_CASE_P(FluidRoi, SequenceOfBlursRoiTest,
  97. Combine(Values(BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT_101),
  98. Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));
  99. struct TwoBlursRoiTest : public TestWithParam <std::tuple<int, int, int, int, int, int, bool, cv::Rect>> {};
  100. TEST_P(TwoBlursRoiTest, Test)
  101. {
  102. cv::Size sz_in = { 320, 240 };
  103. int kernelSize1 = 0, kernelSize2 = 0;
  104. int borderType1 = -1, borderType2 = -1;
  105. cv::Scalar borderValue1{}, borderValue2{};
  106. bool readFromInput = false;
  107. cv::Rect outRoi;
  108. std::tie(kernelSize1, borderType1, borderValue1, kernelSize2, borderType2, borderValue2, readFromInput, outRoi) = GetParam();
  109. cv::Mat in_mat(sz_in, CV_8UC1);
  110. cv::Scalar mean = cv::Scalar(127.0f);
  111. cv::Scalar stddev = cv::Scalar(40.f);
  112. cv::randn(in_mat, mean, stddev);
  113. cv::Point anchor = {-1, -1};
  114. auto blur1 = kernelSize1 == 3 ? &TBlur3x3::on : TBlur5x5::on;
  115. auto blur2 = kernelSize2 == 3 ? &TBlur3x3::on : TBlur5x5::on;
  116. GMat in, out1, out2;
  117. if (readFromInput)
  118. {
  119. out1 = blur1(in, borderType1, borderValue1);
  120. out2 = blur2(in, borderType2, borderValue2);
  121. }
  122. else
  123. {
  124. auto mid = TAddCSimple::on(in, 0);
  125. out1 = blur1(mid, borderType1, borderValue1);
  126. out2 = blur2(mid, borderType2, borderValue2);
  127. }
  128. Mat out_mat_gapi1 = Mat::zeros(sz_in, CV_8UC1);
  129. Mat out_mat_gapi2 = Mat::zeros(sz_in, CV_8UC1);
  130. GComputation c(GIn(in), GOut(out1, out2));
  131. auto cc = c.compile(descr_of(in_mat), cv::compile_args(fluidTestPackage, GFluidOutputRois{{outRoi, outRoi}}));
  132. cc(gin(in_mat), gout(out_mat_gapi1, out_mat_gapi2));
  133. cv::Mat out_mat_ocv1 = Mat::zeros(sz_in, CV_8UC1);
  134. cv::Mat out_mat_ocv2 = Mat::zeros(sz_in, CV_8UC1);
  135. cv::blur(in_mat(outRoi), out_mat_ocv1(outRoi), {kernelSize1, kernelSize1}, anchor, borderType1);
  136. cv::blur(in_mat(outRoi), out_mat_ocv2(outRoi), {kernelSize2, kernelSize2}, anchor, borderType2);
  137. EXPECT_EQ(0, cvtest::norm(out_mat_ocv1, out_mat_gapi1, NORM_INF));
  138. EXPECT_EQ(0, cvtest::norm(out_mat_ocv2, out_mat_gapi2, NORM_INF));
  139. }
  140. INSTANTIATE_TEST_CASE_P(FluidRoi, TwoBlursRoiTest,
  141. Combine(Values(3, 5),
  142. Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
  143. Values(0),
  144. Values(3, 5),
  145. Values(cv::BORDER_CONSTANT, cv::BORDER_REPLICATE, cv::BORDER_REFLECT_101),
  146. Values(0),
  147. testing::Bool(), // Read from input directly or place a copy node at start
  148. Values(cv::Rect{0,0,320,240}, cv::Rect{0,64,320,128}, cv::Rect{0,128,320,112})));
  149. } // namespace opencv_test