Skip to content

video: accept CV_Bool masks in findTransformECC (#25895) - #29681

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

asmorkalov merged 3 commits into
opencv:5.xfrom
Neal006:video/ecc-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.

findTransformECCWithMask does not type check inputMask at all. It passes it straight to cv::threshold() at ecc.cpp:483, and cv::threshold() dispatches only on CV_8U, CV_16S, CV_16U, CV_32F and CV_64F, erroring otherwise. So a boolean mask fails with an error pointing at imgproc rather than at the mask:

modules/imgproc/src/thresh.cpp:1607: error: (-210:Unsupported format or combination of formats)
in function 'cv::threshold'

Fix

Widen a CV_Bool mask to CV_8U before the threshold call. The existing THRESH_BINARY step then treats any non-zero entry as selected, which matches the CV_8U mask semantics.

inputMask is used nowhere else in the function (only at ecc.cpp:476 for the empty() check and at :483), so this is the single point that needed handling. CV_8U masks take an unchanged path.

Note that cv::computeECC already accepts a boolean mask, because it only forwards the mask to countNonZero, meanStdDev and subtract, which all handle CV_Bool. This change makes findTransformECC consistent with it.

Test

Video_ECC_BoolMask.matches_uchar_mask builds a blurred checkerboard, warps it by a known translation, then runs findTransformECC with MOTION_TRANSLATION using a Mat_<bool> mask and the equivalent CV_8UC1 mask, and requires the same warp matrix from both.

Verified locally on 5.x: the test fails without the change (with the error above) and passes with it. The full *ECC* set, 15 tests, passes with OPENCV_TEST_DATA_PATH set. No test data needed for the new test itself, it 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. findTransformECC does not type check inputMask, it passes it straight
to cv::threshold(), which has no CV_Bool branch, so a boolean mask fails
with:

  thresh.cpp:1607: error: (-210:Unsupported format or combination of
  formats) in function 'cv::threshold'

Widen a CV_Bool mask to CV_8U before the threshold call. The subsequent
binarization then treats any non-zero entry as selected, matching the
CV_8U mask semantics. CV_8U masks are unaffected, and inputMask is used
nowhere else in the function.

Added a regression test that aligns a checkerboard against a translated
copy with a Mat_<bool> mask and with the equivalent CV_8UC1 mask, and
requires the same warp matrix from both.
Copilot AI lite review requested due to automatic review settings August 8, 2026 12:05

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/video/src/ecc.cpp Resolved
@Neal006

Neal006 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor Author

@asmorkalov Done in 953a178, thanks.

Exactly as you described: the mask was widened to CV_8U, pushed through cv::threshold() with the same 0 and 1 values it already had, then converted to 0.0 and 1.0. CV_Bool now converts directly to CV_32F in one step, which is what your suggestion asked for.

This is safe because conversion out of CV_Bool already normalises every non-zero entry to exactly 1 (DEF_CVTBOOL2_FUNC in modules/core/src/convert.simd.hpp emits (src[j] != 0) * scale), so the single convertTo() yields the same matrix the threshold-then-convert chain produced. The CV_8U path keeps the threshold and is unchanged.

I also dropped the now-stale comment above the test, since it described the widening step that no longer exists.

Rebuilt and reran opencv_test_video: 115 of 116 pass, including Video_ECC_BoolMask and all 8 existing ECC accuracy fixtures that exercise the CV_8U path. The single failure is vittrack.accuracy_vittrack, which needs a dnn ONNX model that this build does not include, and it is unrelated to this change. Negative test reconfirmed, the bool test throws (-210:Unsupported format) from cv::threshold against unmodified 5.x.

Address review feedback: the previous patch widened the bool mask to CV_8U,
ran cv::threshold() over values that were already 0 and 1, then converted the
result to float. Conversion out of CV_Bool already maps every non-zero entry
to exactly 1, so a single convertTo() to CV_32F produces the same matrix in
one pass instead of three. The CV_8U path is unchanged.
@Neal006
Neal006 force-pushed the video/ecc-bool-mask branch from 7f37131 to 953a178 Compare August 10, 2026 11:51
@asmorkalov asmorkalov self-assigned this Aug 10, 2026
@asmorkalov asmorkalov added this to the 5.1 milestone Aug 10, 2026
@asmorkalov
asmorkalov merged commit 8104037 into opencv:5.x Aug 11, 2026
20 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