From c807cf9bcae5b8dce9e38dfaee116ecf423ed209 Mon Sep 17 00:00:00 2001 From: Mohammad Sohail Shaikh <122166523+sohailshk@users.noreply.github.com> Date: Wed, 11 Nov 2026 14:44:47 +0530 Subject: [PATCH] imgproc: fix stackBlur on sub-matrices and in place (#30739) * imgproc: fix stackBlur on sub-matrices and in place The vertical pass stepped between rows by the row width, so a destination that is a region of a larger image was read and written at the wrong rows, and pixels outside the region were overwritten. Step by the real row stride instead. The horizontal pass with a kernel of 9 or fewer pixels reads pixels left of the one it writes, so stackBlur(img, img, ksize) blurred already blurred values. Copy the row first when the blur runs in place. * imgproc: limit the stackBlur in-place copy, parameterize its tests In place, only the row pass for kernels up to 9 pixels wide reads pixels it has already written. A kernel width of 1 skips the row pass, the row branch for wider kernels reads the whole row before writing it, and the column pass always works in place on the destination, so these calls no longer copy the image. Split the sub-matrix and in-place test into two TEST_P tests over depth, channels and kernel size. --- modules/imgproc/src/stackblur.cpp | 14 ++++-- modules/imgproc/test/test_stackblur.cpp | 63 +++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 5 deletions(-) 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() + ) +); } }