Skip to content

RotatedRect fails to calculate correct intersections due to precision #12221

@NCBee

Description

@NCBee
System information (version)
  • OpenCV => master
  • Operating System / Platform => Linux 64 Bit
  • Compiler => gcc
Detailed description

OpenCV RotatedRect intersection calculation currently uses float for intermediate variables. In some edge cases, the limited precision causes assertion problems inside intersection code:

   CV_Assert( intersection.size() <= 8 );

The problem can be mitigated by doing all calculations in double precision as shown below:

      float A = -vec2[j].y;
      float B = vec2[j].x;
      float C = -(A * pts2[j].x + B * pts2[j].y);

      float s = A * x + B * y + C;

//Using float: xx = -1990.670654, yy = 1636.783203,A = 200.753662, B = -121.916260, C = 599184.937500, x = -2071.253906, y = 1504.090820, s = 0.000000

      double A = -vec2[j].y;
      double B = vec2[j].x;
      double C = -(A * pts2[j].x + B * pts2[j].y);

      double s = A * x + B * y + C;

//Using double: xx = -1990.670654, yy = 1636.783203,A = 200.753662, B = -121.916260, C = 599184.910076, x = -2071.253906, y = 1504.090820, s = -0.023922
Steps to reproduce

See above example.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions