Skip to content

cv::boundingRect called with cv::Mat_<bool> throws exception in 5.0.0, works in 4.x #29578

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::boundingRect is implemented as:

return m.depth() <= CV_8U ? maskBoundingRect(m) : pointSetBoundingRect(m);

When passed a cv::Mat_<bool>, this worked fine in 4.x, but now throws an exception from pointSetBoundingRect that depth needs to be CV_32F or CV_32S.

I believe this function was missed when CV_Bool was added, and it should follow (conceptually) the maskBoundingRect path. Unfortunately, that function seems to assume 1 byte wide element type so it will need a specific CV_Bool implementation.

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::boundingRect, which seems contrary to the intention of having a boolean datatype?

Steps to reproduce

#include <opencv2/imgproc.hpp>
#if CV_VERSION_MAJOR > 4
#include <opencv2/geometry/2d.hpp>
#endif

int main()
{
    cv::Mat_<bool> m(2,2);
    [[maybe_unused]] const auto rect = cv::boundingRect(m);
}

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

.../modules/geometry/src/geometry.cpp:599: error: (-215:Assertion failed) npoints >= 0 && (depth == CV_32F || depth == CV_32S) in function 'pointSetBoundingRect'

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 23, 2026
  2. rubenvb commented on Jul 24, 2026

    @rubenvb
    Author

    The proposed fix seems to work for our codebase.

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