Skip to content

boundingRect returns incorrect result for mask with some image/shape combinations #24217

@damonmaria

Description

@damonmaria

System Information

opencv-python-headless: 4.8.0.76
OS: MacOS 13.5.1
Python: 3.11.4

Detailed description

Using cv.boundingRect to find the bounding rectangle of a uint8 mask image (all 0 or 255) works in the vast majority of situations for me but returns the wrong values for some specific combinations.

Steps to reproduce

import numpy as np
import cv2 as cv

for img_w in range(3, 20):
    for img_h in range(1, 15):
        img = np.zeros((img_h, img_w), np.uint8)
        img[-1, :3] = 255
        x, y, w, h = cv.boundingRect(img)
        if w != 3:
            print(f"{img_w}x{img_h}: {w=}")

boundingRect should return w as 3 for all these combinations of img_w and img_h. But several combinations return w as 1. The other values (x, y, h) seem to be correct.

3x4: w=1
3x8: w=1
3x12: w=1
5x2: w=1
5x6: w=1
5x10: w=1
5x14: w=1
7x4: w=1
7x8: w=1
7x12: w=1
9x2: w=1
9x6: w=1
9x10: w=1
9x14: w=1
11x4: w=1
11x8: w=1
11x12: w=1
13x2: w=1
13x6: w=1
13x10: w=1
13x14: w=1
15x4: w=1
15x8: w=1
15x12: w=1
17x2: w=1
17x6: w=1
17x10: w=1
17x14: w=1
19x4: w=1
19x8: w=1
19x12: w=1

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions