test_histogram.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. // HistEven
  47. PARAM_TEST_CASE(HistEven, cv::cuda::DeviceInfo, cv::Size)
  48. {
  49. cv::cuda::DeviceInfo devInfo;
  50. cv::Size size;
  51. virtual void SetUp()
  52. {
  53. devInfo = GET_PARAM(0);
  54. size = GET_PARAM(1);
  55. cv::cuda::setDevice(devInfo.deviceID());
  56. }
  57. };
  58. CUDA_TEST_P(HistEven, Accuracy)
  59. {
  60. cv::Mat src = randomMat(size, CV_8UC1);
  61. int hbins = 30;
  62. float hranges[] = {50.0f, 200.0f};
  63. cv::cuda::GpuMat hist;
  64. cv::cuda::histEven(loadMat(src), hist, hbins, (int) hranges[0], (int) hranges[1]);
  65. cv::Mat hist_gold;
  66. int histSize[] = {hbins};
  67. const float* ranges[] = {hranges};
  68. int channels[] = {0};
  69. cv::calcHist(&src, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);
  70. hist_gold = hist_gold.t();
  71. hist_gold.convertTo(hist_gold, CV_32S);
  72. EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
  73. }
  74. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, HistEven, testing::Combine(
  75. ALL_DEVICES,
  76. DIFFERENT_SIZES));
  77. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  78. // CalcHist
  79. PARAM_TEST_CASE(CalcHist, cv::cuda::DeviceInfo, cv::Size)
  80. {
  81. cv::cuda::DeviceInfo devInfo;
  82. cv::Size size;
  83. virtual void SetUp()
  84. {
  85. devInfo = GET_PARAM(0);
  86. size = GET_PARAM(1);
  87. cv::cuda::setDevice(devInfo.deviceID());
  88. }
  89. };
  90. CUDA_TEST_P(CalcHist, Accuracy)
  91. {
  92. cv::Mat src = randomMat(size, CV_8UC1);
  93. cv::cuda::GpuMat hist;
  94. cv::cuda::calcHist(loadMat(src), hist);
  95. cv::Mat hist_gold;
  96. const int hbins = 256;
  97. const float hranges[] = {0.0f, 256.0f};
  98. const int histSize[] = {hbins};
  99. const float* ranges[] = {hranges};
  100. const int channels[] = {0};
  101. cv::calcHist(&src, 1, channels, cv::Mat(), hist_gold, 1, histSize, ranges);
  102. hist_gold = hist_gold.reshape(1, 1);
  103. hist_gold.convertTo(hist_gold, CV_32S);
  104. EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
  105. }
  106. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, CalcHist, testing::Combine(
  107. ALL_DEVICES,
  108. DIFFERENT_SIZES));
  109. PARAM_TEST_CASE(CalcHistWithMask, cv::cuda::DeviceInfo, cv::Size)
  110. {
  111. cv::cuda::DeviceInfo devInfo;
  112. cv::Size size;
  113. virtual void SetUp()
  114. {
  115. devInfo = GET_PARAM(0);
  116. size = GET_PARAM(1);
  117. cv::cuda::setDevice(devInfo.deviceID());
  118. }
  119. };
  120. CUDA_TEST_P(CalcHistWithMask, Accuracy)
  121. {
  122. cv::Mat src = randomMat(size, CV_8UC1);
  123. cv::Mat mask = randomMat(size, CV_8UC1);
  124. cv::Mat(mask, cv::Rect(0, 0, size.width / 2, size.height / 2)).setTo(0);
  125. cv::cuda::GpuMat hist;
  126. cv::cuda::calcHist(loadMat(src), loadMat(mask), hist);
  127. cv::Mat hist_gold;
  128. const int hbins = 256;
  129. const float hranges[] = {0.0f, 256.0f};
  130. const int histSize[] = {hbins};
  131. const float* ranges[] = {hranges};
  132. const int channels[] = {0};
  133. cv::calcHist(&src, 1, channels, mask, hist_gold, 1, histSize, ranges);
  134. hist_gold = hist_gold.reshape(1, 1);
  135. hist_gold.convertTo(hist_gold, CV_32S);
  136. EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
  137. }
  138. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, CalcHistWithMask, testing::Combine(
  139. ALL_DEVICES,
  140. DIFFERENT_SIZES));
  141. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  142. // EqualizeHist
  143. PARAM_TEST_CASE(EqualizeHist, cv::cuda::DeviceInfo, cv::Size)
  144. {
  145. cv::cuda::DeviceInfo devInfo;
  146. cv::Size size;
  147. virtual void SetUp()
  148. {
  149. devInfo = GET_PARAM(0);
  150. size = GET_PARAM(1);
  151. cv::cuda::setDevice(devInfo.deviceID());
  152. }
  153. };
  154. CUDA_TEST_P(EqualizeHist, Async)
  155. {
  156. cv::Mat src = randomMat(size, CV_8UC1);
  157. cv::cuda::Stream stream;
  158. cv::cuda::GpuMat dst;
  159. cv::cuda::equalizeHist(loadMat(src), dst, stream);
  160. stream.waitForCompletion();
  161. cv::Mat dst_gold;
  162. cv::equalizeHist(src, dst_gold);
  163. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  164. }
  165. CUDA_TEST_P(EqualizeHist, Accuracy)
  166. {
  167. cv::Mat src = randomMat(size, CV_8UC1);
  168. cv::cuda::GpuMat dst;
  169. cv::cuda::equalizeHist(loadMat(src), dst);
  170. cv::Mat dst_gold;
  171. cv::equalizeHist(src, dst_gold);
  172. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  173. }
  174. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, EqualizeHist, testing::Combine(
  175. ALL_DEVICES,
  176. DIFFERENT_SIZES));
  177. TEST(EqualizeHistIssue, Issue18035)
  178. {
  179. std::vector<std::string> imgPaths;
  180. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/3MP.png");
  181. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/5MP.png");
  182. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/airplane.png");
  183. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/baboon.png");
  184. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/box.png");
  185. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/box_in_scene.png");
  186. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/fruits.png");
  187. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/fruits_ecc.png");
  188. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/graffiti.png");
  189. imgPaths.push_back(std::string(cvtest::TS::ptr()->get_data_path()) + "../cv/shared/lena.png");
  190. for (size_t i = 0; i < imgPaths.size(); ++i)
  191. {
  192. std::string imgPath = imgPaths[i];
  193. cv::Mat src = cv::imread(imgPath, cv::IMREAD_GRAYSCALE);
  194. src = src / 30;
  195. cv::cuda::GpuMat d_src, dst;
  196. d_src.upload(src);
  197. cv::cuda::equalizeHist(d_src, dst);
  198. cv::Mat dst_gold;
  199. cv::equalizeHist(src, dst_gold);
  200. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  201. }
  202. }
  203. PARAM_TEST_CASE(EqualizeHistExtreme, cv::cuda::DeviceInfo, cv::Size, int)
  204. {
  205. cv::cuda::DeviceInfo devInfo;
  206. cv::Size size;
  207. int val;
  208. virtual void SetUp()
  209. {
  210. devInfo = GET_PARAM(0);
  211. size = GET_PARAM(1);
  212. val = GET_PARAM(2);
  213. cv::cuda::setDevice(devInfo.deviceID());
  214. }
  215. };
  216. CUDA_TEST_P(EqualizeHistExtreme, Case1)
  217. {
  218. cv::Mat src(size, CV_8UC1, val);
  219. cv::cuda::GpuMat dst;
  220. cv::cuda::equalizeHist(loadMat(src), dst);
  221. cv::Mat dst_gold;
  222. cv::equalizeHist(src, dst_gold);
  223. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  224. }
  225. CUDA_TEST_P(EqualizeHistExtreme, Case2)
  226. {
  227. cv::Mat src = randomMat(size, CV_8UC1, val);
  228. cv::cuda::GpuMat dst;
  229. cv::cuda::equalizeHist(loadMat(src), dst);
  230. cv::Mat dst_gold;
  231. cv::equalizeHist(src, dst_gold);
  232. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  233. }
  234. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, EqualizeHistExtreme, testing::Combine(
  235. ALL_DEVICES,
  236. DIFFERENT_SIZES,
  237. testing::Range(0, 256)));
  238. ///////////////////////////////////////////////////////////////////////////////////////////////////////
  239. // CLAHE
  240. namespace
  241. {
  242. IMPLEMENT_PARAM_CLASS(ClipLimit, double)
  243. }
  244. PARAM_TEST_CASE(CLAHE, cv::cuda::DeviceInfo, cv::Size, ClipLimit, MatType)
  245. {
  246. cv::cuda::DeviceInfo devInfo;
  247. cv::Size size;
  248. double clipLimit;
  249. int type;
  250. virtual void SetUp()
  251. {
  252. devInfo = GET_PARAM(0);
  253. size = GET_PARAM(1);
  254. clipLimit = GET_PARAM(2);
  255. type = GET_PARAM(3);
  256. cv::cuda::setDevice(devInfo.deviceID());
  257. }
  258. };
  259. CUDA_TEST_P(CLAHE, Accuracy)
  260. {
  261. cv::Mat src;
  262. if (type == CV_8UC1)
  263. src = randomMat(size, type);
  264. else if (type == CV_16UC1)
  265. src = randomMat(size, type, 0, 65535);
  266. cv::Ptr<cv::cuda::CLAHE> clahe = cv::cuda::createCLAHE(clipLimit);
  267. cv::cuda::GpuMat dst;
  268. clahe->apply(loadMat(src), dst);
  269. cv::Ptr<cv::CLAHE> clahe_gold = cv::createCLAHE(clipLimit);
  270. cv::Mat dst_gold;
  271. clahe_gold->apply(src, dst_gold);
  272. ASSERT_MAT_NEAR(dst_gold, dst, 1.0);
  273. }
  274. INSTANTIATE_TEST_CASE_P(CUDA_ImgProc, CLAHE, testing::Combine(
  275. ALL_DEVICES,
  276. DIFFERENT_SIZES,
  277. testing::Values(0.0, 5.0, 10.0, 20.0, 40.0),
  278. testing::Values(MatType(CV_8UC1), MatType(CV_16UC1))));
  279. }} // namespace
  280. #endif // HAVE_CUDA