-
-
Notifications
You must be signed in to change notification settings - Fork 56.5k
rotatedRectangleIntersection gives incorrect result for pairs of nearly identical rectangles #21659
Description
System information (version)
- OpenCV => 4.5.5
- Operating System / Platform => Ubuntu 20.04
- Compiler => g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Detailed description
rotatedRectangleIntersection returns an incorrect intersection region for some rectangles with nearly equal parameters. It seems like one or more of the intersections points is missing.
Was discovered when I calculated IOU on rectangles and noted that some rectangles which were a near-perfect match returned very low IOUs or even zero.
I have given examples of some problematic rectangles in the steps to reproduce, but I have a list with more problematic pairs of rectangles which I can provide.
Steps to reproduce
The code below creates two rectangles which have been shown to create an incorrect output. The rectangles are almost identical, with only a small shift in the center position. As both rectangles have an area of 16 it is expected that the area of the intersection region should be close to 16, however, the returned value is 7.99997.
#include <iostream>
#include <opencv2/opencv.hpp>
#include <limits>
int main()
{
std::cout.precision(std::numeric_limits<float>::max_digits10);
std::cout << cv::getVersionString() << std::endl;
cv::RotatedRect rectangle1(cv::Point2f(4.48589373f, 12.5545063f), cv::Size2f(4.0f, 4.0f), 0.0347290039f);
cv::RotatedRect rectangle2(cv::Point2f(4.48589373f, 12.5545235f), cv::Size2f(4.0f, 4.0f), 0.0347290039f);
std::vector<cv::Point2f> intersectionRegion;
cv::RectanglesIntersectTypes intersectType = (cv::RectanglesIntersectTypes)cv::rotatedRectangleIntersection(rectangle1, rectangle2, intersectionRegion);
std::cout << "intersection region" << std::endl;
std::cout << intersectionRegion << std::endl;
float intersectionArea = cv::contourArea(intersectionRegion);
std::cout << "intersection area: " << intersectionArea << std::endl;
}Output:
4.5.5-dev
intersection region
[6.4871054, 10.555737;
6.4846811, 14.555718;
2.4846818, 14.553293]
intersection area: 7.99996519
Adding a small offset to the position of the rectangles gives a correct result:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <limits>
int main()
{
std::cout.precision(std::numeric_limits<float>::max_digits10);
std::cout << cv::getVersionString() << std::endl;
cv::RotatedRect rectangle1(cv::Point2f(4.48589373f, 12.5545063f + 0.01f), cv::Size2f(4.0f, 4.0f), 0.0347290039f);
cv::RotatedRect rectangle2(cv::Point2f(4.48589373f, 12.5545235f), cv::Size2f(4.0f, 4.0f), 0.0347290039f);
std::vector<cv::Point2f> intersectionRegion;
cv::RectanglesIntersectTypes intersectType = (cv::RectanglesIntersectTypes)cv::rotatedRectangleIntersection(rectangle1, rectangle2, intersectionRegion);
std::cout << "intersection region" << std::endl;
std::cout << intersectionRegion << std::endl;
float intersectionArea = cv::contourArea(intersectionRegion);
std::cout << "intersection area: " << intersectionArea << std::endl;
}Output:
4.5.5-dev
intersection region
[2.4846878, 14.55331;
2.4871063, 10.563294;
6.4870996, 10.56572;
6.4846811, 14.555736]
intersection area: 15.960043
Another interesting case is obtained by using the following rectangles, which only returns a line as the intersection:
cv::RotatedRect rectangle1(cv::Point2f(45.0715866, 39.8825722), cv::Size2f(3.0f, 3.0f), 0.10067749f);
cv::RotatedRect rectangle2(cv::Point2f(45.0715866, 39.8825874), cv::Size2f(3.0f, 3.0f), 0.10067749f);
Then the output of the code is:
4.5.5-dev
intersection region
[46.574219, 38.385227;
43.568954, 41.379932]
intersection area: 0
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