test_mean_shift.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // MeanShift
  47. struct MeanShift : testing::TestWithParam<cv::cuda::DeviceInfo>
  48. {
  49. cv::cuda::DeviceInfo devInfo;
  50. cv::Mat img;
  51. int spatialRad;
  52. int colorRad;
  53. virtual void SetUp()
  54. {
  55. devInfo = GetParam();
  56. cv::cuda::setDevice(devInfo.deviceID());
  57. img = readImageType("meanshift/cones.png", CV_8UC4);
  58. ASSERT_FALSE(img.empty());
  59. spatialRad = 30;
  60. colorRad = 30;
  61. }
  62. };
  63. CUDA_TEST_P(MeanShift, Filtering)
  64. {
  65. cv::Mat img_template;
  66. if (supportFeature(devInfo, cv::cuda::FEATURE_SET_COMPUTE_20))
  67. img_template = readImage("meanshift/con_result.png");
  68. else
  69. img_template = readImage("meanshift/con_result_CC1X.png");
  70. ASSERT_FALSE(img_template.empty());
  71. cv::cuda::GpuMat d_dst;
  72. cv::cuda::meanShiftFiltering(loadMat(img), d_dst, spatialRad, colorRad);
  73. ASSERT_EQ(CV_8UC4, d_dst.type());
  74. cv::Mat dst(d_dst);
  75. cv::Mat result;
  76. cv::cvtColor(dst, result, cv::COLOR_BGRA2BGR);
  77. EXPECT_MAT_NEAR(img_template, result, 0.0);
  78. }
  79. CUDA_TEST_P(MeanShift, Proc)
  80. {
  81. cv::FileStorage fs;
  82. if (supportFeature(devInfo, cv::cuda::FEATURE_SET_COMPUTE_20))
  83. fs.open(std::string(cvtest::TS::ptr()->get_data_path()) + "meanshift/spmap.yaml", cv::FileStorage::READ);
  84. else
  85. fs.open(std::string(cvtest::TS::ptr()->get_data_path()) + "meanshift/spmap_CC1X.yaml", cv::FileStorage::READ);
  86. ASSERT_TRUE(fs.isOpened());
  87. cv::Mat spmap_template;
  88. fs["spmap"] >> spmap_template;
  89. ASSERT_FALSE(spmap_template.empty());
  90. cv::cuda::GpuMat rmap_filtered;
  91. cv::cuda::meanShiftFiltering(loadMat(img), rmap_filtered, spatialRad, colorRad);
  92. cv::cuda::GpuMat rmap;
  93. cv::cuda::GpuMat spmap;
  94. cv::cuda::meanShiftProc(loadMat(img), rmap, spmap, spatialRad, colorRad);
  95. ASSERT_EQ(CV_8UC4, rmap.type());
  96. EXPECT_MAT_NEAR(rmap_filtered, rmap, 0.0);
  97. EXPECT_MAT_NEAR(spmap_template, spmap, 0.0);
  98. }
  99. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, MeanShift, ALL_DEVICES);
  100. ////////////////////////////////////////////////////////////////////////////////
  101. // MeanShiftSegmentation
  102. namespace
  103. {
  104. IMPLEMENT_PARAM_CLASS(MinSize, int);
  105. }
  106. PARAM_TEST_CASE(MeanShiftSegmentation, cv::cuda::DeviceInfo, MinSize)
  107. {
  108. cv::cuda::DeviceInfo devInfo;
  109. int minsize;
  110. virtual void SetUp()
  111. {
  112. devInfo = GET_PARAM(0);
  113. minsize = GET_PARAM(1);
  114. cv::cuda::setDevice(devInfo.deviceID());
  115. }
  116. };
  117. CUDA_TEST_P(MeanShiftSegmentation, Regression)
  118. {
  119. cv::Mat img = readImageType("meanshift/cones.png", CV_8UC4);
  120. ASSERT_FALSE(img.empty());
  121. std::ostringstream path;
  122. path << "meanshift/cones_segmented_sp10_sr10_minsize" << minsize;
  123. if (supportFeature(devInfo, cv::cuda::FEATURE_SET_COMPUTE_20))
  124. path << ".png";
  125. else
  126. path << "_CC1X.png";
  127. cv::Mat dst_gold = readImage(path.str());
  128. ASSERT_FALSE(dst_gold.empty());
  129. cv::Mat dst;
  130. cv::cuda::meanShiftSegmentation(loadMat(img), dst, 10, 10, minsize);
  131. cv::Mat dst_rgb;
  132. cv::cvtColor(dst, dst_rgb, cv::COLOR_BGRA2BGR);
  133. EXPECT_MAT_SIMILAR(dst_gold, dst_rgb, 1e-3);
  134. }
  135. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, MeanShiftSegmentation, testing::Combine(
  136. ALL_DEVICES,
  137. testing::Values(MinSize(0), MinSize(4), MinSize(20), MinSize(84), MinSize(340), MinSize(1364))));
  138. }} // namespace
  139. #endif // HAVE_CUDA