test_stackblur.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. /*
  5. StackBlur - a fast almost Gaussian Blur
  6. Theory: http://underdestruction.com/2004/02/25/stackblur-2004
  7. The code has been borrowed from (https://github.com/flozz/StackBlur).
  8. Below is the original copyright
  9. */
  10. /*
  11. Copyright (c) 2010 Mario Klingemann
  12. Permission is hereby granted, free of charge, to any person
  13. obtaining a copy of this software and associated documentation
  14. files (the "Software"), to deal in the Software without
  15. restriction, including without limitation the rights to use,
  16. copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. copies of the Software, and to permit persons to whom the
  18. Software is furnished to do so, subject to the following
  19. conditions:
  20. The above copyright notice and this permission notice shall be
  21. included in all copies or substantial portions of the Software.
  22. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  24. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  26. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  27. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  29. OTHER DEALINGS IN THE SOFTWARE.
  30. */
  31. #include "test_precomp.hpp"
  32. namespace opencv_test { namespace {
  33. template<typename T>
  34. void _stackblurRef(const Mat& src, Mat& dst, Size ksize)
  35. {
  36. CV_Assert(!src.empty());
  37. CV_Assert(ksize.width > 0 && ksize.height > 0 && ksize.height % 2 == 1 && ksize.width % 2 == 1);
  38. dst.create(src.size(), src.type());
  39. const int CN = src.channels();
  40. int rowsImg = src.rows;
  41. int colsImg = src.cols;
  42. int wm = colsImg - 1;
  43. int radiusW = ksize.width / 2;
  44. int stackLenW = ksize.width;
  45. const float mulW = 1.0f / (((float )radiusW + 1.0f) * ((float )radiusW + 1.0f));
  46. // Horizontal direction
  47. std::vector<T> stack(stackLenW * CN);
  48. for (int row = 0; row < rowsImg; row++)
  49. {
  50. std::vector<float> sum(CN, 0);
  51. std::vector<float> sumIn(CN, 0);
  52. std::vector<float> sumOut(CN, 0);
  53. const T* srcPtr = src.ptr<T>(row);
  54. for (int i = 0; i <= radiusW; i++)
  55. {
  56. for (int ci = 0; ci < CN; ci++)
  57. {
  58. T tmp = *(srcPtr + ci);
  59. stack[i * CN + ci] = tmp;
  60. sum[ci] += tmp * (i + 1);
  61. sumOut[ci] += tmp;
  62. }
  63. }
  64. for (int i = 1; i <= radiusW; i++)
  65. {
  66. if (i <= wm) srcPtr += CN;
  67. for(int ci = 0; ci < CN; ci++)
  68. {
  69. T tmp = *(srcPtr + ci);
  70. stack[(i + radiusW) * CN + ci] = tmp;
  71. sum[ci] += tmp * (radiusW + 1 - i);
  72. sumIn[ci] += tmp;
  73. }
  74. }
  75. int sp = radiusW;
  76. int xp = radiusW ;
  77. if (xp > wm) xp = wm;
  78. T* dstPtr = dst.ptr<T>(row);
  79. srcPtr = src.ptr<T>(row) + xp * CN;
  80. int stackStart= 0;
  81. for (int i = 0; i < colsImg; i++)
  82. {
  83. stackStart = sp + stackLenW - radiusW;
  84. if (stackStart >= stackLenW) stackStart -= stackLenW;
  85. for(int ci = 0; ci < CN; ci++)
  86. {
  87. *(dstPtr + ci) = cv::saturate_cast<T>(sum[ci] * mulW);
  88. sum[ci] -= sumOut[ci];
  89. sumOut[ci] -= stack[stackStart*CN + ci];
  90. }
  91. const T* srcNew = srcPtr;
  92. if(xp < wm)
  93. srcNew += CN;
  94. for (int ci = 0; ci < CN; ci++)
  95. {
  96. stack[stackStart * CN + ci] = *(srcNew + ci);
  97. sumIn[ci] += *(srcNew + ci);
  98. sum[ci] += sumIn[ci];
  99. }
  100. int sp1 = sp + 1;
  101. if (sp1 >= stackLenW)
  102. sp1 = 0;
  103. for(int ci = 0; ci < CN; ci++)
  104. {
  105. T tmp = stack[sp1*CN + ci];
  106. sumOut[ci] += tmp;
  107. sumIn[ci] -= tmp;
  108. }
  109. dstPtr += CN;
  110. if (xp < wm)
  111. {
  112. xp++;
  113. srcPtr += CN;
  114. }
  115. ++sp;
  116. if (sp >= stackLenW)
  117. sp = 0;
  118. }
  119. }
  120. // Vertical direction
  121. int hm = rowsImg - 1;
  122. int widthElem = colsImg * CN;
  123. int radiusH = ksize.height / 2;
  124. int stackLenH = ksize.height;
  125. const float mulH = 1.0f / (((float )radiusH + 1.0f) * ((float )radiusH + 1.0f));
  126. stack.resize(stackLenH, 0);
  127. for (int col = 0; col < widthElem; col++)
  128. {
  129. const T* srcPtr =dst.ptr<T>() + col;
  130. float sum0 = 0;
  131. float sumIn0 = 0;
  132. float sumOut0 = 0;
  133. for (int i = 0; i <= radiusH; i++)
  134. {
  135. T tmp = (T)(*srcPtr);
  136. stack[i] = tmp;
  137. sum0 += tmp * (i + 1);
  138. sumOut0 += tmp;
  139. }
  140. for (int i = 1; i <= radiusH; i++)
  141. {
  142. if (i <= hm) srcPtr += widthElem;
  143. T tmp = (T)(*srcPtr);
  144. stack[i + radiusH] = tmp;
  145. sum0 += tmp * (radiusH - i + 1);
  146. sumIn0 += tmp;
  147. }
  148. int sp = radiusH;
  149. int yp = radiusH;
  150. if (yp > hm) yp = hm;
  151. T* dstPtr = dst.ptr<T>() + col;
  152. srcPtr = dst.ptr<T>(yp) + col;
  153. const T* srcNew;
  154. int stackStart = 0;
  155. for (int i = 0; i < rowsImg; i++)
  156. {
  157. stackStart = sp + stackLenH - radiusH;
  158. if (stackStart >= stackLenH) stackStart -= stackLenH;
  159. *(dstPtr) = saturate_cast<T>(sum0 * mulH);
  160. sum0 -= sumOut0;
  161. sumOut0 -= stack[stackStart];
  162. srcNew = srcPtr;
  163. if (yp < hm)
  164. srcNew += widthElem;
  165. stack[stackStart] = *(srcNew);
  166. sumIn0 += *(srcNew);
  167. sum0 += sumIn0;
  168. int sp1 = sp + 1;
  169. sp1 &= -(sp1 < stackLenH);
  170. sumOut0 += stack[sp1];
  171. sumIn0 -= stack[sp1];
  172. dstPtr += widthElem;
  173. if (yp < hm)
  174. {
  175. yp++;
  176. srcPtr += widthElem;
  177. }
  178. ++sp;
  179. if (sp >= stackLenH) sp = 0;
  180. }
  181. }
  182. }
  183. void stackBlurRef(const Mat& img, Mat& dst, Size ksize)
  184. {
  185. if(img.depth() == CV_8U)
  186. _stackblurRef<uchar>(img, dst, ksize);
  187. else if (img.depth() == CV_16S)
  188. _stackblurRef<short>(img, dst, ksize);
  189. else if (img.depth() == CV_16U)
  190. _stackblurRef<ushort>(img, dst, ksize);
  191. else if (img.depth() == CV_32F)
  192. _stackblurRef<float>(img, dst, ksize);
  193. else
  194. CV_Error(Error::StsNotImplemented,
  195. ("Unsupported Mat type in stackBlurRef, "
  196. "the supported formats are: CV_8U, CV_16U, CV_16S and CV_32F."));
  197. }
  198. std::vector<Size> kernelSizeVec = {
  199. Size(3, 3),
  200. Size(5, 5),
  201. Size(101, 101),
  202. Size(3, 9)
  203. };
  204. typedef testing::TestWithParam<tuple<int, int, int> > StackBlur;
  205. TEST_P (StackBlur, regression)
  206. {
  207. Mat img_ = imread(findDataFile("shared/fruits.png"), 1);
  208. const int cn = get<0>(GetParam());
  209. const int kIndex = get<1>(GetParam());
  210. const int dtype = get<2>(GetParam());
  211. Size ksize = kernelSizeVec[kIndex];
  212. Mat img, dstRef, dst;
  213. convert(img_, img, dtype);
  214. vector<Mat> channels;
  215. split(img, channels);
  216. channels.push_back(channels[0]); // channels size is 4.
  217. Mat imgCn;
  218. if (cn == 1)
  219. imgCn = channels[0];
  220. else if (cn == 4)
  221. merge(channels, imgCn);
  222. else
  223. imgCn = img;
  224. stackBlurRef(imgCn, dstRef, ksize);
  225. stackBlur(imgCn, dst, ksize);
  226. EXPECT_LE(cvtest::norm(dstRef, dst, NORM_INF), 2.);
  227. }
  228. INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur,
  229. testing::Combine(
  230. testing::Values(1, 3, 4),
  231. testing::Values(0, 1, 2, 3),
  232. testing::Values(CV_8U, CV_16S, CV_16U, CV_32F)
  233. )
  234. );
  235. typedef testing::TestWithParam<tuple<int> > StackBlur_GaussianBlur;
  236. // StackBlur should produce similar results as GaussianBlur output.
  237. TEST_P(StackBlur_GaussianBlur, compare)
  238. {
  239. Mat img_ = imread(findDataFile("shared/fruits.png"), 1);
  240. const int dtype = get<0>(GetParam());
  241. Size ksize(3, 3);
  242. Mat img, dstS, dstG;
  243. convert(img_, img, dtype);
  244. stackBlur(img, dstS, ksize);
  245. GaussianBlur(img, dstG, ksize, 0);
  246. EXPECT_LE(cvtest::norm(dstS, dstG, NORM_INF), 13.);
  247. }
  248. INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_GaussianBlur, testing::Values(CV_8U, CV_16S, CV_16U, CV_32F));
  249. TEST(Imgproc_StackBlur, regression_28233)
  250. {
  251. Mat src1(1, 1, CV_8UC1, Scalar(123));
  252. Mat dst1;
  253. EXPECT_NO_THROW(stackBlur(src1, dst1, Size(9, 1)));
  254. EXPECT_EQ(dst1.at<uchar>(0, 0), 123);
  255. Mat src2(3, 3, CV_8UC1, Scalar(50));
  256. Mat dst2;
  257. EXPECT_NO_THROW(stackBlur(src2, dst2, Size(11, 11)));
  258. EXPECT_EQ(dst2.at<uchar>(1, 1), 50);
  259. }
  260. }
  261. }