From 37ca0308a773a6504d3fceeb1efe6bf3c014a414 Mon Sep 17 00:00:00 2001 From: Linaname <30354029+Linaname@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:26:56 +0500 Subject: [PATCH] Merge pull request #25402 from Linaname:4.x Handle top and left border masked pixels correctly in inpaint method #25402 Fixes #25389 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/photo/src/inpaint.cpp | 8 ++++---- modules/photo/test/test_inpaint.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/modules/photo/src/inpaint.cpp b/modules/photo/src/inpaint.cpp index d2d495e57e..d9a7dbc58b 100644 --- a/modules/photo/src/inpaint.cpp +++ b/modules/photo/src/inpaint.cpp @@ -340,7 +340,7 @@ icvTeleaInpaintFMM(const CvMat *f, CvMat *t, CvMat *out, int range, CvPriorityQu gradI.y=0; } } - Ia[color] += (float)w * (float)(CV_MAT_3COLOR_ELEM(*out,uchar,km,lm,color)); + Ia[color] += (float)w * (float)(CV_MAT_3COLOR_ELEM(*out,uchar,k-1,l-1,color)); Jx[color] -= (float)w * (float)(gradI.x*r.x); Jy[color] -= (float)w * (float)(gradI.y*r.y); s[color] += w; @@ -452,7 +452,7 @@ icvTeleaInpaintFMM(const CvMat *f, CvMat *t, CvMat *out, int range, CvPriorityQu gradI.y=0; } } - Ia += (float)w * (float)(CV_MAT_ELEM(*out,data_type,km,lm)); + Ia += (float)w * (float)(CV_MAT_ELEM(*out,data_type,k-1,l-1)); Jx -= (float)w * (float)(gradI.x*r.x); Jy -= (float)w * (float)(gradI.y*r.y); s += w; @@ -555,7 +555,7 @@ icvNSInpaintFMM(const CvMat *f, CvMat *t, CvMat *out, int range, CvPriorityQueue dir = (float)fabs(VectorScalMult(r,gradI)/sqrt(VectorLength(r)*VectorLength(gradI))); } w = dst*dir; - Ia[color] += (float)w * (float)(CV_MAT_3COLOR_ELEM(*out,uchar,km,lm,color)); + Ia[color] += (float)w * (float)(CV_MAT_3COLOR_ELEM(*out,uchar,k-1,l-1,color)); s[color] += w; } } @@ -645,7 +645,7 @@ icvNSInpaintFMM(const CvMat *f, CvMat *t, CvMat *out, int range, CvPriorityQueue dir = (float)fabs(VectorScalMult(r,gradI)/sqrt(VectorLength(r)*VectorLength(gradI))); } w = dst*dir; - Ia += (float)w * (float)(CV_MAT_ELEM(*out,data_type,km,lm)); + Ia += (float)w * (float)(CV_MAT_ELEM(*out,data_type,k-1,l-1)); s += w; } } diff --git a/modules/photo/test/test_inpaint.cpp b/modules/photo/test/test_inpaint.cpp index 1536490693..75bab57737 100644 --- a/modules/photo/test/test_inpaint.cpp +++ b/modules/photo/test/test_inpaint.cpp @@ -157,4 +157,30 @@ TEST(Photo_InpaintBorders, regression) ASSERT_TRUE(countNonZero(diff) == 0); } +typedef testing::TestWithParam> Photo_InpaintSmallBorders; + +TEST_P(Photo_InpaintSmallBorders, regression) +{ + int type = get<0>(GetParam()); + Mat img(5, 5, type, Scalar::all(128)); + Mat expected = img.clone(); + + Mat mask = Mat::zeros(5, 5, CV_8U); + mask(Rect(1, 1, 3, 3)) = 255; + + img.setTo(Scalar::all(0), mask); + + Mat inpainted, diff; + + inpaint(img, mask, inpainted, 1, INPAINT_TELEA); + cv::absdiff(inpainted, expected, diff); + ASSERT_EQ(countNonZero(diff.reshape(1)), 0); + + inpaint(img, mask, inpainted, 1, INPAINT_NS); + cv::absdiff(inpainted, expected, diff); + ASSERT_EQ(countNonZero(diff.reshape(1)), 0); +} + +INSTANTIATE_TEST_CASE_P(/*nothing*/, Photo_InpaintSmallBorders, Values(CV_8UC1, CV_8UC3)); + }} // namespace