TestRectStdDev.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. TestRectStdDev::TestRectStdDev(std::string testName_, NCVTestSourceProvider<Ncv8u> &src_,
  44. Ncv32u width_, Ncv32u height_, NcvRect32u rect_, Ncv32f scaleFactor_,
  45. NcvBool bTextureCache_)
  46. :
  47. NCVTestProvider(testName_),
  48. src(src_),
  49. width(width_),
  50. height(height_),
  51. rect(rect_),
  52. scaleFactor(scaleFactor_),
  53. bTextureCache(bTextureCache_)
  54. {
  55. }
  56. bool TestRectStdDev::toString(std::ofstream &strOut)
  57. {
  58. strOut << "width=" << width << std::endl;
  59. strOut << "height=" << height << std::endl;
  60. strOut << "rect=[" << rect.x << ", " << rect.y << ", " << rect.width << ", " << rect.height << "]\n";
  61. strOut << "scaleFactor=" << scaleFactor << std::endl;
  62. strOut << "bTextureCache=" << bTextureCache << std::endl;
  63. return true;
  64. }
  65. bool TestRectStdDev::init()
  66. {
  67. return true;
  68. }
  69. bool TestRectStdDev::process()
  70. {
  71. NCVStatus ncvStat;
  72. bool rcode = false;
  73. Ncv32s _normWidth = (Ncv32s)this->width - this->rect.x - this->rect.width + 1;
  74. Ncv32s _normHeight = (Ncv32s)this->height - this->rect.y - this->rect.height + 1;
  75. if (_normWidth <= 0 || _normHeight <= 0)
  76. {
  77. return true;
  78. }
  79. Ncv32u normWidth = (Ncv32u)_normWidth;
  80. Ncv32u normHeight = (Ncv32u)_normHeight;
  81. NcvSize32u szNormRoi(normWidth, normHeight);
  82. Ncv32u widthII = this->width + 1;
  83. Ncv32u heightII = this->height + 1;
  84. Ncv32u widthSII = this->width + 1;
  85. Ncv32u heightSII = this->height + 1;
  86. NCVMatrixAlloc<Ncv8u> d_img(*this->allocatorGPU.get(), this->width, this->height);
  87. ncvAssertReturn(d_img.isMemAllocated(), false);
  88. NCVMatrixAlloc<Ncv8u> h_img(*this->allocatorCPU.get(), this->width, this->height);
  89. ncvAssertReturn(h_img.isMemAllocated(), false);
  90. NCVMatrixAlloc<Ncv32u> d_imgII(*this->allocatorGPU.get(), widthII, heightII);
  91. ncvAssertReturn(d_imgII.isMemAllocated(), false);
  92. NCVMatrixAlloc<Ncv32u> h_imgII(*this->allocatorCPU.get(), widthII, heightII);
  93. ncvAssertReturn(h_imgII.isMemAllocated(), false);
  94. NCVMatrixAlloc<Ncv64u> d_imgSII(*this->allocatorGPU.get(), widthSII, heightSII);
  95. ncvAssertReturn(d_imgSII.isMemAllocated(), false);
  96. NCVMatrixAlloc<Ncv64u> h_imgSII(*this->allocatorCPU.get(), widthSII, heightSII);
  97. ncvAssertReturn(h_imgSII.isMemAllocated(), false);
  98. NCVMatrixAlloc<Ncv32f> d_norm(*this->allocatorGPU.get(), normWidth, normHeight);
  99. ncvAssertReturn(d_norm.isMemAllocated(), false);
  100. NCVMatrixAlloc<Ncv32f> h_norm(*this->allocatorCPU.get(), normWidth, normHeight);
  101. ncvAssertReturn(h_norm.isMemAllocated(), false);
  102. NCVMatrixAlloc<Ncv32f> h_norm_d(*this->allocatorCPU.get(), normWidth, normHeight);
  103. ncvAssertReturn(h_norm_d.isMemAllocated(), false);
  104. Ncv32u bufSizeII, bufSizeSII;
  105. ncvStat = nppiStIntegralGetSize_8u32u(NcvSize32u(this->width, this->height), &bufSizeII, this->devProp);
  106. ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
  107. ncvStat = nppiStSqrIntegralGetSize_8u64u(NcvSize32u(this->width, this->height), &bufSizeSII, this->devProp);
  108. ncvAssertReturn(NPPST_SUCCESS == ncvStat, false);
  109. Ncv32u bufSize = bufSizeII > bufSizeSII ? bufSizeII : bufSizeSII;
  110. NCVVectorAlloc<Ncv8u> d_tmpBuf(*this->allocatorGPU.get(), bufSize);
  111. ncvAssertReturn(d_tmpBuf.isMemAllocated(), false);
  112. NCV_SET_SKIP_COND(this->allocatorGPU.get()->isCounting());
  113. NCV_SKIP_COND_BEGIN
  114. ncvAssertReturn(this->src.fill(h_img), false);
  115. ncvStat = h_img.copySolid(d_img, 0);
  116. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  117. ncvStat = nppiStIntegral_8u32u_C1R(d_img.ptr(), d_img.pitch(),
  118. d_imgII.ptr(), d_imgII.pitch(),
  119. NcvSize32u(this->width, this->height),
  120. d_tmpBuf.ptr(), bufSize, this->devProp);
  121. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  122. ncvStat = nppiStSqrIntegral_8u64u_C1R(d_img.ptr(), d_img.pitch(),
  123. d_imgSII.ptr(), d_imgSII.pitch(),
  124. NcvSize32u(this->width, this->height),
  125. d_tmpBuf.ptr(), bufSize, this->devProp);
  126. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  127. ncvStat = nppiStRectStdDev_32f_C1R(d_imgII.ptr(), d_imgII.pitch(),
  128. d_imgSII.ptr(), d_imgSII.pitch(),
  129. d_norm.ptr(), d_norm.pitch(),
  130. szNormRoi, this->rect,
  131. this->scaleFactor,
  132. this->bTextureCache);
  133. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  134. ncvStat = d_norm.copySolid(h_norm_d, 0);
  135. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  136. ncvStat = nppiStIntegral_8u32u_C1R_host(h_img.ptr(), h_img.pitch(),
  137. h_imgII.ptr(), h_imgII.pitch(),
  138. NcvSize32u(this->width, this->height));
  139. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  140. ncvStat = nppiStSqrIntegral_8u64u_C1R_host(h_img.ptr(), h_img.pitch(),
  141. h_imgSII.ptr(), h_imgSII.pitch(),
  142. NcvSize32u(this->width, this->height));
  143. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  144. ncvStat = nppiStRectStdDev_32f_C1R_host(h_imgII.ptr(), h_imgII.pitch(),
  145. h_imgSII.ptr(), h_imgSII.pitch(),
  146. h_norm.ptr(), h_norm.pitch(),
  147. szNormRoi, this->rect,
  148. this->scaleFactor);
  149. ncvAssertReturn(ncvStat == NPPST_SUCCESS, false);
  150. NCV_SKIP_COND_END
  151. //bit-to-bit check
  152. bool bLoopVirgin = true;
  153. NCV_SKIP_COND_BEGIN
  154. const Ncv64f relEPS = 0.005;
  155. for (Ncv32u i=0; bLoopVirgin && i < h_norm.height(); i++)
  156. {
  157. for (Ncv32u j=0; bLoopVirgin && j < h_norm.width(); j++)
  158. {
  159. Ncv64f absErr = fabs(h_norm.ptr()[h_norm.stride()*i+j] - h_norm_d.ptr()[h_norm_d.stride()*i+j]);
  160. Ncv64f relErr = absErr / h_norm.ptr()[h_norm.stride()*i+j];
  161. if (relErr > relEPS)
  162. {
  163. bLoopVirgin = false;
  164. }
  165. }
  166. }
  167. NCV_SKIP_COND_END
  168. if (bLoopVirgin)
  169. {
  170. rcode = true;
  171. }
  172. return rcode;
  173. }
  174. bool TestRectStdDev::deinit()
  175. {
  176. return true;
  177. }