Skip to content

cv::distanceTransform throws exception on input mask of type CV_Bool #29596

Closed

Description

@rubenvb

System Information

OpenCV version: 4.13 and 5.0.0
OS: Windows+macOS
Compilers: MSVS 2022/2026 and XCode 16.1/26.4 Clang

Detailed description

In OpenCV 4.x, cv::Mat_<bool>::depth() returns 0 (==CV_8U)
In OpenCV 5.0.0, cv::Mat_<bool>::depth() returns 9 (==CV_Bool)

In OpenCV 5.0.0, cv::distanceTransform is implemented with CALL_HAL, e.g.

CALL_HAL(distanceTransform, cv_hal_distanceTransform, src.ptr<uchar>(), src.step, dst.ptr<uchar>(), dst.step,
         src.cols, src.rows, CV_32F, distType, cv::DIST_MASK_5);

When passed a cv::Mat_<bool>, cv::distanceTransform worked fine in 4.x, but now asserts at modules/imgproc/src/distransform.cpp:1024:

CV_Assert( src.type() == CV_8UC1);

I believe this function was (and all other related functions were) missed when CV_Bool was added, and the assertion should be extended to allow CV_Bool as src.type() value.

This behaviour violates the expectation expressed in the migration guide as

CV_Bool matrices can now be used as masks wherever a CV_8U or CV_8S mask was previously accepted.

Workarounds include not using cv::Mat_<bool> as a mask type (at least for cv::connectedComponentsWithStats), which seems contrary to the intention of having a boolean datatype?
See also previous similar issues: #29578, #29593

Steps to reproduce

#include <opencv2/imgproc.hpp>

int main()
{
    cv::Mat_<bool> input(2,2);
    cv::Mat_<float> result;
    cv::distanceTransform(input, result, cv::DIST_L2, cv::DIST_MASK_PRECISE);
}

This works with OpenCV 4.13 throws an exception with OpenCV 5.0.0:

cv::Exception: OpenCV(5.0.0) .../modules/imgproc/src/distransform.cpp:1034: error: (-215:Assertion failed) src.type() == CV_8UC1 in function 'distanceTransform'

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)

Activity

  1. added this to the 5.1 milestone on Jul 25, 2026
  2. added a commit that references this issue on Jul 27, 2026
    b41cda2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions