Skip to content

photo: accept CV_Bool masks in inpaint (#25895) - #29676

Merged
asmorkalov merged 1 commit into
opencv:5.xfrom
Neal006:photo/inpaint-bool-mask
Aug 11, 2026
Merged

asmorkalov merged 1 commit into
opencv:5.xfrom
Neal006:photo/inpaint-bool-mask

Conversation

@Neal006

@Neal006 Neal006 commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Problem

cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it returned CV_8U in 4.x. inpaint.cpp:712 gates the mask on inpaint_mask.type() != CV_8UC1, so a boolean mask that worked in 4.x now fails in 5.0 with:

(-210:Unsupported format or combination of formats) The mask must be 8-bit 1-channel image
in function 'icvInpaint'

A binary mask is the natural input for inpainting, so this is a usability regression against 4.x.

Fix

Allow CV_BoolC1 in the type check, and update the message accordingly.

No conversion is needed. COPY_MASK_BORDER1_C1 and the FMM helpers read the mask with Mat::at<uchar>, and Mat::at only asserts that the element size matches (CV_ELEM_SIZE1(traits::Depth<_Tp>::value) == elemSize1()). CV_Bool is one byte like CV_8U, so those reads are already correct for a boolean mask.

Test

Photo_InpaintBoolMask.accepts_bool_mask runs the same mask as Mat_<bool> and as CV_8UC1 through both INPAINT_TELEA and INPAINT_NS, over CV_8UC1 and CV_8UC3 images, 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 with OPENCV_TEST_DATA_PATH set. 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_8UC1 with convertTo, and justified it by claiming that Mat::at<uchar> would otherwise trip a type assert in debug builds. That justification was wrong. Mat::at checks element size, not depth, and CV_Bool is 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

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • 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

Copilot AI lite review requested due to automatic review settings August 8, 2026 10:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_BoolC1 inpaint masks to CV_8UC1 before 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.

@Neal006 Neal006 changed the title photo: accept CV_Bool masks in inpaint (#25895) 🤖🤖🤖 photo: accept CV_Bool masks in inpaint (#25895) Aug 8, 2026
@Neal006
Neal006 requested a lite review from Copilot August 8, 2026 10:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 inpaintMask must 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 in modules/photo/include/opencv2/photo.hpp to mention CV_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" );

@Neal006
Neal006 force-pushed the photo/inpaint-bool-mask branch from 9931f32 to 2f37ec1 Compare August 8, 2026 11:12
@Neal006

Neal006 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Updated the patch and the description.

The first revision converted the mask with convertTo and justified it by claiming Mat::at<uchar> would otherwise trip a type assert in debug builds. That was wrong: Mat::at asserts on element size, not depth, and CV_Bool is one byte like CV_8U, so the existing reads were already correct.

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.

Comment thread modules/photo/src/inpaint.cpp Resolved
Comment thread modules/photo/test/test_inpaint.cpp Resolved
Comment thread modules/photo/test/test_inpaint.cpp Resolved
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.
@Neal006
Neal006 force-pushed the photo/inpaint-bool-mask branch from 2f37ec1 to a23985f Compare August 10, 2026 17:36
@Neal006

Neal006 commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@asmorkalov All three addressed in a23985f, force-pushed so the PR is still a single commit.

  1. Removed the redundant comment above the type check in inpaint.cpp.
  2. Removed the redundant comments above the test.
  3. Extracted the inpainting algorithm as a test parameter using CV_ENUM, following modules/imgproc/test/test_templmatchmask.cpp. The inner for (int flag : ...) loop is gone, so a failure now points at one specific combination instead of one of two iterations, and the enum name prints properly:
Photo_InpaintBoolMask.accepts_bool_mask/0, where GetParam() = (8UC1, cv::INPAINT_NS)
Photo_InpaintBoolMask.accepts_bool_mask/1, where GetParam() = (8UC1, cv::INPAINT_TELEA)
Photo_InpaintBoolMask.accepts_bool_mask/2, where GetParam() = (8UC3, cv::INPAINT_NS)
Photo_InpaintBoolMask.accepts_bool_mask/3, where GetParam() = (8UC3, cv::INPAINT_TELEA)

Rebuilt and reran: all 4 cases pass, and the negative test is reconfirmed, all 4 fail against unmodified 5.x with (-210) The mask must be 8-bit 1-channel image from icvInpaint. The remaining 7 failures in opencv_test_photo are all ChromaticAberrationTest and I confirmed they fail identically without this change.

@asmorkalov asmorkalov self-assigned this Aug 11, 2026
@asmorkalov
asmorkalov merged commit d9d551e into opencv:5.x Aug 11, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants