Skip to content

imgproc: accept CV_Bool masks in distanceTransform 🤖🤖🤖 - #29597

Merged
abhishek-gola merged 2 commits into
opencv:5.xfrom
biconcavelens:fix/distancetransform-bool-mask
Jul 27, 2026
Merged

abhishek-gola merged 2 commits into
opencv:5.xfrom
biconcavelens:fix/distancetransform-bool-mask

Conversation

@biconcavelens

Copy link
Copy Markdown
Contributor

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • 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

Closes #29596.

Problem

cv::Mat_<bool>::depth() returns CV_Bool in 5.0, where it returned CV_8U in 4.x. distanceTransform therefore rejects boolean masks that worked in 4.x:

error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'distanceTransform'

This contradicts the 4→5 migration guide, which states that CV_Bool matrices can be used as masks wherever a CV_8U or CV_8S mask was previously accepted.

Change

CV_Bool is a 1-byte type (CV_ELEM_SIZE1(CV_Bool) == 1) and every read of src on 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 a CV_Bool mask is byte-compatible and semantically identical to a CV_8UC1 mask here.

The type checks are relaxed to accept CV_BoolC1, following the existing convention in accum.cpp, histogram.cpp, homography_decomp.cpp, levmarq.cpp, ptsetreg.cpp and 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:

Location Reached via
cv::distanceTransform (public wrapper) all calls
distanceTransform_L1_8U DIST_L1 + dstType == CV_8U
trueDistTrans DIST_MASK_PRECISE
distanceATS_L1_8u DIST_L1 + dstType == CV_8U

Testing

Added Imgproc_DistanceTransform.bool_mask_29596, which exercises all four paths and asserts the CV_Bool result is identical to the equivalent CV_8UC1 mask result (float output for DIST_MASK_3/5/PRECISE, the CV_8U output path, and the labels overload).

Verified against a local build (Ninja + MinGW-w64 g++ 15.2, BUILD_LIST=imgproc,ts,imgcodecs):

  • The new test fails without the source change, reproducing exactly the assertion from the report (distransform.cpp:981: (-215:Assertion failed) src.type() == CV_8UC1), and passes with it.
  • Full opencv_test_imgproc suite (1592 tests): no new failures. 5 pre-existing failures (Imgproc_AdaptiveThreshold.*, GaussianBlur_Bitexact.regression_9863) reproduce identically on unmodified 5.x in this environment and are unrelated to this change.

No opencv_extra data 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.

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
Comment thread modules/imgproc/src/distransform.cpp Resolved
Comment thread modules/imgproc/src/distransform.cpp Resolved
@biconcavelens

Copy link
Copy Markdown
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.
Internal server error. Correlation ID: dc93c619-2203-4ea0-9d44-c3289c203e86"

@abhishek-gola
abhishek-gola merged commit 53d308c into opencv:5.x Jul 27, 2026
47 of 81 checks passed
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).
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
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