diff --git a/modules/imgproc/src/stackblur.cpp b/modules/imgproc/src/stackblur.cpp index e0a1be1f58..32cf9cd951 100644 --- a/modules/imgproc/src/stackblur.cpp +++ b/modules/imgproc/src/stackblur.cpp @@ -1081,7 +1081,7 @@ public: ParallelStackBlurColumn (const Mat & _src, Mat &_dst, int _radius):src(_src), dst(_dst) ,radius(_radius) { CN = src.channels(); - widthElem = CN * src.cols; + rowStep = (int)dst.step1(); height = src.rows; hm = src.rows - 1; mulVal = 1.0f / ((float)(radius + 1) * (radius + 1)); @@ -1134,7 +1134,7 @@ public: for (int i = 1; i <= radius; i++) { - if (i <= hm) srcPtr += widthElem; + if (i <= hm) srcPtr += rowStep; for (int k = 0; k < widthLen; k++) { T tmp = *(srcPtr + k); @@ -1165,7 +1165,7 @@ public: if (yp < hm) { yp++; - srcPtr += widthElem; + srcPtr += rowStep; } int k = 0; @@ -1188,7 +1188,7 @@ public: sumIn[k] -= stack[sp1 * widthLen + k]; } - dstPtr += widthElem; + dstPtr += rowStep; ++sp; if (sp >= kernelSize) sp = 0; @@ -1201,7 +1201,7 @@ private: int radius; int CN; int height; - int widthElem; + int rowStep; int hm; float mulVal; int mulValTab; @@ -1268,6 +1268,10 @@ void stackBlur(InputArray _src, OutputArray _dst, Size ksize) } Mat dst = _dst.getMat(); + // In place, the row pass for kernels up to 9 wide reads pixels it has already written. + // The other row branch and the column pass are safe in place. + if (src.data == dst.data && ksize.width > 1 && ksize.width <= 9 && src.cols > ksize.width) + src = src.clone(); int numOfThreads = getNumThreads(); int widthElem = src.cols * src.channels(); diff --git a/modules/imgproc/test/test_stackblur.cpp b/modules/imgproc/test/test_stackblur.cpp index 6f96de69d2..76daff78ff 100644 --- a/modules/imgproc/test/test_stackblur.cpp +++ b/modules/imgproc/test/test_stackblur.cpp @@ -479,5 +479,68 @@ TEST(Imgproc_StackBlur, full_range_thread_invariance) EXPECT_EQ(cvtest::norm(single, multi, NORM_INF), 0.); } + +typedef testing::TestWithParam > StackBlur_SubmatrixDst; + +// The destination is a region of a larger image: it gets the blur of a continuous copy of +// the source, and the pixels around it don't change. +TEST_P(StackBlur_SubmatrixDst, regression) +{ + const int type = CV_MAKETYPE(get<0>(GetParam()), get<1>(GetParam())); + const Size ksize = get<2>(GetParam()); + const Rect roi(3, 2, 17, 11); + + Mat srcParent(roi.height + 6, roi.width + 9, type); + randu(srcParent, Scalar::all(0), Scalar::all(200)); + Mat src = srcParent(roi), blurred; + stackBlur(src.clone(), blurred, ksize); + + Mat dstParent(srcParent.size(), type, Scalar::all(77)); + Mat expected = dstParent.clone(); + blurred.copyTo(expected(roi)); + Mat dst = dstParent(roi); + stackBlur(src, dst, ksize); + EXPECT_EQ(cvtest::norm(expected, dstParent, NORM_INF), 0.); +} + +INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_SubmatrixDst, + testing::Combine( + testing::Values(CV_8U, CV_16S, CV_16U, CV_32F), + testing::Values(1, 3), + testing::Values(Size(3, 3), Size(5, 7), Size(1, 5), Size(5, 1), Size(11, 5)) + ) +); + +typedef testing::TestWithParam > StackBlur_InPlace; + +// In place, the result equals the blur of a separate copy, also when the image is a region of +// a larger one, whose other pixels don't change. +TEST_P(StackBlur_InPlace, regression) +{ + const int type = CV_MAKETYPE(get<0>(GetParam()), get<1>(GetParam())); + const Size ksize = get<2>(GetParam()); + const bool useRoi = get<3>(GetParam()); + + Mat parent(17, 26, type); + randu(parent, Scalar::all(0), Scalar::all(200)); + const Rect roi = useRoi ? Rect(3, 2, 17, 11) : Rect(Point(), parent.size()); + Mat blurred; + stackBlur(parent(roi).clone(), blurred, ksize); + Mat expected = parent.clone(); + blurred.copyTo(expected(roi)); + + Mat img = parent(roi); + stackBlur(img, img, ksize); + EXPECT_EQ(cvtest::norm(expected, parent, NORM_INF), 0.); +} + +INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_InPlace, + testing::Combine( + testing::Values(CV_8U, CV_16S, CV_16U, CV_32F), + testing::Values(1, 3), + testing::Values(Size(3, 3), Size(5, 7), Size(1, 5), Size(5, 1), Size(11, 5)), + testing::Bool() + ) +); } }