Commit 7cc7414a authored by Vladislav Sovrasov's avatar Vladislav Sovrasov

Fix numerical issue in guided filter

parent 1903d9fd
......@@ -424,6 +424,13 @@ void GuidedFilterImpl::ComputeCovGuideInv_ParBody::operator()(const Range& range
add_mul(det, a, ac, gf.w);
}
if (gf.eps < 1e-2)
{
for (int j = 0; j < gf.w; j++)
if (abs(det[j]) < 1e-6f)
det[j] = 1.f;
}
for (int k = 0; k < gf.covarsInv.total(); k += 1)
{
div_1x(gf.covarsInv(k).ptr<float>(i), det, gf.w);
......@@ -788,4 +795,4 @@ void guidedFilter(InputArray guide, InputArray src, OutputArray dst, int radius,
}
}
}
\ No newline at end of file
}
......@@ -387,6 +387,47 @@ TEST_P(GuidedFilterTest, accuracy)
}
}
TEST_P(GuidedFilterTest, smallParamsIssue)
{
GFParams params = GetParam();
string guideFileName = get<1>(params);
string srcFileName = get<2>(params);
int guideCnNum = 3;
int srcCnNum = get<0>(params);
Mat guide = imread(getOpenCVExtraDir() + guideFileName);
Mat src = imread(getOpenCVExtraDir() + srcFileName);
ASSERT_TRUE(!guide.empty() && !src.empty());
Size dstSize(guide.cols, guide.rows);
guide = convertTypeAndSize(guide, CV_MAKE_TYPE(guide.depth(), guideCnNum), dstSize);
src = convertTypeAndSize(src, CV_MAKE_TYPE(src.depth(), srcCnNum), dstSize);
Mat output;
ximgproc::guidedFilter(guide, src, output, 3, 1e-6);
size_t whitePixels = 0;
for(int i = 0; i < output.cols; i++)
{
for(int j = 0; j < output.rows; j++)
{
if(output.channels() == 1)
{
if(output.ptr<uchar>(i)[j] == 255)
whitePixels++;
}
else if(output.channels() == 3)
{
Vec3b currentPixel = output.ptr<Vec3b>(i)[j];
if(currentPixel == Vec3b(255, 255, 255))
whitePixels++;
}
}
}
double whiteRate = whitePixels / (double) output.total();
EXPECT_LE(whiteRate, 0.1);
}
INSTANTIATE_TEST_CASE_P(TypicalSet, GuidedFilterTest,
Combine(
Values(1, 3),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment