test_imgproc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. // Niko Li, newlife20080214@gmail.com
  20. // Jia Haipeng, jiahaipeng95@gmail.com
  21. // Shengen Yan, yanshengen@gmail.com
  22. // Jiang Liyuan, lyuan001.good@163.com
  23. // Rock Li, Rock.Li@amd.com
  24. // Wu Zailong, bullet@yeah.net
  25. // Xu Pang, pangxu010@163.com
  26. // Sen Liu, swjtuls1987@126.com
  27. //
  28. // Redistribution and use in source and binary forms, with or without modification,
  29. // are permitted provided that the following conditions are met:
  30. //
  31. // * Redistribution's of source code must retain the above copyright notice,
  32. // this list of conditions and the following disclaimer.
  33. //
  34. // * Redistribution's in binary form must reproduce the above copyright notice,
  35. // this list of conditions and the following disclaimer in the documentation
  36. // and/or other materials provided with the distribution.
  37. //
  38. // * The name of the copyright holders may not be used to endorse or promote products
  39. // derived from this software without specific prior written permission.
  40. //
  41. // This software is provided by the copyright holders and contributors "as is" and
  42. // any express or implied warranties, including, but not limited to, the implied
  43. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  44. // In no event shall the Intel Corporation or contributors be liable for any direct,
  45. // indirect, incidental, special, exemplary, or consequential damages
  46. // (including, but not limited to, procurement of substitute goods or services;
  47. // loss of use, data, or profits; or business interruption) however caused
  48. // and on any theory of liability, whether in contract, strict liability,
  49. // or tort (including negligence or otherwise) arising in any way out of
  50. // the use of this software, even if advised of the possibility of such damage.
  51. //
  52. //M*/
  53. #include "../test_precomp.hpp"
  54. #include "opencv2/ts/ocl_test.hpp"
  55. #ifdef HAVE_OPENCL
  56. namespace opencv_test {
  57. namespace ocl {
  58. ///////////////////////////////////////////////////////////////////////////////
  59. PARAM_TEST_CASE(ImgprocTestBase, MatType,
  60. int, // blockSize
  61. int, // border type
  62. bool) // roi or not
  63. {
  64. int type, borderType, blockSize;
  65. bool useRoi;
  66. TEST_DECLARE_INPUT_PARAMETER(src);
  67. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  68. virtual void SetUp()
  69. {
  70. type = GET_PARAM(0);
  71. blockSize = GET_PARAM(1);
  72. borderType = GET_PARAM(2);
  73. useRoi = GET_PARAM(3);
  74. }
  75. void random_roi()
  76. {
  77. Size roiSize = randomSize(1, MAX_VALUE);
  78. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  79. randomSubMat(src, src_roi, roiSize, srcBorder, type, 5, 256);
  80. Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  81. randomSubMat(dst, dst_roi, roiSize, dstBorder, type, 5, 16);
  82. UMAT_UPLOAD_INPUT_PARAMETER(src);
  83. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  84. }
  85. void Near(double threshold = 0.0, bool relative = false)
  86. {
  87. if (relative)
  88. OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);
  89. else
  90. OCL_EXPECT_MATS_NEAR(dst, threshold);
  91. }
  92. };
  93. //////////////////////////////// copyMakeBorder ////////////////////////////////////////////
  94. PARAM_TEST_CASE(CopyMakeBorder, MatDepth, // depth
  95. Channels, // channels
  96. bool, // isolated or not
  97. BorderType, // border type
  98. bool) // roi or not
  99. {
  100. int type, borderType;
  101. bool useRoi;
  102. TestUtils::Border border;
  103. Scalar val;
  104. TEST_DECLARE_INPUT_PARAMETER(src);
  105. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  106. virtual void SetUp()
  107. {
  108. type = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));
  109. borderType = GET_PARAM(3);
  110. if (GET_PARAM(2))
  111. borderType |= BORDER_ISOLATED;
  112. useRoi = GET_PARAM(4);
  113. }
  114. void random_roi()
  115. {
  116. border = randomBorder(0, MAX_VALUE << 2);
  117. val = randomScalar(-MAX_VALUE, MAX_VALUE);
  118. Size roiSize = randomSize(1, MAX_VALUE);
  119. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  120. randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);
  121. Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  122. dstBorder.top += border.top;
  123. dstBorder.lef += border.lef;
  124. dstBorder.rig += border.rig;
  125. dstBorder.bot += border.bot;
  126. randomSubMat(dst, dst_roi, roiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);
  127. UMAT_UPLOAD_INPUT_PARAMETER(src);
  128. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  129. }
  130. void Near()
  131. {
  132. OCL_EXPECT_MATS_NEAR(dst, 0);
  133. }
  134. };
  135. OCL_TEST_P(CopyMakeBorder, Mat)
  136. {
  137. for (int i = 0; i < test_loop_times; ++i)
  138. {
  139. random_roi();
  140. OCL_OFF(cv::copyMakeBorder(src_roi, dst_roi, border.top, border.bot, border.lef, border.rig, borderType, val));
  141. OCL_ON(cv::copyMakeBorder(usrc_roi, udst_roi, border.top, border.bot, border.lef, border.rig, borderType, val));
  142. Near();
  143. }
  144. }
  145. //////////////////////////////// equalizeHist //////////////////////////////////////////////
  146. typedef ImgprocTestBase EqualizeHist;
  147. OCL_TEST_P(EqualizeHist, Mat)
  148. {
  149. for (int j = 0; j < test_loop_times; j++)
  150. {
  151. random_roi();
  152. OCL_OFF(cv::equalizeHist(src_roi, dst_roi));
  153. OCL_ON(cv::equalizeHist(usrc_roi, udst_roi));
  154. Near(1);
  155. }
  156. }
  157. //////////////////////////////// Corners test //////////////////////////////////////////
  158. struct CornerTestBase :
  159. public ImgprocTestBase
  160. {
  161. void random_roi()
  162. {
  163. Mat image = readImageType("../gpu/stereobm/aloe-L.png", type);
  164. ASSERT_FALSE(image.empty());
  165. bool isFP = CV_MAT_DEPTH(type) >= CV_32F;
  166. float val = 255.0f;
  167. if (isFP)
  168. {
  169. image.convertTo(image, -1, 1.0 / 255);
  170. val /= 255.0f;
  171. }
  172. Size roiSize = image.size();
  173. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  174. Size wholeSize = Size(roiSize.width + srcBorder.lef + srcBorder.rig, roiSize.height + srcBorder.top + srcBorder.bot);
  175. src = randomMat(wholeSize, type, -val, val, false);
  176. src_roi = src(Rect(srcBorder.lef, srcBorder.top, roiSize.width, roiSize.height));
  177. image.copyTo(src_roi);
  178. Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  179. randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_32FC1, 5, 16);
  180. UMAT_UPLOAD_INPUT_PARAMETER(src);
  181. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  182. }
  183. };
  184. typedef CornerTestBase CornerMinEigenVal;
  185. OCL_TEST_P(CornerMinEigenVal, Mat)
  186. {
  187. for (int j = 0; j < test_loop_times; j++)
  188. {
  189. random_roi();
  190. int apertureSize = 3;
  191. OCL_OFF(cv::cornerMinEigenVal(src_roi, dst_roi, blockSize, apertureSize, borderType));
  192. OCL_ON(cv::cornerMinEigenVal(usrc_roi, udst_roi, blockSize, apertureSize, borderType));
  193. // The corner kernel uses native_sqrt() which has implementation defined accuracy.
  194. // If we're using a CL implementation that isn't intel, test with relaxed accuracy.
  195. if (!ocl::useOpenCL() || ocl::Device::getDefault().isIntel())
  196. Near(1e-5, true);
  197. else
  198. Near(0.1, true);
  199. }
  200. }
  201. //////////////////////////////// cornerHarris //////////////////////////////////////////
  202. typedef CornerTestBase CornerHarris;
  203. OCL_TEST_P(CornerHarris, Mat)
  204. {
  205. for (int j = 0; j < test_loop_times; j++)
  206. {
  207. random_roi();
  208. int apertureSize = 3;
  209. double k = randomDouble(0.01, 0.9);
  210. OCL_OFF(cv::cornerHarris(src_roi, dst_roi, blockSize, apertureSize, k, borderType));
  211. OCL_ON(cv::cornerHarris(usrc_roi, udst_roi, blockSize, apertureSize, k, borderType));
  212. Near(1e-6, true);
  213. }
  214. }
  215. //////////////////////////////// preCornerDetect //////////////////////////////////////////
  216. typedef ImgprocTestBase PreCornerDetect;
  217. OCL_TEST_P(PreCornerDetect, Mat)
  218. {
  219. for (int j = 0; j < test_loop_times; j++)
  220. {
  221. random_roi();
  222. const int apertureSize = blockSize;
  223. OCL_OFF(cv::preCornerDetect(src_roi, dst_roi, apertureSize, borderType));
  224. OCL_ON(cv::preCornerDetect(usrc_roi, udst_roi, apertureSize, borderType));
  225. Near(1e-6, true);
  226. }
  227. }
  228. ////////////////////////////////// integral /////////////////////////////////////////////////
  229. struct Integral :
  230. public ImgprocTestBase
  231. {
  232. int sdepth, sqdepth;
  233. TEST_DECLARE_OUTPUT_PARAMETER(dst2);
  234. virtual void SetUp()
  235. {
  236. type = GET_PARAM(0);
  237. sdepth = GET_PARAM(1);
  238. sqdepth = GET_PARAM(2);
  239. useRoi = GET_PARAM(3);
  240. }
  241. void random_roi()
  242. {
  243. ASSERT_EQ(CV_MAT_CN(type), 1);
  244. Size roiSize = randomSize(1, MAX_VALUE), isize = Size(roiSize.width + 1, roiSize.height + 1);
  245. Border srcBorder = randomBorder(0, useRoi ? 2 : 0);
  246. randomSubMat(src, src_roi, roiSize, srcBorder, type, 5, 256);
  247. Border dstBorder = randomBorder(0, useRoi ? 2 : 0);
  248. randomSubMat(dst, dst_roi, isize, dstBorder, sdepth, 5, 16);
  249. Border dst2Border = randomBorder(0, useRoi ? 2 : 0);
  250. randomSubMat(dst2, dst2_roi, isize, dst2Border, sqdepth, 5, 16);
  251. UMAT_UPLOAD_INPUT_PARAMETER(src);
  252. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  253. UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
  254. }
  255. void Near2(double threshold = 0.0, bool relative = false)
  256. {
  257. if (relative)
  258. OCL_EXPECT_MATS_NEAR_RELATIVE(dst2, threshold);
  259. else
  260. OCL_EXPECT_MATS_NEAR(dst2, threshold);
  261. }
  262. };
  263. OCL_TEST_P(Integral, Mat1)
  264. {
  265. for (int j = 0; j < test_loop_times; j++)
  266. {
  267. random_roi();
  268. OCL_OFF(cv::integral(src_roi, dst_roi, sdepth));
  269. OCL_ON(cv::integral(usrc_roi, udst_roi, sdepth));
  270. Near();
  271. }
  272. }
  273. OCL_TEST_P(Integral, Mat2)
  274. {
  275. for (int j = 0; j < test_loop_times; j++)
  276. {
  277. random_roi();
  278. OCL_OFF(cv::integral(src_roi, dst_roi, dst2_roi, sdepth, sqdepth));
  279. OCL_ON(cv::integral(usrc_roi, udst_roi, udst2_roi, sdepth, sqdepth));
  280. Near();
  281. sqdepth == CV_32F ? Near2(1e-6, true) : Near2();
  282. }
  283. }
  284. //////////////////////////////////////// threshold //////////////////////////////////////////////
  285. struct Threshold :
  286. public ImgprocTestBase
  287. {
  288. int thresholdType;
  289. virtual void SetUp()
  290. {
  291. type = GET_PARAM(0);
  292. thresholdType = GET_PARAM(2);
  293. useRoi = GET_PARAM(3);
  294. }
  295. };
  296. OCL_TEST_P(Threshold, Mat)
  297. {
  298. for (int j = 0; j < test_loop_times; j++)
  299. {
  300. random_roi();
  301. double maxVal = randomDouble(20.0, 127.0);
  302. double thresh = randomDouble(0.0, maxVal);
  303. OCL_OFF(cv::threshold(src_roi, dst_roi, thresh, maxVal, thresholdType));
  304. OCL_ON(cv::threshold(usrc_roi, udst_roi, thresh, maxVal, thresholdType));
  305. Near(1);
  306. }
  307. }
  308. /////////////////////////////////////////// CLAHE //////////////////////////////////////////////////
  309. PARAM_TEST_CASE(CLAHETest, Size, double, bool)
  310. {
  311. Size gridSize;
  312. double clipLimit;
  313. bool useRoi;
  314. TEST_DECLARE_INPUT_PARAMETER(src);
  315. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  316. virtual void SetUp()
  317. {
  318. gridSize = GET_PARAM(0);
  319. clipLimit = GET_PARAM(1);
  320. useRoi = GET_PARAM(2);
  321. }
  322. void random_roi()
  323. {
  324. Size roiSize = randomSize(std::max(gridSize.height, gridSize.width), MAX_VALUE);
  325. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  326. randomSubMat(src, src_roi, roiSize, srcBorder, CV_8UC1, 5, 256);
  327. Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  328. randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_8UC1, 5, 16);
  329. UMAT_UPLOAD_INPUT_PARAMETER(src);
  330. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  331. }
  332. void Near(double threshold = 0.0)
  333. {
  334. OCL_EXPECT_MATS_NEAR(dst, threshold);
  335. }
  336. };
  337. OCL_TEST_P(CLAHETest, Accuracy)
  338. {
  339. for (int i = 0; i < test_loop_times; ++i)
  340. {
  341. random_roi();
  342. Ptr<CLAHE> clahe = cv::createCLAHE(clipLimit, gridSize);
  343. OCL_OFF(clahe->apply(src_roi, dst_roi));
  344. OCL_ON(clahe->apply(usrc_roi, udst_roi));
  345. Near(1.0);
  346. }
  347. }
  348. /////////////////////////////////////////////////////////////////////////////////////
  349. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, EqualizeHist, Combine(
  350. Values((MatType)CV_8UC1),
  351. Values(0), // not used
  352. Values(0), // not used
  353. Bool()));
  354. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CornerMinEigenVal, Combine(
  355. Values((MatType)CV_8UC1, (MatType)CV_32FC1),
  356. Values(3, 5),
  357. Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
  358. (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT101),
  359. Bool()));
  360. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CornerHarris, Combine(
  361. Values((MatType)CV_8UC1, CV_32FC1),
  362. Values(3, 5),
  363. Values( (BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
  364. (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT_101),
  365. Bool()));
  366. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, PreCornerDetect, Combine(
  367. Values((MatType)CV_8UC1, CV_32FC1),
  368. Values(3, 5),
  369. Values( (BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE,
  370. (BorderType)BORDER_REFLECT, (BorderType)BORDER_REFLECT_101),
  371. Bool()));
  372. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Integral, Combine(
  373. Values((MatType)CV_8UC1), // TODO does not work with CV_32F, CV_64F
  374. Values(CV_32SC1, CV_32FC1), // desired sdepth
  375. Values(CV_32FC1, CV_64FC1), // desired sqdepth
  376. Bool()));
  377. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, Threshold, Combine(
  378. Values(CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4,
  379. CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4,
  380. CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4),
  381. Values(0),
  382. Values(ThreshOp(THRESH_BINARY),
  383. ThreshOp(THRESH_BINARY_INV), ThreshOp(THRESH_TRUNC),
  384. ThreshOp(THRESH_TOZERO), ThreshOp(THRESH_TOZERO_INV)),
  385. Bool()));
  386. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CLAHETest, Combine(
  387. Values(Size(4, 4), Size(32, 8), Size(8, 64)),
  388. Values(0.0, 10.0, 62.0, 300.0),
  389. Bool()));
  390. OCL_INSTANTIATE_TEST_CASE_P(ImgprocTestBase, CopyMakeBorder, Combine(
  391. testing::Values((MatDepth)CV_8U, (MatDepth)CV_16S, (MatDepth)CV_32S, (MatDepth)CV_32F),
  392. testing::Values(Channels(1), Channels(3), (Channels)4),
  393. Bool(), // border isolated or not
  394. Values((BorderType)BORDER_CONSTANT, (BorderType)BORDER_REPLICATE, (BorderType)BORDER_REFLECT,
  395. (BorderType)BORDER_WRAP, (BorderType)BORDER_REFLECT_101),
  396. Bool()));
  397. } } // namespace opencv_test::ocl
  398. #endif // HAVE_OPENCL