Skip to content

cv::connectedComponentsWithStats asserts on input mask of type CV_Bool #29593

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 both versions, cv::connectedComponentsWithStats calls connectedComponents_sub1. This function contains following assertion:

CV_Assert(iDepth == CV_8U || iDepth == CV_8S);

When passed a cv::Mat_<bool>, this worked fine in 4.x, but now asserts on this condition.

I believe this function was missed when CV_Bool was added, and the assertion should be extended to allow CV_Bool as iDepth 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 issue: #29578

Steps to reproduce

#include <opencv2/imgproc.hpp>

int main()
{
    cv::Mat1w labels;
    cv::Mat1i stats;
    cv::Mat1d centroids;
    int num_labels = 0;
    cv::Mat_<bool> mask(2,2);
    num_labels = cv::connectedComponentsWithStats(mask, labels, stats, centroids, 8, CV_16U);
}

This works with OpenCV 4.13 but asserts with OpenCV 5.0.0:

cv::Exception: OpenCV(5.0.0) .../modules/imgproc/src/connectedcomponents.cpp:5662: error: (-215:Assertion failed) iDepth == CV_8U || iDepth == CV_8S in function 'connectedComponents_sub1'

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)
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