test_histogram.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. // Copyright (C) 2014, Itseez, Inc., all rights reserved.
  17. // Third party copyrights are property of their respective owners.
  18. //
  19. // @Authors
  20. // Niko Li, newlife20080214@gmail.com
  21. // Jia Haipeng, jiahaipeng95@gmail.com
  22. // Shengen Yan, yanshengen@gmail.com
  23. // Jiang Liyuan, lyuan001.good@163.com
  24. // Rock Li, Rock.Li@amd.com
  25. // Wu Zailong, bullet@yeah.net
  26. // Xu Pang, pangxu010@163.com
  27. // Sen Liu, swjtuls1987@126.com
  28. //
  29. // Redistribution and use in source and binary forms, with or without modification,
  30. // are permitted provided that the following conditions are met:
  31. //
  32. // * Redistribution's of source code must retain the above copyright notice,
  33. // this list of conditions and the following disclaimer.
  34. //
  35. // * Redistribution's in binary form must reproduce the above copyright notice,
  36. // this list of conditions and the following disclaimer in the documentation
  37. // and/or other materials provided with the distribution.
  38. //
  39. // * The name of the copyright holders may not be used to endorse or promote products
  40. // derived from this software without specific prior written permission.
  41. //
  42. // This software is provided by the copyright holders and contributors "as is" and
  43. // any express or implied warranties, including, but not limited to, the implied
  44. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  45. // In no event shall the Intel Corporation or contributors be liable for any direct,
  46. // indirect, incidental, special, exemplary, or consequential damages
  47. // (including, but not limited to, procurement of substitute goods or services;
  48. // loss of use, data, or profits; or business interruption) however caused
  49. // and on any theory of liability, whether in contract, strict liability,
  50. // or tort (including negligence or otherwise) arising in any way out of
  51. // the use of this software, even if advised of the possibility of such damage.
  52. //
  53. //M*/
  54. #include "../test_precomp.hpp"
  55. #include "opencv2/ts/ocl_test.hpp"
  56. #ifdef HAVE_OPENCL
  57. namespace opencv_test {
  58. namespace ocl {
  59. ///////////////////////////////////////////////////////////////////////////////
  60. PARAM_TEST_CASE(CalcBackProject, MatDepth, int, bool)
  61. {
  62. int depth, N;
  63. bool useRoi;
  64. std::vector<float> ranges;
  65. std::vector<int> channels;
  66. double scale;
  67. std::vector<Mat> images;
  68. std::vector<Mat> images_roi;
  69. std::vector<UMat> uimages;
  70. std::vector<UMat> uimages_roi;
  71. TEST_DECLARE_INPUT_PARAMETER(hist);
  72. TEST_DECLARE_OUTPUT_PARAMETER(dst);
  73. virtual void SetUp()
  74. {
  75. depth = GET_PARAM(0);
  76. N = GET_PARAM(1);
  77. useRoi = GET_PARAM(2);
  78. ASSERT_GE(2, N);
  79. images.resize(N);
  80. images_roi.resize(N);
  81. uimages.resize(N);
  82. uimages_roi.resize(N);
  83. }
  84. void random_roi()
  85. {
  86. Size roiSize = randomSize(1, MAX_VALUE);
  87. int totalChannels = 0;
  88. ranges.clear();
  89. channels.clear();
  90. for (int i = 0; i < N; ++i)
  91. {
  92. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  93. int cn = randomInt(1, 5);
  94. randomSubMat(images[i], images_roi[i], roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 0, 125);
  95. ranges.push_back(10);
  96. ranges.push_back(100);
  97. channels.push_back(randomInt(0, cn) + totalChannels);
  98. totalChannels += cn;
  99. }
  100. Mat tmpHist;
  101. {
  102. std::vector<int> hist_size(N);
  103. for (int i = 0 ; i < N; ++i)
  104. hist_size[i] = randomInt(10, 50);
  105. cv::calcHist(images_roi, channels, noArray(), tmpHist, hist_size, ranges);
  106. ASSERT_EQ(CV_32FC1, tmpHist.type());
  107. }
  108. Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  109. randomSubMat(hist, hist_roi, tmpHist.size(), histBorder, tmpHist.type(), 0, MAX_VALUE);
  110. tmpHist.copyTo(hist_roi);
  111. Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  112. randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, 1), 5, 16);
  113. for (int i = 0; i < N; ++i)
  114. {
  115. images[i].copyTo(uimages[i]);
  116. Size _wholeSize;
  117. Point ofs;
  118. images_roi[i].locateROI(_wholeSize, ofs);
  119. uimages_roi[i] = uimages[i](Rect(ofs.x, ofs.y, images_roi[i].cols, images_roi[i].rows));
  120. }
  121. UMAT_UPLOAD_INPUT_PARAMETER(hist);
  122. UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
  123. scale = randomDouble(0.1, 1);
  124. }
  125. void test_by_pict()
  126. {
  127. Mat frame1 = readImage("optflow/RubberWhale1.png", IMREAD_GRAYSCALE);
  128. UMat usrc;
  129. frame1.copyTo(usrc);
  130. int histSize = randomInt(3, 29);
  131. float hue_range[] = { 0, 180 };
  132. const float* ranges1 = { hue_range };
  133. Mat hist1;
  134. //compute histogram
  135. calcHist(&frame1, 1, 0, Mat(), hist1, 1, &histSize, &ranges1, true, false);
  136. normalize(hist1, hist1, 0, 255, NORM_MINMAX, -1, Mat());
  137. Mat dst1;
  138. UMat udst1, src, uhist1;
  139. hist1.copyTo(uhist1);
  140. std::vector<UMat> uims;
  141. uims.push_back(usrc);
  142. std::vector<float> urngs;
  143. urngs.push_back(0);
  144. urngs.push_back(180);
  145. std::vector<int> chs;
  146. chs.push_back(0);
  147. OCL_OFF(calcBackProject(&frame1, 1, 0, hist1, dst1, &ranges1, 1, true));
  148. OCL_ON(calcBackProject(uims, chs, uhist1, udst1, urngs, 1.0));
  149. if (cv::ocl::useOpenCL() && cv::ocl::Device::getDefault().isAMD())
  150. {
  151. Size dstSize = dst1.size();
  152. int nDiffs = (int)(0.03f*dstSize.height*dstSize.width);
  153. //check if the dst mats are the same except 3% difference
  154. EXPECT_MAT_N_DIFF(dst1, udst1, nDiffs);
  155. }
  156. else
  157. {
  158. EXPECT_MAT_NEAR(dst1, udst1, 0.0);
  159. }
  160. }
  161. };
  162. //////////////////////////////// CalcBackProject //////////////////////////////////////////////
  163. OCL_TEST_P(CalcBackProject, Mat)
  164. {
  165. for (int j = 0; j < test_loop_times; j++)
  166. {
  167. random_roi();
  168. OCL_OFF(cv::calcBackProject(images_roi, channels, hist_roi, dst_roi, ranges, scale));
  169. OCL_ON(cv::calcBackProject(uimages_roi, channels, uhist_roi, udst_roi, ranges, scale));
  170. Size dstSize = dst_roi.size();
  171. int nDiffs = std::max((int)(0.07f*dstSize.area()), 1);
  172. //check if the dst mats are the same except 7% difference
  173. EXPECT_MAT_N_DIFF(dst_roi, udst_roi, nDiffs);
  174. }
  175. }
  176. OCL_TEST_P(CalcBackProject, Mat_RealImage)
  177. {
  178. //check on given image
  179. test_by_pict();
  180. }
  181. //////////////////////////////// CalcHist //////////////////////////////////////////////
  182. PARAM_TEST_CASE(CalcHist, bool)
  183. {
  184. bool useRoi;
  185. TEST_DECLARE_INPUT_PARAMETER(src);
  186. TEST_DECLARE_OUTPUT_PARAMETER(hist);
  187. virtual void SetUp()
  188. {
  189. useRoi = GET_PARAM(0);
  190. }
  191. void random_roi()
  192. {
  193. Size roiSize = randomSize(1, MAX_VALUE);
  194. Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  195. randomSubMat(src, src_roi, roiSize, srcBorder, CV_8UC1, 0, 256);
  196. Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);
  197. randomSubMat(hist, hist_roi, Size(1, 256), histBorder, CV_32SC1, 0, MAX_VALUE);
  198. UMAT_UPLOAD_INPUT_PARAMETER(src);
  199. UMAT_UPLOAD_OUTPUT_PARAMETER(hist);
  200. }
  201. };
  202. OCL_TEST_P(CalcHist, Mat)
  203. {
  204. const std::vector<int> channels(1, 0);
  205. std::vector<float> ranges(2);
  206. std::vector<int> histSize(1, 256);
  207. ranges[0] = 0;
  208. ranges[1] = 256;
  209. for (int j = 0; j < test_loop_times; j++)
  210. {
  211. random_roi();
  212. OCL_OFF(cv::calcHist(std::vector<Mat>(1, src_roi), channels, noArray(), hist_roi, histSize, ranges, false));
  213. OCL_ON(cv::calcHist(std::vector<UMat>(1, usrc_roi), channels, noArray(), uhist_roi, histSize, ranges, false));
  214. OCL_EXPECT_MATS_NEAR(hist, 0.0);
  215. }
  216. }
  217. /////////////////////////////////////////////////////////////////////////////////////
  218. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcBackProject, Combine(Values((MatDepth)CV_8U), Values(1, 2), Bool()));
  219. OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcHist, Values(true, false));
  220. } } // namespace opencv_test::ocl
  221. #endif // HAVE_OPENCL