Commit aba73af7 authored by Vladislav Sovrasov's avatar Vladislav Sovrasov

ximgproc: fix weightedMedianFilter crash on zero mask

parent 8b831fef
......@@ -493,7 +493,7 @@ Mat filterCore(Mat &I, Mat &F, float **wMap, int r=20, int nF=256, int nI=256, M
// Move cut-point to the left
if(balanceWeight >= 0)
{
for(;balanceWeight >= 0 && curMedianVal; curMedianVal--)
for(;balanceWeight >= 0 && curMedianVal > 0; curMedianVal--)
{
float curWeight = 0;
int *nextHist = H[curMedianVal];
......@@ -539,8 +539,13 @@ Mat filterCore(Mat &I, Mat &F, float **wMap, int r=20, int nF=256, int nI=256, M
}
// Weighted median is found and written to the output image
if(balanceWeight<0)outImg.ptr<int>(y,x)[0] = curMedianVal+1;
else outImg.ptr<int>(y,x)[0] = curMedianVal;
if(curMedianVal != -1)
{
if(balanceWeight < 0)
outImg.ptr<int>(y,x)[0] = curMedianVal+1;
else
outImg.ptr<int>(y,x)[0] = curMedianVal;
}
// Update joint-histogram and BCB when local window is shifted.
int fval,gval,*curHist;
......
......@@ -102,6 +102,16 @@ TEST(WeightedMedianFilterTest, ReferenceAccuracy)
EXPECT_LE(cvtest::norm(res, ref, NORM_L2), totalMaxError);
}
TEST(WeightedMedianFilterTest, mask_zeros_no_crash)
{
Mat img = imread(getDataDir() + "cv/ximgproc/sources/01.png");
Mat mask = Mat::zeros(img.size(), CV_8U);
Mat filtered;
weightedMedianFilter(img, img, filtered, 3, 20, WMF_EXP, mask);
EXPECT_EQ(cv::norm(img, filtered, NORM_INF), 0.0);
}
INSTANTIATE_TEST_CASE_P(TypicalSET, WeightedMedianFilterTest, Combine(Values(szODD, szQVGA), Values(WMF_EXP, WMF_IV2, WMF_OFF)));
}
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