Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update rotatedRectangleIntersection function to calculate near to origin#19842

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
alalek merged 6 commits intoopencv:3.4fromgasparitiago:3.4
Jun 12, 2021

Conversation

@gasparitiago
Copy link
Contributor

@gasparitiagogasparitiago commentedApr 2, 2021
edited
Loading

This commit changes the rotatedRectangleIntersection function in order to calculate the intersection of two rectangles considering that they are shifted near the coordinates origin (0, 0).

This commit solves the problem in some assertions from rotatedRectangleIntersection when dealing with rectangles far from origin.

Original issue:#19824

Pull Request Readiness Checklist

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

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
force_builders=linux,docs

@gasparitiagogasparitiago changed the titleIn the function that sets the points of a RotatedRect, the typesChange type used in points function from RotatedRectApr 2, 2021
In the function that sets the points of a RotatedRect, the typesshould be double in order to keep the precision when dealing withRotatedRects that are defined far from the origin.This commit solves the problem in some assertions fromrotatedRectangleIntersection when dealing with rectangles far fromorigin.
@crackwitz
Copy link
Contributor

crackwitz commentedApr 3, 2021
edited
Loading

does this actually make a difference? it only applies double precision to the calculation itself but the result is still just FP32.

the demo code uses values like 4002326. there is simply not enough precision to go around (6-7 digits).

>>> [cx,cy,w,h,angle]=np.float32([246805.033  ,4002326.94   ,26.40587,6.20026,-62.10156])>>>_angle=angle*pi/180>>>a=np.sin(_angle)*0.5>>>b=np.cos(_angle)*0.5>>>f=np.float32;cx-f(a)*h-f(b)*w;cy+f(b)*h-f(a)*w246801.64002340.2>>>f=np.float64;cx-f(a)*h-f(b)*w;cy+f(b)*h-f(a)*w246801.593348281434002340.119037186

@gasparitiago
Copy link
ContributorAuthor

@crackwitz

does this actually make a difference?

Yes. When testing the code provided by @jbwintergest here:#19824:

import cv2 as cvimport numpy as npdef box(box):    return ((box[0], box[1]), (box[2], box[3]), box[4])box_1 = np.array([ 246805.033  , 4002326.94   ,      26.40587,       6.20026,     -62.10156])box_2 = np.array([ 246805.122  , 4002326.59   ,      27.4821 ,       8.5361 ,     -56.33761])try:    print(cv.rotatedRectangleIntersection(box(box_1), box(box_2)))  # Assertion errorexcept Exception as e:    print(e)offset = (246805, 4002326, 0, 0, 0)box_1 -= offsetbox_2 -= offsetprint(cv.rotatedRectangleIntersection(box(box_1), box(box_2)))  # that works

when the functionrotatedRectangleIntersection, creates thepts1 andpts2 arrays and populate them, like this:

    Point2f pts1[4], pts2[4];    ...    rect1.points(pts1);    rect2.points(pts2);

This is the content ofrect1,rect2,pts1[0] andpts2[0] before and after this commit:

Before:

rect1.center.x246805.03125rect1.center.y4002327rect1.size.width26.405870437622070312rect1.size.height6.200260162353515625rect1.angle-62.101558685302734375rect2.center.x246805.125rect2.center.y4002326.5rect2.size.width27.482099533081054688rect2.size.height8.5361003875732421875rect2.angle-56.33760833740234375pts1[0].x pts1[0].y246801.59375 4002340pts2[0].x pts2[0].y246801.0625 4002340.25

After:

rect1.center.x246805.03125rect1.center.y4002327rect1.size.width26.405870437622070312rect1.size.height6.200260162353515625rect1.angle-62.101558685302734375rect2.center.x246805.125rect2.center.y4002326.5rect2.size.width27.482099533081054688rect2.size.height8.5361003875732421875rect2.angle-56.33760833740234375pts1[0].x pts1[0].y246801.59375 4002340.25pts2[0].x pts2[0].y246801.0625 4002340.25

When the functionrotatedRectangleIntersection code considers if two points are the same or not, points that are too far away from the origin (high x or y) are considered overlapped in thebefore code.

@crackwitz
Copy link
Contributor

the only difference between your two texts is that pts1[0].y is completely integer in the first and has a .25 decimal part in the second:

image

the algorithm itself will likely need fixes, not just theRect::points() method

alalek reacted with thumbs up emoji

@asenyaev
Copy link
Contributor

jenkins cn please retry a build

@vpisarev
Copy link
Contributor

@gasparitiago, thank you for the contribution! I wonder if you could also modifyrotatedRectangleIntersection function to shift intersected rectangles closer to the coordinate origin (and then shift the intersection back)? This way we will get much more robust algorithm

@vpisarevvpisarev self-assigned thisJun 6, 2021
@gasparitiago
Copy link
ContributorAuthor

Hello@vpisarev, I'm doing it. When I finish I will update this PR and let you know.

To be honest I will need some help on how to test the solution.

@gasparitiago
Copy link
ContributorAuthor

@vpisarev can you take a look at my last commit?

This commit changes the rotatedRectangleIntersection function in orderto calculate the intersection of two rectangles considering that theyare shifted near the coordinates origin (0, 0).This commit solves the problem in some assertions fromrotatedRectangleIntersection when dealing with rectangles far fromorigin.
Copy link
Member

@alalekalalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you for update!

Copy link
Member

@alalekalalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks good!
Please update PR's title and description before merge.

@gasparitiagogasparitiago changed the titleChange type used in points function from RotatedRectUpdate rotatedRectangleIntersection function to calculate near to originJun 8, 2021
Copy link
Member

@alalekalalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Thank you for contribution 👍

gasparitiago reacted with hooray emoji
@alalekalalek merged commit3cf4375 intoopencv:3.4Jun 12, 2021
@alalekalalek mentioned this pull requestJun 19, 2021
@alalekalalek mentioned this pull requestOct 15, 2021
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@alalekalalekalalek approved these changes

@vpisarevvpisarevAwaiting requested review from vpisarev

Assignees

@alalekalalek

Projects

None yet

Milestone

3.4.15

Development

Successfully merging this pull request may close these issues.

6 participants

@gasparitiago@crackwitz@asenyaev@vpisarev@alalek@asmorkalov

[8]ページ先頭

©2009-2025 Movatter.jp