Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
4 tasks done
damonmaria opened this issue Sep 2, 2023 · 3 comments · Fixed by #25183
Closed
4 tasks done

Comments

@damonmaria
Copy link

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)
@damonmaria damonmaria added the bug label Sep 2, 2023
@damonmaria
Copy link
Author

is because the white pixel at the bottom of the image is not connected to any other white pixels

@iknothing In all of those images generated in the code I posted there are 3 white pixels in a row, all connected, extending right from the bottom right corner. There is never a single pixel by itself.

As a simplified example:

img = np.array([
    [0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0],
], np.uint8)
print(cv.boundingRect(img))

Prints: (0, 1, 1, 1), so a width of 1.

Whereas:

img = np.array([
    [0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0],
], np.uint8)
print(cv.boundingRect(img))

Prints: (0, 2, 3, 1), which correctly has a width of 3.

@crackwitz
Copy link
Contributor

I've replicated the issue with images having shape (h,w,1). The glue logic that converts to cv::Mat seems to be unaffected.

Here's a scatterplot of those (w,h) pairs for which boundingRect() seems to fail. Perhaps the pattern means anything.

image

@crackwitz
Copy link
Contributor

crackwitz commented Sep 15, 2023

@opencv-alalek opencv-alalek added this to the 4.9.0 milestone Sep 15, 2023
@asmorkalov asmorkalov modified the milestones: 4.9.0, 4.10.0 Dec 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants