test_channels.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
  14. // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
  15. // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // @Authors
  19. // Jia Haipeng, jiahaipeng95@gmail.com
  20. //
  21. // Redistribution and use in source and binary forms, with or without modification,
  22. // are permitted provided that the following conditions are met:
  23. //
  24. // * Redistribution's of source code must retain the above copyright notice,
  25. // this list of conditions and the following disclaimer.
  26. //
  27. // * Redistribution's in binary form must reproduce the above copyright notice,
  28. // this list of conditions and the following disclaimer in the documentation
  29. // and/or other materials provided with the distribution.
  30. //
  31. // * The name of the copyright holders may not be used to endorse or promote products
  32. // derived from this software without specific prior written permission.
  33. //
  34. // This software is provided by the copyright holders and contributors "as is" and
  35. // any express or implied warranties, including, but not limited to, the implied
  36. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  37. // In no event shall the Intel Corporation or contributors be liable for any direct,
  38. // indirect, incidental, special, exemplary, or consequential damages
  39. // (including, but not limited to, procurement of substitute goods or services;
  40. // loss of use, data, or profits; or business interruption) however caused
  41. // and on any theory of liability, whether in contract, strict liability,
  42. // or tort (including negligence or otherwise) arising in any way out of
  43. // the use of this software, even if advised of the possibility of such damage.
  44. //
  45. //M*/
  46. #include "../test_precomp.hpp"
  47. #include "opencv2/ts/ocl_test.hpp"
  48. #ifdef HAVE_OPENCL
  49. namespace opencv_test {
  50. namespace ocl {
  51. //////////////////////////////////////// Merge ///////////////////////////////////////////////
  52. PARAM_TEST_CASE(Merge, MatDepth, int, bool)
  53. {
  54. int depth, nsrc;
  55. bool use_roi;
  56. TEST_DECLARE_INPUT_PARAMETER(src1);
  57. TEST_DECLARE_INPUT_PARAMETER(src2);
  58. TEST_DECLARE_INPUT_PARAMETER(src3);
  59. TEST_DECLARE_INPUT_PARAMETER(src4);
  60. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  61. std::vector<Mat> src_roi;
  62. std::vector<UMat> usrc_roi;
  63. virtual void SetUp()
  64. {
  65. depth = GET_PARAM(0);
  66. nsrc = GET_PARAM(1);
  67. use_roi = GET_PARAM(2);
  68. CV_Assert(nsrc >= 1 && nsrc <= 4);
  69. }
  70. int type()
  71. {
  72. return CV_MAKE_TYPE(depth, randomInt(1, 3));
  73. }
  74. void generateTestData()
  75. {
  76. Size roiSize = randomSize(1, MAX_VALUE);
  77. {
  78. Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  79. randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);
  80. Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  81. randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);
  82. Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  83. randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);
  84. Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  85. randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);
  86. }
  87. UMAT_UPLOAD_INPUT_PARAMETER(src1);
  88. UMAT_UPLOAD_INPUT_PARAMETER(src2);
  89. UMAT_UPLOAD_INPUT_PARAMETER(src3);
  90. UMAT_UPLOAD_INPUT_PARAMETER(src4);
  91. src_roi.clear(); usrc_roi.clear(); // for test_loop_times > 1
  92. src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);
  93. if (nsrc >= 2)
  94. src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);
  95. if (nsrc >= 3)
  96. src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);
  97. if (nsrc >= 4)
  98. src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);
  99. int dcn = 0;
  100. for (int i = 0; i < nsrc; ++i)
  101. dcn += src_roi[i].channels();
  102. Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  103. randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, dcn), 5, 16);
  104. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  105. }
  106. void Near(double threshold = 0.)
  107. {
  108. OCL_EXPECT_MATS_NEAR(dst, threshold);
  109. }
  110. };
  111. OCL_TEST_P(Merge, Accuracy)
  112. {
  113. for(int j = 0; j < test_loop_times; j++)
  114. {
  115. generateTestData();
  116. OCL_OFF(cv::merge(src_roi, dst_roi));
  117. OCL_ON(cv::merge(usrc_roi, udst_roi));
  118. Near();
  119. }
  120. }
  121. //////////////////////////////////////// Split ///////////////////////////////////////////////
  122. PARAM_TEST_CASE(Split, MatType, Channels, bool)
  123. {
  124. int depth, cn;
  125. bool use_roi;
  126. TEST_DECLARE_INPUT_PARAMETER(src);
  127. TEST_DECLARE_OUTPUT_PARAMETER(dst1);
  128. TEST_DECLARE_OUTPUT_PARAMETER(dst2);
  129. TEST_DECLARE_OUTPUT_PARAMETER(dst3);
  130. TEST_DECLARE_OUTPUT_PARAMETER(dst4);
  131. std::vector<Mat> dst_roi, dst;
  132. std::vector<UMat> udst_roi, udst;
  133. virtual void SetUp()
  134. {
  135. depth = GET_PARAM(0);
  136. cn = GET_PARAM(1);
  137. use_roi = GET_PARAM(2);
  138. CV_Assert(cn >= 1 && cn <= 4);
  139. }
  140. void generateTestData()
  141. {
  142. Size roiSize = randomSize(1, MAX_VALUE);
  143. Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  144. randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 5, 16);
  145. {
  146. Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  147. randomSubMat(dst1, dst1_roi, roiSize, dst1Border, depth, 2, 11);
  148. Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  149. randomSubMat(dst2, dst2_roi, roiSize, dst2Border, depth, -1540, 1740);
  150. Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  151. randomSubMat(dst3, dst3_roi, roiSize, dst3Border, depth, -1540, 1740);
  152. Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  153. randomSubMat(dst4, dst4_roi, roiSize, dst4Border, depth, -1540, 1740);
  154. }
  155. UMAT_UPLOAD_INPUT_PARAMETER(src);
  156. UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);
  157. UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
  158. UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);
  159. UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);
  160. dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),
  161. dst.push_back(dst1), udst.push_back(udst1);
  162. if (cn >= 2)
  163. dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),
  164. dst.push_back(dst2), udst.push_back(udst2);
  165. if (cn >= 3)
  166. dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),
  167. dst.push_back(dst3), udst.push_back(udst3);
  168. if (cn >= 4)
  169. dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),
  170. dst.push_back(dst4), udst.push_back(udst4);
  171. }
  172. };
  173. OCL_TEST_P(Split, Accuracy)
  174. {
  175. for (int j = 0; j < test_loop_times; j++)
  176. {
  177. generateTestData();
  178. OCL_OFF(cv::split(src_roi, dst_roi));
  179. OCL_ON(cv::split(usrc_roi, udst_roi));
  180. for (int i = 0; i < cn; ++i)
  181. {
  182. EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);
  183. EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);
  184. }
  185. }
  186. }
  187. //////////////////////////////////////// MixChannels ///////////////////////////////////////////////
  188. PARAM_TEST_CASE(MixChannels, MatType, bool)
  189. {
  190. int depth;
  191. bool use_roi;
  192. TEST_DECLARE_INPUT_PARAMETER(src1);
  193. TEST_DECLARE_INPUT_PARAMETER(src2);
  194. TEST_DECLARE_INPUT_PARAMETER(src3);
  195. TEST_DECLARE_INPUT_PARAMETER(src4);
  196. TEST_DECLARE_OUTPUT_PARAMETER(dst1);
  197. TEST_DECLARE_OUTPUT_PARAMETER(dst2);
  198. TEST_DECLARE_OUTPUT_PARAMETER(dst3);
  199. TEST_DECLARE_OUTPUT_PARAMETER(dst4);
  200. std::vector<Mat> src_roi, dst_roi, dst;
  201. std::vector<UMat> usrc_roi, udst_roi, udst;
  202. std::vector<int> fromTo;
  203. virtual void SetUp()
  204. {
  205. depth = GET_PARAM(0);
  206. use_roi = GET_PARAM(1);
  207. }
  208. // generate number of channels and create type
  209. int type()
  210. {
  211. int cn = randomInt(1, 5);
  212. return CV_MAKE_TYPE(depth, cn);
  213. }
  214. void generateTestData()
  215. {
  216. src_roi.clear();
  217. dst_roi.clear();
  218. dst.clear();
  219. usrc_roi.clear();
  220. udst_roi.clear();
  221. udst.clear();
  222. fromTo.clear();
  223. Size roiSize = randomSize(1, MAX_VALUE);
  224. {
  225. Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  226. randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);
  227. Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  228. randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);
  229. Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  230. randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);
  231. Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  232. randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);
  233. }
  234. {
  235. Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  236. randomSubMat(dst1, dst1_roi, roiSize, dst1Border, type(), 2, 11);
  237. Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  238. randomSubMat(dst2, dst2_roi, roiSize, dst2Border, type(), -1540, 1740);
  239. Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  240. randomSubMat(dst3, dst3_roi, roiSize, dst3Border, type(), -1540, 1740);
  241. Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
  242. randomSubMat(dst4, dst4_roi, roiSize, dst4Border, type(), -1540, 1740);
  243. }
  244. UMAT_UPLOAD_INPUT_PARAMETER(src1);
  245. UMAT_UPLOAD_INPUT_PARAMETER(src2);
  246. UMAT_UPLOAD_INPUT_PARAMETER(src3);
  247. UMAT_UPLOAD_INPUT_PARAMETER(src4);
  248. UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);
  249. UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
  250. UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);
  251. UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);
  252. int nsrc = randomInt(1, 5), ndst = randomInt(1, 5);
  253. src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);
  254. if (nsrc >= 2)
  255. src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);
  256. if (nsrc >= 3)
  257. src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);
  258. if (nsrc >= 4)
  259. src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);
  260. dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),
  261. dst.push_back(dst1), udst.push_back(udst1);
  262. if (ndst >= 2)
  263. dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),
  264. dst.push_back(dst2), udst.push_back(udst2);
  265. if (ndst >= 3)
  266. dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),
  267. dst.push_back(dst3), udst.push_back(udst3);
  268. if (ndst >= 4)
  269. dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),
  270. dst.push_back(dst4), udst.push_back(udst4);
  271. int scntotal = 0, dcntotal = 0;
  272. for (int i = 0; i < nsrc; ++i)
  273. scntotal += src_roi[i].channels();
  274. for (int i = 0; i < ndst; ++i)
  275. dcntotal += dst_roi[i].channels();
  276. int npairs = randomInt(1, std::min(scntotal, dcntotal) + 1);
  277. fromTo.resize(npairs << 1);
  278. for (int i = 0; i < npairs; ++i)
  279. {
  280. fromTo[i<<1] = randomInt(0, scntotal);
  281. fromTo[(i<<1)+1] = randomInt(0, dcntotal);
  282. }
  283. }
  284. };
  285. OCL_TEST_P(MixChannels, Accuracy)
  286. {
  287. for (int j = 0; j < test_loop_times + 10; j++)
  288. {
  289. generateTestData();
  290. OCL_OFF(cv::mixChannels(src_roi, dst_roi, fromTo));
  291. OCL_ON(cv::mixChannels(usrc_roi, udst_roi, fromTo));
  292. for (size_t i = 0, size = dst_roi.size(); i < size; ++i)
  293. {
  294. EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);
  295. EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);
  296. }
  297. }
  298. }
  299. //////////////////////////////////////// InsertChannel ///////////////////////////////////////////////
  300. PARAM_TEST_CASE(InsertChannel, MatDepth, Channels, bool)
  301. {
  302. int depth, cn, coi;
  303. bool use_roi;
  304. TEST_DECLARE_INPUT_PARAMETER(src);
  305. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  306. virtual void SetUp()
  307. {
  308. depth = GET_PARAM(0);
  309. cn = GET_PARAM(1);
  310. use_roi = GET_PARAM(2);
  311. }
  312. void generateTestData()
  313. {
  314. Size roiSize = randomSize(1, MAX_VALUE);
  315. coi = randomInt(0, cn);
  316. Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  317. randomSubMat(src, src_roi, roiSize, srcBorder, depth, 2, 11);
  318. Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  319. randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, cn), 5, 16);
  320. UMAT_UPLOAD_INPUT_PARAMETER(src);
  321. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  322. }
  323. };
  324. OCL_TEST_P(InsertChannel, Accuracy)
  325. {
  326. for(int j = 0; j < test_loop_times; j++)
  327. {
  328. generateTestData();
  329. OCL_OFF(cv::insertChannel(src_roi, dst_roi, coi));
  330. OCL_ON(cv::insertChannel(usrc_roi, udst_roi, coi));
  331. OCL_EXPECT_MATS_NEAR(dst, 0);
  332. }
  333. }
  334. //////////////////////////////////////// ExtractChannel ///////////////////////////////////////////////
  335. PARAM_TEST_CASE(ExtractChannel, MatDepth, Channels, bool)
  336. {
  337. int depth, cn, coi;
  338. bool use_roi;
  339. TEST_DECLARE_INPUT_PARAMETER(src);
  340. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  341. virtual void SetUp()
  342. {
  343. depth = GET_PARAM(0);
  344. cn = GET_PARAM(1);
  345. use_roi = GET_PARAM(2);
  346. }
  347. void generateTestData()
  348. {
  349. Size roiSize = randomSize(1, MAX_VALUE);
  350. coi = randomInt(0, cn);
  351. Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  352. randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 2, 11);
  353. Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
  354. randomSubMat(dst, dst_roi, roiSize, dstBorder, depth, 5, 16);
  355. UMAT_UPLOAD_INPUT_PARAMETER(src);
  356. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  357. }
  358. };
  359. OCL_TEST_P(ExtractChannel, Accuracy)
  360. {
  361. for(int j = 0; j < test_loop_times; j++)
  362. {
  363. generateTestData();
  364. OCL_OFF(cv::extractChannel(src_roi, dst_roi, coi));
  365. OCL_ON(cv::extractChannel(usrc_roi, udst_roi, coi));
  366. OCL_EXPECT_MATS_NEAR(dst, 0);
  367. }
  368. }
  369. //////////////////////////////////////// Instantiation ///////////////////////////////////////////////
  370. OCL_INSTANTIATE_TEST_CASE_P(Channels, Merge, Combine(OCL_ALL_DEPTHS, Values(1, 2, 3, 4), Bool()));
  371. OCL_INSTANTIATE_TEST_CASE_P(Channels, Split, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
  372. OCL_INSTANTIATE_TEST_CASE_P(Channels, MixChannels, Combine(OCL_ALL_DEPTHS, Bool()));
  373. OCL_INSTANTIATE_TEST_CASE_P(Channels, InsertChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
  374. OCL_INSTANTIATE_TEST_CASE_P(Channels, ExtractChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
  375. } } // namespace opencv_test::ocl
  376. #endif // HAVE_OPENCL