Repository navigation
imgproc: accept CV_Bool masks in distanceTransform 🤖🤖🤖 - #29597
Merged
abhishek-gola merged 2 commits intoJul 27, 2026
Merged
Conversation
cv::Mat_<bool>::depth() returns CV_Bool in 5.0 where it returned CV_8U in 4.x, so distanceTransform started rejecting boolean masks. CV_Bool is a 1-byte type whose values are only zero-tested here, so accept it wherever a CV_8UC1 mask was previously accepted, matching the 4.x behaviour and the 4->5 migration guide. Relaxes the type checks on all four reachable paths: the public wrapper, distanceTransform_L1_8U, trueDistTrans (DIST_MASK_PRECISE) and distanceATS_L1_8u (CV_8U output). Closes opencv#29596
abhishek-gola
approved these changes
Jul 25, 2026
asmorkalov
reviewed
Jul 25, 2026
Per asmorkalov's review on opencv#29597.
asmorkalov
approved these changes
Jul 25, 2026
Contributor
Author
|
hey, i got this about the checks failing, can i get a rerun "GitHub Actions has encountered an internal error when running your job. |
biconcavelens
added a commit
to biconcavelens/opencv
that referenced
this pull request
Jul 29, 2026
Pre-empting review feedback: keeping the diff minimal, matching how opencv#29597 ended up after review (assert lines with no inline commentary).
This was referenced Aug 8, 2026
asmorkalov
pushed a commit
that referenced
this pull request
Aug 11, 2026
video: accept CV_Bool masks in findTransformECC (#25895) #29681 ### 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 - [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 - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
asmorkalov
pushed a commit
that referenced
this pull request
Aug 11, 2026
imgproc: accept CV_Bool masks in matchTemplate (#25895) - #29677 ### 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 - [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 - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.
Closes #29596.
Problem
cv::Mat_<bool>::depth()returnsCV_Boolin 5.0, where it returnedCV_8Uin 4.x.distanceTransformtherefore rejects boolean masks that worked in 4.x:This contradicts the 4→5 migration guide, which states that
CV_Boolmatrices can be used as masks wherever aCV_8UorCV_8Smask was previously accepted.Change
CV_Boolis a 1-byte type (CV_ELEM_SIZE1(CV_Bool) == 1) and every read ofsrcon these paths is a plain zero-test (if (!s[j]),srcptr[j] == 0,sbase[0] == 0 ? 0 : 255), never a use of the stored value. So aCV_Boolmask is byte-compatible and semantically identical to aCV_8UC1mask here.The type checks are relaxed to accept
CV_BoolC1, following the existing convention inaccum.cpp,histogram.cpp,homography_decomp.cpp,levmarq.cpp,ptsetreg.cppand the recently merged #29580.The issue only names the assert at
distransform.cpp:981, but there are four reachable type checks; relaxing only the reported one moves the failure deeper instead of fixing it:cv::distanceTransform(public wrapper)distanceTransform_L1_8UDIST_L1+dstType == CV_8UtrueDistTransDIST_MASK_PRECISEdistanceATS_L1_8uDIST_L1+dstType == CV_8UTesting
Added
Imgproc_DistanceTransform.bool_mask_29596, which exercises all four paths and asserts theCV_Boolresult is identical to the equivalentCV_8UC1mask result (float output forDIST_MASK_3/5/PRECISE, theCV_8Uoutput path, and the labels overload).Verified against a local build (Ninja + MinGW-w64 g++ 15.2,
BUILD_LIST=imgproc,ts,imgcodecs):distransform.cpp:981: (-215:Assertion failed) src.type() == CV_8UC1), and passes with it.opencv_test_imgprocsuite (1592 tests): no new failures. 5 pre-existing failures (Imgproc_AdaptiveThreshold.*,GaussianBlur_Bitexact.regression_9863) reproduce identically on unmodified5.xin this environment and are unrelated to this change.No
opencv_extradata is needed — the test builds its masks in-code.Authored with AI assistance, tagged per the note in the PR template. All claims above were verified empirically against a local build rather than assumed; the accompanying analysis (1-byte element size, zero-test-only usage, and the four reachable assert sites) was checked directly against the source.