Repository navigation
photo: accept CV_Bool masks in inpaint (#25895) - #29676
Conversation
There was a problem hiding this comment.
Pull request overview
This PR restores compatibility for boolean inpainting masks in OpenCV 5.x by converting CV_BoolC1 masks to CV_8UC1 inside cv::inpaint, preventing a usability regression from OpenCV 4.x where cv::Mat_<bool> masks previously worked.
Changes:
- Normalize
CV_BoolC1inpaint masks toCV_8UC1before calling the internal inpaint implementation. - Update the inpaint mask type error message to reflect that boolean masks are accepted at the public API level.
- Add a regression test ensuring boolean and 8-bit masks produce byte-identical results for both inpaint algorithms and for 1- and 3-channel inputs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| modules/photo/src/inpaint.cpp | Converts CV_BoolC1 masks to CV_8UC1 before invoking icvInpaint, and updates the mask-type error message. |
| modules/photo/test/test_inpaint.cpp | Adds a parametrized regression test validating identical outputs for Mat_<bool> vs CV_8UC1 masks across flags and image types. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Suppressed comments (1)
modules/photo/src/inpaint.cpp:713
- The public API documentation for cv::inpaint still states that
inpaintMaskmust be an “8-bit 1-channel image”, but this change (and the updated error message here) now accepts boolean masks as well. This mismatch can confuse API users and downstream bindings; please update the doc comment inmodules/photo/include/opencv2/photo.hppto mentionCV_BoolC1(and that non-zero/true pixels indicate the region to inpaint).
if( inpaint_mask.type() != CV_8UC1 )
CV_Error( cv::Error::StsUnsupportedFormat, "The mask must be 8-bit or boolean 1-channel image" );
9931f32 to
2f37ec1
Compare
|
Updated the patch and the description. The first revision converted the mask with The patch is now just the type check relaxation, which drops an unnecessary mask copy. Re-verified: the regression test fails against unmodified 5.x and passes with the change. |
cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it was CV_8U in 4.x, so code that passed a boolean mask to inpaint() stopped working on the 5.x branch. icvInpaint() rejected anything that was not CV_8UC1, even though CV_Bool is a single byte like CV_8U and the uchar reads in the fast marching code are already correct for it. Relaxing the type gate is therefore sufficient, no conversion of the mask is needed. The regression test checks a boolean mask against the equivalent CV_8U mask and requires both the output type and every pixel to match, for both inpainting algorithms and for 1 and 3 channel input. Verified locally: the test fails against unmodified 5.x with "The mask must be 8-bit 1-channel image" and passes with the change. The rest of opencv_test_photo is unaffected.
2f37ec1 to
a23985f
Compare
|
@asmorkalov All three addressed in a23985f, force-pushed so the PR is still a single commit.
Rebuilt and reran: all 4 cases pass, and the negative test is reconfirmed, all 4 fail against unmodified 5.x with |
Problem
cv::Mat_<bool>::depth()returnsCV_Boolin 5.0, where it returnedCV_8Uin 4.x.inpaint.cpp:712gates the mask oninpaint_mask.type() != CV_8UC1, so a boolean mask that worked in 4.x now fails in 5.0 with:A binary mask is the natural input for inpainting, so this is a usability regression against 4.x.
Fix
Allow
CV_BoolC1in the type check, and update the message accordingly.No conversion is needed.
COPY_MASK_BORDER1_C1and the FMM helpers read the mask withMat::at<uchar>, andMat::atonly asserts that the element size matches (CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1()).CV_Boolis one byte likeCV_8U, so those reads are already correct for a boolean mask.Test
Photo_InpaintBoolMask.accepts_bool_maskruns the same mask asMat_<bool>and asCV_8UC1through bothINPAINT_TELEAandINPAINT_NS, overCV_8UC1andCV_8UC3images, and requires identical output.Verified locally on 5.x: both parameter cases fail without the change (throwing the error above) and pass with it. The full
opencv_test_photo*Inpaint*set, 10 tests, passes withOPENCV_TEST_DATA_PATHset. No test data is needed for the new test itself, it is synthetic.Part of #25895, and follows the same approach as #29580, #29597 and #29622.
Note on an earlier revision of this PR: the first version converted the mask to
CV_8UC1withconvertTo, and justified it by claiming thatMat::at<uchar>would otherwise trip a type assert in debug builds. That justification was wrong.Mat::atchecks element size, not depth, andCV_Boolis one byte, so the conversion was unnecessary. The patch has been simplified to just relax the type check, which removes a needless mask copy.Pull Request Readiness Checklist
Patch to opencv_extra has the same branch name.