Files
opencv-MIRROR/modules/imgproc/test/test_stackblur.cpp
Mohammad Sohail Shaikh c807cf9bca 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.
2026-11-11 14:44:47 +05:30

547 lines
18 KiB
C++

// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
/*
StackBlur - a fast almost Gaussian Blur
Theory: http://underdestruction.com/2004/02/25/stackblur-2004
The code has been borrowed from (https://github.com/flozz/StackBlur).
Below is the original copyright
*/
/*
Copyright (c) 2010 Mario Klingemann
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
#include "test_precomp.hpp"
namespace opencv_test { namespace {
template<typename T>
void _stackblurRef(const Mat& src, Mat& dst, Size ksize)
{
CV_Assert(!src.empty());
CV_Assert(ksize.width > 0 && ksize.height > 0 && ksize.height % 2 == 1 && ksize.width % 2 == 1);
dst.create(src.size(), src.type());
const int CN = src.channels();
int rowsImg = src.rows;
int colsImg = src.cols;
int wm = colsImg - 1;
int radiusW = ksize.width / 2;
int stackLenW = ksize.width;
const float mulW = 1.0f / (((float )radiusW + 1.0f) * ((float )radiusW + 1.0f));
// Horizontal direction
std::vector<T> stack(stackLenW * CN);
for (int row = 0; row < rowsImg; row++)
{
std::vector<float> sum(CN, 0);
std::vector<float> sumIn(CN, 0);
std::vector<float> sumOut(CN, 0);
const T* srcPtr = src.ptr<T>(row);
for (int i = 0; i <= radiusW; i++)
{
for (int ci = 0; ci < CN; ci++)
{
T tmp = *(srcPtr + ci);
stack[i * CN + ci] = tmp;
sum[ci] += tmp * (i + 1);
sumOut[ci] += tmp;
}
}
for (int i = 1; i <= radiusW; i++)
{
if (i <= wm) srcPtr += CN;
for(int ci = 0; ci < CN; ci++)
{
T tmp = *(srcPtr + ci);
stack[(i + radiusW) * CN + ci] = tmp;
sum[ci] += tmp * (radiusW + 1 - i);
sumIn[ci] += tmp;
}
}
int sp = radiusW;
int xp = radiusW ;
if (xp > wm) xp = wm;
T* dstPtr = dst.ptr<T>(row);
srcPtr = src.ptr<T>(row) + xp * CN;
int stackStart= 0;
for (int i = 0; i < colsImg; i++)
{
stackStart = sp + stackLenW - radiusW;
if (stackStart >= stackLenW) stackStart -= stackLenW;
for(int ci = 0; ci < CN; ci++)
{
*(dstPtr + ci) = cv::saturate_cast<T>(sum[ci] * mulW);
sum[ci] -= sumOut[ci];
sumOut[ci] -= stack[stackStart*CN + ci];
}
const T* srcNew = srcPtr;
if(xp < wm)
srcNew += CN;
for (int ci = 0; ci < CN; ci++)
{
stack[stackStart * CN + ci] = *(srcNew + ci);
sumIn[ci] += *(srcNew + ci);
sum[ci] += sumIn[ci];
}
int sp1 = sp + 1;
if (sp1 >= stackLenW)
sp1 = 0;
for(int ci = 0; ci < CN; ci++)
{
T tmp = stack[sp1*CN + ci];
sumOut[ci] += tmp;
sumIn[ci] -= tmp;
}
dstPtr += CN;
if (xp < wm)
{
xp++;
srcPtr += CN;
}
++sp;
if (sp >= stackLenW)
sp = 0;
}
}
// Vertical direction
int hm = rowsImg - 1;
int widthElem = colsImg * CN;
int radiusH = ksize.height / 2;
int stackLenH = ksize.height;
const float mulH = 1.0f / (((float )radiusH + 1.0f) * ((float )radiusH + 1.0f));
stack.resize(stackLenH, 0);
for (int col = 0; col < widthElem; col++)
{
const T* srcPtr =dst.ptr<T>() + col;
float sum0 = 0;
float sumIn0 = 0;
float sumOut0 = 0;
for (int i = 0; i <= radiusH; i++)
{
T tmp = (T)(*srcPtr);
stack[i] = tmp;
sum0 += tmp * (i + 1);
sumOut0 += tmp;
}
for (int i = 1; i <= radiusH; i++)
{
if (i <= hm) srcPtr += widthElem;
T tmp = (T)(*srcPtr);
stack[i + radiusH] = tmp;
sum0 += tmp * (radiusH - i + 1);
sumIn0 += tmp;
}
int sp = radiusH;
int yp = radiusH;
if (yp > hm) yp = hm;
T* dstPtr = dst.ptr<T>() + col;
srcPtr = dst.ptr<T>(yp) + col;
const T* srcNew;
int stackStart = 0;
for (int i = 0; i < rowsImg; i++)
{
stackStart = sp + stackLenH - radiusH;
if (stackStart >= stackLenH) stackStart -= stackLenH;
*(dstPtr) = saturate_cast<T>(sum0 * mulH);
sum0 -= sumOut0;
sumOut0 -= stack[stackStart];
srcNew = srcPtr;
if (yp < hm)
srcNew += widthElem;
stack[stackStart] = *(srcNew);
sumIn0 += *(srcNew);
sum0 += sumIn0;
int sp1 = sp + 1;
sp1 &= -(sp1 < stackLenH);
sumOut0 += stack[sp1];
sumIn0 -= stack[sp1];
dstPtr += widthElem;
if (yp < hm)
{
yp++;
srcPtr += widthElem;
}
++sp;
if (sp >= stackLenH) sp = 0;
}
}
}
void stackBlurRef(const Mat& img, Mat& dst, Size ksize)
{
if(img.depth() == CV_8U)
_stackblurRef<uchar>(img, dst, ksize);
else if (img.depth() == CV_16S)
_stackblurRef<short>(img, dst, ksize);
else if (img.depth() == CV_16U)
_stackblurRef<ushort>(img, dst, ksize);
else if (img.depth() == CV_32F)
_stackblurRef<float>(img, dst, ksize);
else
CV_Error(Error::StsNotImplemented,
("Unsupported Mat type in stackBlurRef, "
"the supported formats are: CV_8U, CV_16U, CV_16S and CV_32F."));
}
std::vector<Size> kernelSizeVec = {
Size(3, 3),
Size(5, 5),
Size(101, 101),
Size(3, 9)
};
typedef testing::TestWithParam<tuple<int, int, int> > StackBlur;
TEST_P (StackBlur, regression)
{
Mat img_ = imread(findDataFile("shared/fruits.png"), 1);
const int cn = get<0>(GetParam());
const int kIndex = get<1>(GetParam());
const int dtype = get<2>(GetParam());
Size ksize = kernelSizeVec[kIndex];
Mat img, dstRef, dst;
convert(img_, img, dtype);
vector<Mat> channels;
split(img, channels);
channels.push_back(channels[0]); // channels size is 4.
Mat imgCn;
if (cn == 1)
imgCn = channels[0];
else if (cn == 4)
merge(channels, imgCn);
else
imgCn = img;
stackBlurRef(imgCn, dstRef, ksize);
stackBlur(imgCn, dst, ksize);
EXPECT_LE(cvtest::norm(dstRef, dst, NORM_INF), 2.);
}
INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur,
testing::Combine(
testing::Values(1, 3, 4),
testing::Values(0, 1, 2, 3),
testing::Values(CV_8U, CV_16S, CV_16U, CV_32F)
)
);
typedef testing::TestWithParam<tuple<int> > StackBlur_GaussianBlur;
// StackBlur should produce similar results as GaussianBlur output.
TEST_P(StackBlur_GaussianBlur, compare)
{
Mat img_ = imread(findDataFile("shared/fruits.png"), 1);
const int dtype = get<0>(GetParam());
Size ksize(3, 3);
Mat img, dstS, dstG;
convert(img_, img, dtype);
stackBlur(img, dstS, ksize);
GaussianBlur(img, dstG, ksize, 0);
EXPECT_LE(cvtest::norm(dstS, dstG, NORM_INF), 13.);
}
INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_GaussianBlur, testing::Values(CV_8U, CV_16S, CV_16U, CV_32F));
TEST(Imgproc_StackBlur, regression_28233)
{
Mat src1(1, 1, CV_8UC1, Scalar(123));
Mat dst1;
EXPECT_NO_THROW(stackBlur(src1, dst1, Size(9, 1)));
EXPECT_EQ(dst1.at<uchar>(0, 0), 123);
Mat src2(3, 3, CV_8UC1, Scalar(50));
Mat dst2;
EXPECT_NO_THROW(stackBlur(src2, dst2, Size(11, 11)));
EXPECT_EQ(dst2.at<uchar>(1, 1), 50);
}
typedef testing::TestWithParam<tuple<int, int, int> > StackBlur_FullRange;
// A constant image stays constant, however large its values, and full-range noise matches the reference.
TEST_P(StackBlur_FullRange, regression)
{
const int depth = get<0>(GetParam());
const int cn = get<1>(GetParam());
const int kernel = get<2>(GetParam());
const Size ksize(kernel, kernel);
const double maxVal = depth == CV_16U ? 65535 : 32767;
const double minVal = depth == CV_16U ? 0 : -32768;
for (double value : {maxVal, minVal, maxVal / 2})
{
Mat constant(64, 96, CV_MAKETYPE(depth, cn), Scalar::all(value)), dst;
stackBlur(constant, dst, ksize);
EXPECT_LE(cvtest::norm(constant, dst, NORM_INF), 1.) << "value=" << value;
}
// stackBlurRef accumulates in float, which drifts at this range, so the reference is the
// triangular kernel stackBlur stands for, computed in double.
const int radius = kernel / 2;
Mat tri(kernel, 1, CV_64F);
for (int i = 0; i < kernel; i++)
tri.at<double>(i) = (radius + 1 - std::abs(i - radius)) / double((radius + 1) * (radius + 1));
Mat noise(64, 96, CV_MAKETYPE(depth, cn)), dst, ref, dst64;
randu(noise, Scalar::all(minVal), Scalar::all(maxVal));
sepFilter2D(noise, ref, CV_64F, tri, tri, Point(-1, -1), 0, BORDER_REPLICATE);
stackBlur(noise, dst, ksize);
dst.convertTo(dst64, CV_64F);
const Rect inner(radius, radius, noise.cols - 2 * radius, noise.rows - 2 * radius);
EXPECT_LE(cvtest::norm(ref(inner), dst64(inner), NORM_INF), 2.);
}
INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_FullRange,
testing::Combine(
testing::Values(CV_16U, CV_16S),
testing::Values(1, 3, 4),
testing::Values(3, 5, 7, 15, 51)
)
);
typedef testing::TestWithParam<tuple<int, int> > StackBlur_LargeKernel;
// The sum no longer fits 32 bits from radius 181 for CV_16U and radius 255 for CV_16S.
TEST_P(StackBlur_LargeKernel, regression)
{
const int depth = get<0>(GetParam());
const int kernel = get<1>(GetParam());
const double maxVal = depth == CV_16U ? 65535 : 32767;
const double minVal = depth == CV_16U ? 0 : -32768;
const int side = kernel + 32;
for (double value : {maxVal, minVal, maxVal / 2})
{
Mat constant(side, side, CV_MAKETYPE(depth, 3), Scalar::all(value)), dst;
for (const Size& ksize : {Size(kernel, kernel), Size(kernel, 1), Size(1, kernel)})
{
stackBlur(constant, dst, ksize);
EXPECT_LE(cvtest::norm(constant, dst, NORM_INF), 1.) << "value=" << value << " ksize=" << ksize;
}
}
const int radius = kernel / 2;
Mat tri(kernel, 1, CV_64F);
for (int i = 0; i < kernel; i++)
tri.at<double>(i) = (radius + 1 - std::abs(i - radius)) / double((radius + 1) * (radius + 1));
Mat noise(side, side, depth), dst, ref, dst64;
randu(noise, Scalar::all(minVal), Scalar::all(maxVal));
sepFilter2D(noise, ref, CV_64F, tri, tri, Point(-1, -1), 0, BORDER_REPLICATE);
stackBlur(noise, dst, Size(kernel, kernel));
dst.convertTo(dst64, CV_64F);
const Rect inner(radius, radius, noise.cols - 2 * radius, noise.rows - 2 * radius);
EXPECT_LE(cvtest::norm(ref(inner), dst64(inner), NORM_INF), 2.);
}
INSTANTIATE_TEST_CASE_P(Imgproc, StackBlur_LargeKernel,
testing::Combine(
testing::Values(CV_16U, CV_16S),
testing::Values(361, 363, 509, 511, 513)
)
);
// For CV_8U the sum no longer fits 32 bits from radius 2901.
TEST(Imgproc_StackBlur, large_kernel_8u)
{
for (int kernel : {5801, 5803})
{
const int radius = kernel / 2;
for (double value : {255., 128.})
{
Mat row(3, kernel + 32, CV_8UC1, Scalar::all(value)), col(kernel + 32, 3, CV_8UC1, Scalar::all(value)), dst;
stackBlur(row, dst, Size(kernel, 1));
EXPECT_LE(cvtest::norm(row, dst, NORM_INF), 1.) << "value=" << value << " kernel=" << kernel;
stackBlur(col, dst, Size(1, kernel));
EXPECT_LE(cvtest::norm(col, dst, NORM_INF), 1.) << "value=" << value << " kernel=" << kernel;
}
Mat tri(1, kernel, CV_64F), one(1, 1, CV_64F, Scalar::all(1.));
for (int i = 0; i < kernel; i++)
tri.at<double>(i) = (radius + 1 - std::abs(i - radius)) / double((radius + 1) * (radius + 1));
Mat noise(3, 2 * kernel, CV_8UC1), dst, ref, dst64;
randu(noise, Scalar::all(0), Scalar::all(256));
sepFilter2D(noise, ref, CV_64F, tri, one, Point(-1, -1), 0, BORDER_REPLICATE);
stackBlur(noise, dst, Size(kernel, 1));
dst.convertTo(dst64, CV_64F);
const Rect inner(radius, 0, noise.cols - 2 * radius, noise.rows);
EXPECT_LE(cvtest::norm(ref(inner), dst64(inner), NORM_INF), 1.) << "kernel=" << kernel;
}
}
// (radius + 1)^2 itself doesn't fit 32 bits here.
TEST(Imgproc_StackBlur, full_range_huge_kernel)
{
const int kernel = 92681;
for (int depth : {CV_16U, CV_16S})
{
const double value = depth == CV_16U ? 65535 : -32768;
Mat constant(1, kernel + 2, depth, Scalar::all(value)), dst;
stackBlur(constant, dst, Size(kernel, 1));
EXPECT_LE(cvtest::norm(constant, dst, NORM_INF), 1.) << "depth=" << depth;
}
}
class ScopedThreadsGuard
{
public:
ScopedThreadsGuard() : old_threads(getNumThreads()) {}
~ScopedThreadsGuard() { setNumThreads(old_threads); }
void set(int n) { setNumThreads(n); }
private:
int old_threads;
};
// The result doesn't depend on how the image is split between threads. The width is
// odd so that some stripe leaves pixels to the scalar loop at any thread count.
TEST(Imgproc_StackBlur, full_range_thread_invariance)
{
Mat noise(120, 161, CV_16UC3), single, multi;
randu(noise, Scalar::all(0), Scalar::all(65535));
ScopedThreadsGuard threadsGuard;
const int threads = std::max(2, getNumThreads());
threadsGuard.set(1);
stackBlur(noise, single, Size(5, 5));
threadsGuard.set(threads);
stackBlur(noise, multi, Size(5, 5));
EXPECT_EQ(cvtest::norm(single, multi, NORM_INF), 0.);
}
typedef testing::TestWithParam<tuple<int, int, Size> > 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<tuple<int, int, Size, bool> > 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()
)
);
}
}