Skip to content

imgproc: accept CV_Bool masks in matchTemplate (#25895) - #29677

Merged
asmorkalov merged 3 commits into
opencv:5.xfrom
Neal006:imgproc/matchtemplate-bool-mask
Aug 11, 2026
Merged

asmorkalov merged 3 commits into
opencv:5.xfrom
Neal006:imgproc/matchtemplate-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. matchTemplateMask gates the mask at templmatch.cpp:737, so passing a boolean mask now fails with:

(-215:Assertion failed) _mask.depth() == CV_8U || _mask.depth() == CV_32F in function 'cv::matchTemplateMask'

A binary mask is a normal input for masked template matching, so this is a regression against 4.x.

Fix

Allow CV_Bool in the assertion and widen the mask to CV_8U before the existing binarization step.

The widening is required rather than passing CV_Bool straight through, because cv::threshold() does not accept CV_Bool. Once widened, the existing THRESH_BINARY path treats any non-zero entry as selected, which is exactly the documented CV_8U mask semantics. CV_8U and CV_32F masks take an unchanged path.

The masked path is the only one affected: cv::matchTemplate routes every non-empty mask through matchTemplateMask, and there is no OpenCL mask variant.

Test

Imgproc_MatchTemplateBoolMask.matches_uchar_mask compares a Mat_<bool> mask against the equivalent CV_8UC1 mask and requires the results to agree, across all six match methods (TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED) for CV_8UC1, CV_8UC3 and CV_32FC1 images. 18 parameter combinations.

Verified locally on 5.x: all 18 fail without the source change (with the assertion above) and pass with it. The full *MatchTemplate* set, 165 tests, passes. No test data needed, the test is synthetic.

Part of #25895, and follows the same approach as #29580, #29597 and #29622.

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

cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it returned CV_8U in
4.x, so matchTemplate with a boolean mask now fails the type check in
matchTemplateMask:

  (-215) _mask.depth() == CV_8U || _mask.depth() == CV_32F

Allow CV_Bool and widen it to CV_8U before the existing binarization step.
The widening is required because cv::threshold() does not accept CV_Bool;
once widened, the existing THRESH_BINARY path treats any non-zero entry as
selected, which matches the documented CV_8U mask semantics. CV_8U and
CV_32F masks are unaffected.

Added a regression test that compares a Mat_<bool> mask against the
equivalent CV_8UC1 mask across all six match methods for CV_8UC1, CV_8UC3
and CV_32FC1 images.
Copilot AI lite review requested due to automatic review settings August 8, 2026 10:56

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread modules/imgproc/src/templmatch.cpp Outdated
Comment on lines 753 to 764

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The following CV_8U case applies threshold and converts bool mask with 1 and 0 into 1.0 and 0.0. The bool path does not look efficient.

@Neal006

Neal006 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

@asmorkalov Good catch, fixed in f50c64c.

You are right that the bool path was doing redundant work. It widened the mask to CV_8U and then fell into the CV_8U branch, which ran cv::threshold() over values that were already 0 and 1 before converting them to 0.0 and 1.0.

Conversion out of CV_Bool already maps every non-zero entry to exactly 1 (DEF_CVTBOOL2_FUNC in modules/core/src/convert.simd.hpp emits (src[j] != 0) * scale), so CV_Bool now gets its own branch that converts straight to CV_32F in a single pass. The result is bit-identical to what the three-step path produced, and the CV_8U branch is untouched.

Rebuilt and reran the imgproc suite: all 18 Imgproc_MatchTemplateBoolMask cases and the other 147 tests pass. I also reconfirmed the negative test, all 18 fail against unmodified 5.x.

Address review feedback: the previous patch widened the bool mask to CV_8U and
then fell into the CV_8U branch, which applies cv::threshold() to values that
are already 0 and 1 before converting them to 0.0 and 1.0. Conversion out of
CV_Bool already maps every non-zero entry to exactly 1, so a single convertTo()
to CV_32F produces the same mask in one pass. The CV_8U branch is unchanged.
@Neal006
Neal006 force-pushed the imgproc/matchtemplate-bool-mask branch from d72a485 to f50c64c Compare August 10, 2026 11:51
@asmorkalov asmorkalov self-assigned this Aug 10, 2026
@asmorkalov
asmorkalov merged commit 042034e into opencv:5.x Aug 11, 2026
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