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

rotatedRectangleIntersection does not return all intersection points as expected #23546

Closed
4 tasks done
Kroppeb opened this issue Apr 25, 2023 · 2 comments · Fixed by #23690
Closed
4 tasks done

rotatedRectangleIntersection does not return all intersection points as expected #23546

Kroppeb opened this issue Apr 25, 2023 · 2 comments · Fixed by #23690
Labels
bug category: imgproc confirmed There is stable reproducer / investigation complete
Milestone

Comments

@Kroppeb
Copy link

Kroppeb commented Apr 25, 2023

System Information

OpenCV python version: 4.7.0
Operating System / Platform: Windows 10
Python version: 3.10.10

Detailed description

During some testing of a system that used rotatedRectangleIntersection and then contourArea to get the intersection area of 2 overlapping rectangles, we got a size of ~3500mm² instead of the expected ~7000mm².

It seems to be related to the fact that one edge of each rectangle are on the same line.

  • Rectangle 1: RotatedRect(center=(824.6421183672817, 280.28737007069833), extent=(565.0, 140.0), angle=-177.80506896972656)
  • Rectangle 2: RotatedRect(center=(567.3310438828003, 270.42527355719545), extent=(275.0, 50.0), angle=92.19493103027344)

This seems related to #21659, which should be fixed by #21677, but I haven't tested that as it hasn't been released.

Steps to reproduce

import cv2 as cv

res, points = cv.rotatedRectangleIntersection(
    ((824.6421183672817, 280.28737007069833), (565.0, 140.0), -177.80506896972656),
    ((567.3310438828003, 270.42527355719545), (275.0, 50.0), 92.19493103027344),
)

assert res == 1
assert points is not None

points = points.reshape(-1, 2)
area = cv.contourArea(points)
print(area)  # expected ~7000, got ~3500

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)
@Kroppeb Kroppeb added the bug label Apr 25, 2023
@dkurt
Copy link
Member

dkurt commented May 25, 2023

Can confirm on 26a7b33 (green are rect1, rect2 and blue color is intersection):
изображение

import numpy as np
import cv2 as cv

rect1 = cv.RotatedRect((824.6421183672817, 280.28737007069833), (565.0, 140.0), -177.80506896972656)
rect2 = cv.RotatedRect((567.3310438828003, 270.42527355719545), (275.0, 50.0), 92.19493103027344)
_, inter_pts = cv.rotatedRectangleIntersection(rect1, rect2)

img = np.zeros((512, 1200, 3), dtype=np.uint8)
cv.polylines(img, rect1.points().astype(np.int32).reshape(1, 4, 2), True, (0, 255, 0))
cv.polylines(img, rect2.points().astype(np.int32).reshape(1, 4, 2), True, (0, 255, 0))
cv.polylines(img, inter_pts.astype(np.int32).reshape(1, -1, 2), True, (255, 255, 0))
cv.imwrite("img.png", img)

necessary changes with RotatedRect bindings: https://github.com/dkurt/opencv/tree/py_rotated_rect

@dkurt dkurt added the confirmed There is stable reproducer / investigation complete label May 25, 2023
@chacha21
Copy link
Contributor

I have tried to use double precision instead of float precision in imgproc.cpp:_rotatedRectangleIntersection()
I confirm that the returned area value becomes 7000.0047071566805

@dkurt dkurt linked a pull request May 26, 2023 that will close this issue
6 tasks
chacha21 added a commit to chacha21/opencv that referenced this issue May 26, 2023
Added accuracy test for opencv#23546
@asmorkalov asmorkalov added this to the 4.8.0 milestone May 30, 2023
asmorkalov pushed a commit that referenced this issue May 30, 2023
…precision

better accuracy for _rotatedRectangleIntersection() (proposal for #23546) #23690

_rotatedRectangleIntersection() can be (statically) customized to use double instead of float for better accuracy
this is a proposal for experimentation around #23546

for better accuracy, _rotatedRectangleIntersection() could use double. It will still return cv::Point2f list for backward compatibility, but the inner computations are controlled by a typedef

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
asmorkalov pushed a commit that referenced this issue Jun 22, 2023
Python binding for RotatedRect #23702

### Pull Request Readiness Checklist

related: #23546 (comment)

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue Jan 4, 2024
…ction_precision

better accuracy for _rotatedRectangleIntersection() (proposal for opencv#23546) opencv#23690

_rotatedRectangleIntersection() can be (statically) customized to use double instead of float for better accuracy
this is a proposal for experimentation around opencv#23546

for better accuracy, _rotatedRectangleIntersection() could use double. It will still return cv::Point2f list for backward compatibility, but the inner computations are controlled by a typedef

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue Jan 4, 2024
Python binding for RotatedRect opencv#23702

### Pull Request Readiness Checklist

related: opencv#23546 (comment)

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue May 29, 2024
…ction_precision

better accuracy for _rotatedRectangleIntersection() (proposal for opencv#23546) opencv#23690

_rotatedRectangleIntersection() can be (statically) customized to use double instead of float for better accuracy
this is a proposal for experimentation around opencv#23546

for better accuracy, _rotatedRectangleIntersection() could use double. It will still return cv::Point2f list for backward compatibility, but the inner computations are controlled by a typedef

- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
thewoz pushed a commit to thewoz/opencv that referenced this issue May 29, 2024
Python binding for RotatedRect opencv#23702

### Pull Request Readiness Checklist

related: opencv#23546 (comment)

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug category: imgproc confirmed There is stable reproducer / investigation complete
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants