gapi_scalar_tests.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 <iostream>
  8. namespace opencv_test
  9. {
  10. TEST(GAPI_Scalar, Argument)
  11. {
  12. cv::Size sz(2, 2);
  13. cv::Mat in_mat(sz, CV_8U);
  14. cv::randn(in_mat, cv::Scalar::all(127), cv::Scalar::all(40.f));
  15. cv::GComputationT<cv::GMat (cv::GMat, cv::GScalar)> mulS([](cv::GMat in, cv::GScalar c)
  16. {
  17. return in*c;
  18. });
  19. cv::Mat out_mat(sz, CV_8U);
  20. mulS.apply(in_mat, cv::Scalar(2), out_mat);
  21. cv::Mat reference = in_mat*2;
  22. EXPECT_EQ(0, cvtest::norm(out_mat, reference, NORM_INF));
  23. }
  24. TEST(GAPI_Scalar, ReturnValue)
  25. {
  26. const cv::Size sz(2, 2);
  27. cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));
  28. cv::GComputationT<cv::GScalar (cv::GMat)> sum_of_sum([](cv::GMat in)
  29. {
  30. return cv::gapi::sum(in + in);
  31. });
  32. cv::Scalar out;
  33. sum_of_sum.apply(in_mat, out);
  34. EXPECT_EQ(8, out[0]);
  35. }
  36. TEST(GAPI_Scalar, TmpScalar)
  37. {
  38. const cv::Size sz(2, 2);
  39. cv::Mat in_mat(sz, CV_8U, cv::Scalar(1));
  40. cv::GComputationT<cv::GMat (cv::GMat)> mul_by_sum([](cv::GMat in)
  41. {
  42. return in * cv::gapi::sum(in);
  43. });
  44. cv::Mat out_mat(sz, CV_8U);
  45. mul_by_sum.apply(in_mat, out_mat);
  46. cv::Mat reference = cv::Mat(sz, CV_8U, cv::Scalar(4));
  47. EXPECT_EQ(0, cvtest::norm(out_mat, reference, NORM_INF));
  48. }
  49. TEST(GAPI_ScalarWithValue, Simple_Arithmetic_Pipeline)
  50. {
  51. GMat in;
  52. GMat out = (in + 1) * 2;
  53. cv::GComputation comp(in, out);
  54. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);
  55. cv::Mat ref_mat, out_mat;
  56. ref_mat = (in_mat + 1) * 2;
  57. comp.apply(in_mat, out_mat);
  58. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  59. }
  60. TEST(GAPI_ScalarWithValue, GScalar_Initilization)
  61. {
  62. cv::Scalar sc(2);
  63. cv::GMat in;
  64. cv::GScalar s(sc);
  65. cv::GComputation comp(in, cv::gapi::mulC(in, s));
  66. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);
  67. cv::Mat ref_mat, out_mat;
  68. cv::multiply(in_mat, sc, ref_mat, 1, CV_8UC1);
  69. comp.apply(cv::gin(in_mat), cv::gout(out_mat));
  70. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  71. }
  72. TEST(GAPI_ScalarWithValue, Constant_GScalar_In_Middle_Graph)
  73. {
  74. cv::Scalar sc(5);
  75. cv::GMat in1;
  76. cv::GScalar in2;
  77. cv::GScalar s(sc);
  78. auto add_out = cv::gapi::addC(in1, in2);
  79. cv::GComputation comp(cv::GIn(in1, in2), cv::GOut(cv::gapi::mulC(add_out, s)));
  80. cv::Mat in_mat = cv::Mat::eye(3, 3, CV_8UC1);
  81. cv::Scalar in_scalar(3);
  82. cv::Mat ref_mat, out_mat, add_mat;
  83. cv::add(in_mat, in_scalar, add_mat);
  84. cv::multiply(add_mat, sc, ref_mat, 1, CV_8UC1);
  85. comp.apply(cv::gin(in_mat, in_scalar), cv::gout(out_mat));
  86. EXPECT_EQ(0, cvtest::norm(out_mat, ref_mat, NORM_INF));
  87. }
  88. } // namespace opencv_test