test_canny.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #include "test_precomp.hpp"
  43. #ifdef HAVE_CUDA
  44. namespace opencv_test { namespace {
  45. ////////////////////////////////////////////////////////
  46. // Canny
  47. namespace
  48. {
  49. IMPLEMENT_PARAM_CLASS(AppertureSize, int)
  50. IMPLEMENT_PARAM_CLASS(L2gradient, bool)
  51. }
  52. PARAM_TEST_CASE(Canny, cv::cuda::DeviceInfo, AppertureSize, L2gradient, UseRoi)
  53. {
  54. cv::cuda::DeviceInfo devInfo;
  55. int apperture_size;
  56. bool useL2gradient;
  57. bool useRoi;
  58. virtual void SetUp()
  59. {
  60. devInfo = GET_PARAM(0);
  61. apperture_size = GET_PARAM(1);
  62. useL2gradient = GET_PARAM(2);
  63. useRoi = GET_PARAM(3);
  64. cv::cuda::setDevice(devInfo.deviceID());
  65. }
  66. };
  67. CUDA_TEST_P(Canny, Accuracy)
  68. {
  69. cv::Mat img = readImage("stereobm/aloe-L.png", cv::IMREAD_GRAYSCALE);
  70. ASSERT_FALSE(img.empty());
  71. double low_thresh = 50.0;
  72. double high_thresh = 100.0;
  73. cv::Ptr<cv::cuda::CannyEdgeDetector> canny = cv::cuda::createCannyEdgeDetector(low_thresh, high_thresh, apperture_size, useL2gradient);
  74. cv::cuda::GpuMat edges;
  75. canny->detect(loadMat(img, useRoi), edges);
  76. cv::Mat edges_gold;
  77. cv::Canny(img, edges_gold, low_thresh, high_thresh, apperture_size, useL2gradient);
  78. EXPECT_MAT_SIMILAR(edges_gold, edges, 2e-2);
  79. }
  80. class CannyAsyncParallelLoopBody : public cv::ParallelLoopBody
  81. {
  82. public:
  83. CannyAsyncParallelLoopBody(const cv::cuda::GpuMat& d_img_, cv::cuda::GpuMat* edges_, double low_thresh_, double high_thresh_, int apperture_size_, bool useL2gradient_)
  84. : d_img(d_img_), edges(edges_), low_thresh(low_thresh_), high_thresh(high_thresh_), apperture_size(apperture_size_), useL2gradient(useL2gradient_) {}
  85. ~CannyAsyncParallelLoopBody() {};
  86. void operator()(const cv::Range& r) const
  87. {
  88. for (int i = r.start; i < r.end; i++) {
  89. cv::cuda::Stream stream;
  90. cv::Ptr<cv::cuda::CannyEdgeDetector> canny = cv::cuda::createCannyEdgeDetector(low_thresh, high_thresh, apperture_size, useL2gradient);
  91. canny->detect(d_img, edges[i], stream);
  92. stream.waitForCompletion();
  93. }
  94. }
  95. protected:
  96. const cv::cuda::GpuMat& d_img;
  97. cv::cuda::GpuMat* edges;
  98. double low_thresh;
  99. double high_thresh;
  100. int apperture_size;
  101. bool useL2gradient;
  102. };
  103. #define NUM_STREAMS 128
  104. CUDA_TEST_P(Canny, Async)
  105. {
  106. if (!supportFeature(devInfo, cv::cuda::FEATURE_SET_COMPUTE_30))
  107. {
  108. throw SkipTestException("CUDA device doesn't support texture objects");
  109. }
  110. else
  111. {
  112. const cv::Mat img = readImage("stereobm/aloe-L.png", cv::IMREAD_GRAYSCALE);
  113. ASSERT_FALSE(img.empty());
  114. const cv::cuda::GpuMat d_img_roi = loadMat(img, useRoi);
  115. double low_thresh = 50.0;
  116. double high_thresh = 100.0;
  117. // Synchronous call
  118. cv::Ptr<cv::cuda::CannyEdgeDetector> canny = cv::cuda::createCannyEdgeDetector(low_thresh, high_thresh, apperture_size, useL2gradient);
  119. cv::cuda::GpuMat edges_gold;
  120. canny->detect(d_img_roi, edges_gold);
  121. // Asynchronous call
  122. cv::cuda::GpuMat edges[NUM_STREAMS];
  123. cv::parallel_for_(cv::Range(0, NUM_STREAMS), CannyAsyncParallelLoopBody(d_img_roi, edges, low_thresh, high_thresh, apperture_size, useL2gradient));
  124. // Compare the results of synchronous call and asynchronous call
  125. for (int i = 0; i < NUM_STREAMS; i++)
  126. EXPECT_MAT_NEAR(edges_gold, edges[i], 0.0);
  127. }
  128. }
  129. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, Canny, testing::Combine(
  130. ALL_DEVICES,
  131. testing::Values(AppertureSize(3), AppertureSize(5), AppertureSize(7)),
  132. testing::Values(L2gradient(false), L2gradient(true)),
  133. WHOLE_SUBMAT));
  134. }} // namespace
  135. #endif // HAVE_CUDA