test_core.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. // Merge
  47. PARAM_TEST_CASE(Merge, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels, UseRoi)
  48. {
  49. cv::cuda::DeviceInfo devInfo;
  50. cv::Size size;
  51. int depth;
  52. int channels;
  53. bool useRoi;
  54. virtual void SetUp()
  55. {
  56. devInfo = GET_PARAM(0);
  57. size = GET_PARAM(1);
  58. depth = GET_PARAM(2);
  59. channels = GET_PARAM(3);
  60. useRoi = GET_PARAM(4);
  61. cv::cuda::setDevice(devInfo.deviceID());
  62. }
  63. };
  64. CUDA_TEST_P(Merge, Accuracy)
  65. {
  66. std::vector<cv::Mat> src;
  67. src.reserve(channels);
  68. for (int i = 0; i < channels; ++i)
  69. src.push_back(cv::Mat(size, depth, cv::Scalar::all(i)));
  70. std::vector<cv::cuda::GpuMat> d_src;
  71. for (int i = 0; i < channels; ++i)
  72. d_src.push_back(loadMat(src[i], useRoi));
  73. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  74. {
  75. try
  76. {
  77. cv::cuda::GpuMat dst;
  78. cv::cuda::merge(d_src, dst);
  79. }
  80. catch (const cv::Exception& e)
  81. {
  82. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  83. }
  84. }
  85. else
  86. {
  87. cv::cuda::GpuMat dst;
  88. cv::cuda::merge(d_src, dst);
  89. cv::Mat dst_gold;
  90. cv::merge(src, dst_gold);
  91. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  92. }
  93. }
  94. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Merge, testing::Combine(
  95. ALL_DEVICES,
  96. DIFFERENT_SIZES,
  97. ALL_DEPTH,
  98. testing::Values(1, 2, 3, 4),
  99. WHOLE_SUBMAT));
  100. ////////////////////////////////////////////////////////////////////////////////
  101. // Split
  102. PARAM_TEST_CASE(Split, cv::cuda::DeviceInfo, cv::Size, MatDepth, Channels, UseRoi)
  103. {
  104. cv::cuda::DeviceInfo devInfo;
  105. cv::Size size;
  106. int depth;
  107. int channels;
  108. bool useRoi;
  109. int type;
  110. virtual void SetUp()
  111. {
  112. devInfo = GET_PARAM(0);
  113. size = GET_PARAM(1);
  114. depth = GET_PARAM(2);
  115. channels = GET_PARAM(3);
  116. useRoi = GET_PARAM(4);
  117. cv::cuda::setDevice(devInfo.deviceID());
  118. type = CV_MAKE_TYPE(depth, channels);
  119. }
  120. };
  121. CUDA_TEST_P(Split, Accuracy)
  122. {
  123. cv::Mat src = randomMat(size, type);
  124. if (depth == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  125. {
  126. try
  127. {
  128. std::vector<cv::cuda::GpuMat> dst;
  129. cv::cuda::split(loadMat(src), dst);
  130. }
  131. catch (const cv::Exception& e)
  132. {
  133. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  134. }
  135. }
  136. else
  137. {
  138. std::vector<cv::cuda::GpuMat> dst;
  139. cv::cuda::split(loadMat(src, useRoi), dst);
  140. std::vector<cv::Mat> dst_gold;
  141. cv::split(src, dst_gold);
  142. ASSERT_EQ(dst_gold.size(), dst.size());
  143. for (size_t i = 0; i < dst_gold.size(); ++i)
  144. {
  145. EXPECT_MAT_NEAR(dst_gold[i], dst[i], 0.0);
  146. }
  147. }
  148. }
  149. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Split, testing::Combine(
  150. ALL_DEVICES,
  151. DIFFERENT_SIZES,
  152. ALL_DEPTH,
  153. testing::Values(1, 2, 3, 4),
  154. WHOLE_SUBMAT));
  155. ////////////////////////////////////////////////////////////////////////////////
  156. // Transpose
  157. PARAM_TEST_CASE(Transpose, cv::cuda::DeviceInfo, cv::Size, MatType, UseRoi)
  158. {
  159. cv::cuda::DeviceInfo devInfo;
  160. cv::Size size;
  161. int type;
  162. bool useRoi;
  163. virtual void SetUp()
  164. {
  165. devInfo = GET_PARAM(0);
  166. size = GET_PARAM(1);
  167. type = GET_PARAM(2);
  168. useRoi = GET_PARAM(3);
  169. cv::cuda::setDevice(devInfo.deviceID());
  170. }
  171. };
  172. CUDA_TEST_P(Transpose, Accuracy)
  173. {
  174. cv::Mat src = randomMat(size, type);
  175. if (CV_MAT_DEPTH(type) == CV_64F && !supportFeature(devInfo, cv::cuda::NATIVE_DOUBLE))
  176. {
  177. try
  178. {
  179. cv::cuda::GpuMat dst;
  180. cv::cuda::transpose(loadMat(src), dst);
  181. }
  182. catch (const cv::Exception& e)
  183. {
  184. ASSERT_EQ(cv::Error::StsUnsupportedFormat, e.code);
  185. }
  186. }
  187. else
  188. {
  189. cv::cuda::GpuMat dst = createMat(cv::Size(size.height, size.width), type, useRoi);
  190. cv::cuda::transpose(loadMat(src, useRoi), dst);
  191. cv::Mat dst_gold;
  192. cv::transpose(src, dst_gold);
  193. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  194. }
  195. }
  196. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Transpose, testing::Combine(
  197. ALL_DEVICES,
  198. DIFFERENT_SIZES,
  199. testing::Values(MatType(CV_8UC1),
  200. MatType(CV_8UC4),
  201. MatType(CV_16UC2),
  202. MatType(CV_16SC2),
  203. MatType(CV_32SC1),
  204. MatType(CV_32SC2),
  205. MatType(CV_64FC1)),
  206. WHOLE_SUBMAT));
  207. ////////////////////////////////////////////////////////////////////////////////
  208. // Flip
  209. enum {FLIP_BOTH = 0, FLIP_X = 1, FLIP_Y = -1};
  210. CV_ENUM(FlipCode, FLIP_BOTH, FLIP_X, FLIP_Y)
  211. #define ALL_FLIP_CODES testing::Values(FlipCode(FLIP_BOTH), FlipCode(FLIP_X), FlipCode(FLIP_Y))
  212. PARAM_TEST_CASE(Flip, cv::cuda::DeviceInfo, cv::Size, MatType, FlipCode, UseRoi)
  213. {
  214. cv::cuda::DeviceInfo devInfo;
  215. cv::Size size;
  216. int type;
  217. int flip_code;
  218. bool useRoi;
  219. virtual void SetUp()
  220. {
  221. devInfo = GET_PARAM(0);
  222. size = GET_PARAM(1);
  223. type = GET_PARAM(2);
  224. flip_code = GET_PARAM(3);
  225. useRoi = GET_PARAM(4);
  226. cv::cuda::setDevice(devInfo.deviceID());
  227. }
  228. };
  229. CUDA_TEST_P(Flip, Accuracy)
  230. {
  231. cv::Mat src = randomMat(size, type);
  232. cv::cuda::GpuMat dst = createMat(size, type, useRoi);
  233. cv::cuda::flip(loadMat(src, useRoi), dst, flip_code);
  234. cv::Mat dst_gold;
  235. cv::flip(src, dst_gold, flip_code);
  236. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  237. }
  238. CUDA_TEST_P(Flip, AccuracyInplace)
  239. {
  240. cv::Mat src = randomMat(size, type);
  241. bool isSizeOdd = ((size.width & 1) == 1) || ((size.height & 1) == 1);
  242. cv::cuda::GpuMat srcDst = loadMat(src, useRoi);
  243. if(isSizeOdd)
  244. {
  245. EXPECT_THROW(cv::cuda::flip(srcDst, srcDst, flip_code), cv::Exception);
  246. return;
  247. }
  248. cv::cuda::flip(srcDst, srcDst, flip_code);
  249. cv::Mat dst_gold;
  250. cv::flip(src, dst_gold, flip_code);
  251. EXPECT_MAT_NEAR(dst_gold, srcDst, 0.0);
  252. }
  253. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, Flip, testing::Combine(
  254. ALL_DEVICES,
  255. DIFFERENT_SIZES,
  256. testing::Values(MatType(CV_8UC1),
  257. MatType(CV_8UC3),
  258. MatType(CV_8UC4),
  259. MatType(CV_16UC1),
  260. MatType(CV_16UC3),
  261. MatType(CV_16UC4),
  262. MatType(CV_32SC1),
  263. MatType(CV_32SC3),
  264. MatType(CV_32SC4),
  265. MatType(CV_32FC1),
  266. MatType(CV_32FC3),
  267. MatType(CV_32FC4)),
  268. ALL_FLIP_CODES,
  269. WHOLE_SUBMAT));
  270. ////////////////////////////////////////////////////////////////////////////////
  271. // LUT
  272. PARAM_TEST_CASE(LUT, cv::cuda::DeviceInfo, cv::Size, MatType, UseRoi)
  273. {
  274. cv::cuda::DeviceInfo devInfo;
  275. cv::Size size;
  276. int type;
  277. bool useRoi;
  278. virtual void SetUp()
  279. {
  280. devInfo = GET_PARAM(0);
  281. size = GET_PARAM(1);
  282. type = GET_PARAM(2);
  283. useRoi = GET_PARAM(3);
  284. cv::cuda::setDevice(devInfo.deviceID());
  285. }
  286. };
  287. CUDA_TEST_P(LUT, OneChannel)
  288. {
  289. cv::Mat src = randomMat(size, type);
  290. cv::Mat lut = randomMat(cv::Size(256, 1), CV_8UC1);
  291. cv::Ptr<cv::cuda::LookUpTable> lutAlg = cv::cuda::createLookUpTable(lut);
  292. cv::cuda::GpuMat dst = createMat(size, CV_MAKE_TYPE(lut.depth(), src.channels()));
  293. lutAlg->transform(loadMat(src, useRoi), dst);
  294. cv::Mat dst_gold;
  295. cv::LUT(src, lut, dst_gold);
  296. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  297. }
  298. CUDA_TEST_P(LUT, MultiChannel)
  299. {
  300. cv::Mat src = randomMat(size, type);
  301. cv::Mat lut = randomMat(cv::Size(256, 1), CV_MAKE_TYPE(CV_8U, src.channels()));
  302. cv::Ptr<cv::cuda::LookUpTable> lutAlg = cv::cuda::createLookUpTable(lut);
  303. cv::cuda::GpuMat dst = createMat(size, CV_MAKE_TYPE(lut.depth(), src.channels()), useRoi);
  304. lutAlg->transform(loadMat(src, useRoi), dst);
  305. cv::Mat dst_gold;
  306. cv::LUT(src, lut, dst_gold);
  307. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  308. }
  309. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, LUT, testing::Combine(
  310. ALL_DEVICES,
  311. DIFFERENT_SIZES,
  312. testing::Values(MatType(CV_8UC1), MatType(CV_8UC3)),
  313. WHOLE_SUBMAT));
  314. //////////////////////////////////////////////////////////////////////////////
  315. // CopyMakeBorder
  316. namespace
  317. {
  318. IMPLEMENT_PARAM_CLASS(Border, int)
  319. }
  320. PARAM_TEST_CASE(CopyMakeBorder, cv::cuda::DeviceInfo, cv::Size, MatType, Border, BorderType, UseRoi)
  321. {
  322. cv::cuda::DeviceInfo devInfo;
  323. cv::Size size;
  324. int type;
  325. int border;
  326. int borderType;
  327. bool useRoi;
  328. virtual void SetUp()
  329. {
  330. devInfo = GET_PARAM(0);
  331. size = GET_PARAM(1);
  332. type = GET_PARAM(2);
  333. border = GET_PARAM(3);
  334. borderType = GET_PARAM(4);
  335. useRoi = GET_PARAM(5);
  336. cv::cuda::setDevice(devInfo.deviceID());
  337. }
  338. };
  339. CUDA_TEST_P(CopyMakeBorder, Accuracy)
  340. {
  341. cv::Mat src = randomMat(size, type);
  342. cv::Scalar val = randomScalar(0, 255);
  343. cv::cuda::GpuMat dst = createMat(cv::Size(size.width + 2 * border, size.height + 2 * border), type, useRoi);
  344. cv::cuda::copyMakeBorder(loadMat(src, useRoi), dst, border, border, border, border, borderType, val);
  345. cv::Mat dst_gold;
  346. cv::copyMakeBorder(src, dst_gold, border, border, border, border, borderType, val);
  347. EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
  348. }
  349. INSTANTIATE_TEST_CASE_P(CUDA_Arithm, CopyMakeBorder, testing::Combine(
  350. ALL_DEVICES,
  351. DIFFERENT_SIZES,
  352. testing::Values(MatType(CV_8UC1),
  353. MatType(CV_8UC3),
  354. MatType(CV_8UC4),
  355. MatType(CV_16UC1),
  356. MatType(CV_16UC3),
  357. MatType(CV_16UC4),
  358. MatType(CV_32FC1),
  359. MatType(CV_32FC3),
  360. MatType(CV_32FC4)),
  361. testing::Values(Border(1), Border(10), Border(50)),
  362. ALL_BORDER_TYPES,
  363. WHOLE_SUBMAT));
  364. }} // namespace
  365. #endif // HAVE_CUDA