Commit 07dc6d2b authored by Dmitry Kurtaev's avatar Dmitry Kurtaev

Return a convex hull from rotatedRectangleIntersection

parent 8488f2e2
......@@ -32,14 +32,13 @@ void NMSBoxes(const std::vector<Rect>& bboxes, const std::vector<float>& scores,
static inline float rotatedRectIOU(const RotatedRect& a, const RotatedRect& b)
{
std::vector<Point2f> inter, hull;
std::vector<Point2f> inter;
int res = rotatedRectangleIntersection(a, b, inter);
if (inter.empty() || res == INTERSECT_NONE)
return 0.0f;
if (res == INTERSECT_FULL)
return 1.0f;
convexHull(inter, hull);
float interArea = contourArea(hull);
float interArea = contourArea(inter);
return interArea / (a.size.area() + b.size.area() - interArea);
}
......
......@@ -219,13 +219,15 @@ int rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& r
}
}
// Get rid of dupes
// Get rid of dupes and order points.
for( int i = 0; i < (int)intersection.size()-1; i++ )
{
float dx1 = intersection[i + 1].x - intersection[i].x;
float dy1 = intersection[i + 1].y - intersection[i].y;
for( size_t j = i+1; j < intersection.size(); j++ )
{
float dx = intersection[i].x - intersection[j].x;
float dy = intersection[i].y - intersection[j].y;
float dx = intersection[j].x - intersection[i].x;
float dy = intersection[j].y - intersection[i].y;
double d2 = dx*dx + dy*dy; // can be a really small number, need double here
if( d2 < samePointEps*samePointEps )
......@@ -235,6 +237,12 @@ int rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& r
intersection.pop_back();
j--; // restart check
}
else if (dx1 * dy - dy1 * dx < 0)
{
std::swap(intersection[i + 1], intersection[j]);
dx1 = dx;
dy1 = dy;
}
}
}
......
......@@ -66,8 +66,27 @@ private:
void test7();
void test8();
void test9();
void test10();
void test11();
void test12();
void test13();
void test14();
};
static void compare(const std::vector<Point2f>& test, const std::vector<Point2f>& target)
{
ASSERT_EQ(test.size(), target.size());
ASSERT_TRUE(test.size() < 4 || isContourConvex(test));
ASSERT_TRUE(target.size() < 4 || isContourConvex(target));
for( size_t i = 0; i < test.size(); i++ )
{
double dx = test[i].x - target[i].x;
double dy = test[i].y - target[i].y;
double r = sqrt(dx*dx + dy*dy);
ASSERT_LT(r, ACCURACY);
}
}
void CV_RotatedRectangleIntersectionTest::run(int)
{
// See pics/intersection.png for the scenarios we are testing
......@@ -92,28 +111,20 @@ void CV_RotatedRectangleIntersectionTest::run(int)
test7();
test8();
test9();
test10();
test11();
test12();
test13();
test14();
}
void CV_RotatedRectangleIntersectionTest::test1()
{
// no intersection
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 12.0f;
rect2.center.x = 10;
rect2.center.y = 10;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 34.0f;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f);
RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_NONE);
......@@ -123,375 +134,243 @@ void CV_RotatedRectangleIntersectionTest::test1()
void CV_RotatedRectangleIntersectionTest::test2()
{
// partial intersection, rectangles translated
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
rect2.center.x = 1;
rect2.center.y = 1;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 0;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 4);
vector<Point2f> possibleVertices(4);
possibleVertices[0] = Point2f(0.0f, 0.0f);
possibleVertices[1] = Point2f(1.0f, 1.0f);
possibleVertices[2] = Point2f(0.0f, 1.0f);
possibleVertices[3] = Point2f(1.0f, 0.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
bestR = std::min(bestR, r);
}
CV_Assert(bestR < ACCURACY);
}
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(1.0f, 0.0f);
targetVertices[1] = Point2f(1.0f, 1.0f);
targetVertices[2] = Point2f(0.0f, 1.0f);
targetVertices[3] = Point2f(0.0f, 0.0f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test3()
{
// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
rect2.center.x = 1;
rect2.center.y = 1;
rect2.size.width = sqrt(2.0f);
rect2.size.height = 20;
rect2.angle = 45.0f;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 3);
vector<Point2f> possibleVertices(3);
possibleVertices[0] = Point2f(1.0f, 1.0f);
possibleVertices[1] = Point2f(0.0f, 1.0f);
possibleVertices[2] = Point2f(1.0f, 0.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
bestR = std::min(bestR, r);
}
CV_Assert(bestR < ACCURACY);
}
vector<Point2f> targetVertices(3);
targetVertices[0] = Point2f(1.0f, 0.0f);
targetVertices[1] = Point2f(1.0f, 1.0f);
targetVertices[2] = Point2f(0.0f, 1.0f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test4()
{
// full intersection, rectangles of same size directly on top of each other
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
rect2.center.x = 0;
rect2.center.y = 0;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 0;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_FULL);
CV_Assert(vertices.size() == 4);
vector<Point2f> possibleVertices(4);
possibleVertices[0] = Point2f(-1.0f, 1.0f);
possibleVertices[1] = Point2f(1.0f, -1.0f);
possibleVertices[2] = Point2f(-1.0f, -1.0f);
possibleVertices[3] = Point2f(1.0f, 1.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
bestR = std::min(bestR, r);
}
CV_Assert(bestR < ACCURACY);
}
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(-1.0f, 1.0f);
targetVertices[1] = Point2f(-1.0f, -1.0f);
targetVertices[2] = Point2f(1.0f, -1.0f);
targetVertices[3] = Point2f(1.0f, 1.0f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test5()
{
// partial intersection, rectangle on top rotated 45 degrees
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
rect2.center.x = 0;
rect2.center.y = 0;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 45.0f;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 8);
vector<Point2f> possibleVertices(8);
possibleVertices[0] = Point2f(-1.0f, -0.414214f);
possibleVertices[1] = Point2f(-1.0f, 0.414214f);
possibleVertices[2] = Point2f(-0.414214f, -1.0f);
possibleVertices[3] = Point2f(0.414214f, -1.0f);
possibleVertices[4] = Point2f(1.0f, -0.414214f);
possibleVertices[5] = Point2f(1.0f, 0.414214f);
possibleVertices[6] = Point2f(0.414214f, 1.0f);
possibleVertices[7] = Point2f(-0.414214f, 1.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
bestR = std::min(bestR, r);
}
CV_Assert(bestR < ACCURACY);
}
vector<Point2f> targetVertices(8);
targetVertices[0] = Point2f(-1.0f, -0.414214f);
targetVertices[1] = Point2f(-0.414214f, -1.0f);
targetVertices[2] = Point2f(0.414214f, -1.0f);
targetVertices[3] = Point2f(1.0f, -0.414214f);
targetVertices[4] = Point2f(1.0f, 0.414214f);
targetVertices[5] = Point2f(0.414214f, 1.0f);
targetVertices[6] = Point2f(-0.414214f, 1.0f);
targetVertices[7] = Point2f(-1.0f, 0.414214f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test6()
{
// 6 - partial intersection, rectangle on top of different size
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
rect2.center.x = 0;
rect2.center.y = 0;
rect2.size.width = 2;
rect2.size.height = 10;
rect2.angle = 0;
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 4);
vector<Point2f> possibleVertices(4);
possibleVertices[0] = Point2f(1.0f, 1.0f);
possibleVertices[1] = Point2f(1.0f, -1.0f);
possibleVertices[2] = Point2f(-1.0f, -1.0f);
possibleVertices[3] = Point2f(-1.0f, 1.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
bestR = std::min(bestR, r);
}
CV_Assert(bestR < ACCURACY);
}
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(-1.0f, -1.0f);
targetVertices[1] = Point2f(1.0f, -1.0f);
targetVertices[2] = Point2f(1.0f, 1.0f);
targetVertices[3] = Point2f(-1.0f, 1.0f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test7()
{
// full intersection, rectangle fully enclosed in the other
RotatedRect rect1, rect2;
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 12.34f;
rect1.size.height = 56.78f;
rect1.angle = 0;
rect2.center.x = 0;
rect2.center.y = 0;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 0;
RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f);
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_FULL);
CV_Assert(vertices.size() == 4);
vector<Point2f> possibleVertices(4);
possibleVertices[0] = Point2f(1.0f, 1.0f);
possibleVertices[1] = Point2f(1.0f, -1.0f);
possibleVertices[2] = Point2f(-1.0f, -1.0f);
possibleVertices[3] = Point2f(-1.0f, 1.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(-1.0f, 1.0f);
targetVertices[1] = Point2f(-1.0f, -1.0f);
targetVertices[2] = Point2f(1.0f, -1.0f);
targetVertices[3] = Point2f(1.0f, 1.0f);
compare(vertices, targetVertices);
}
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
void CV_RotatedRectangleIntersectionTest::test8()
{
// intersection by a single vertex
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f);
bestR = std::min(bestR, r);
}
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(bestR < ACCURACY);
}
CV_Assert(ret == INTERSECT_PARTIAL);
compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f)));
}
void CV_RotatedRectangleIntersectionTest::test8()
void CV_RotatedRectangleIntersectionTest::test9()
{
// full intersection, rectangle fully enclosed in the other
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f);
RotatedRect rect1, rect2;
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
vector<Point2f> targetVertices(2);
targetVertices[0] = Point2f(1.0f, -1.0f);
targetVertices[1] = Point2f(1.0f, 1.0f);
compare(vertices, targetVertices);
}
rect2.center.x = 2;
rect2.center.y = 2;
rect2.size.width = 2;
rect2.size.height = 2;
rect2.angle = 0;
void CV_RotatedRectangleIntersectionTest::test10()
{
// three points of rect2 are inside rect1.
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(0, 0.5), Size2f(1, 1), 45.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 1);
double dx = vertices[0].x - 1;
double dy = vertices[0].y - 1;
double r = sqrt(dx*dx + dy*dy);
CV_Assert(r < ACCURACY);
vector<Point2f> targetVertices(5);
targetVertices[0] = Point2f(0.207107f, 1.0f);
targetVertices[1] = Point2f(-0.207107f, 1.0f);
targetVertices[2] = Point2f(-0.707107f, 0.5f);
targetVertices[3] = Point2f(0.0f, -0.207107f);
targetVertices[4] = Point2f(0.707107f, 0.5f);
compare(vertices, targetVertices);
}
void CV_RotatedRectangleIntersectionTest::test9()
void CV_RotatedRectangleIntersectionTest::test11()
{
// full intersection, rectangle fully enclosed in the other
RotatedRect rect1(Point2f(0, 0), Size2f(4, 2), 0.0f);
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), -45.0f);
RotatedRect rect1, rect2;
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
rect1.center.x = 0;
rect1.center.y = 0;
rect1.size.width = 2;
rect1.size.height = 2;
rect1.angle = 0;
CV_Assert(ret == INTERSECT_PARTIAL);
rect2.center.x = 2;
rect2.center.y = 0;
rect2.size.width = 2;
rect2.size.height = 123.45f;
rect2.angle = 0;
vector<Point2f> targetVertices(6);
targetVertices[0] = Point2f(-0.414214f, -1.0f);
targetVertices[1] = Point2f(0.414213f, -1.0f);
targetVertices[2] = Point2f(1.41421f, 0.0f);
targetVertices[3] = Point2f(0.414214f, 1.0f);
targetVertices[4] = Point2f(-0.414213f, 1.0f);
targetVertices[5] = Point2f(-1.41421f, 0.0f);
compare(vertices, targetVertices);
}
vector<Point2f> vertices;
void CV_RotatedRectangleIntersectionTest::test12()
{
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
RotatedRect rect2(Point2f(0, 1), Size2f(1, 1), 0.0f);
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
CV_Assert(ret == INTERSECT_PARTIAL);
CV_Assert(vertices.size() == 2);
vector<Point2f> possibleVertices(2);
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(-0.5f, 1.0f);
targetVertices[1] = Point2f(-0.5f, 0.5f);
targetVertices[2] = Point2f(0.5f, 0.5f);
targetVertices[3] = Point2f(0.5f, 1.0f);
compare(vertices, targetVertices);
}
possibleVertices[0] = Point2f(1.0f, 1.0f);
possibleVertices[1] = Point2f(1.0f, -1.0f);
void CV_RotatedRectangleIntersectionTest::test13()
{
RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f);
RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f);
for( size_t i = 0; i < vertices.size(); i++ )
{
double bestR = DBL_MAX;
vector<Point2f> vertices;
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
for( size_t j = 0; j < possibleVertices.size(); j++ )
{
double dx = vertices[i].x - possibleVertices[j].x;
double dy = vertices[i].y - possibleVertices[j].y;
double r = sqrt(dx*dx + dy*dy);
CV_Assert(ret == INTERSECT_PARTIAL);
bestR = std::min(bestR, r);
}
vector<Point2f> targetVertices(4);
targetVertices[0] = Point2f(-0.5f, 0.5f);
targetVertices[1] = Point2f(0.5f, 0.5f);
targetVertices[2] = Point2f(0.5f, 1.5f);
targetVertices[3] = Point2f(-0.5f, 1.5f);
compare(vertices, targetVertices);
}
CV_Assert(bestR < ACCURACY);
void CV_RotatedRectangleIntersectionTest::test14()
{
const int kNumTests = 100;
const int kWidth = 5;
const int kHeight = 5;
RotatedRect rects[2];
std::vector<Point2f> inter;
for (int i = 0; i < kNumTests; ++i)
{
for (int j = 0; j < 2; ++j)
{
rects[j].center = Point2f((float)(rand() % kWidth), (float)(rand() % kHeight));
rects[j].size = Size2f(rand() % kWidth + 1.0f, rand() % kHeight + 1.0f);
rects[j].angle = (float)(rand() % 360);
}
rotatedRectangleIntersection(rects[0], rects[1], inter);
ASSERT_TRUE(inter.size() < 4 || isContourConvex(inter));
}
}
......
......@@ -124,17 +124,14 @@ void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
const int height = scores.size[2];
const int width = scores.size[3];
const int planeSize = height * width;
float* scoresData = (float*)scores.data;
float* geometryData = (float*)geometry.data;
float* x0_data = geometryData;
float* x1_data = geometryData + planeSize;
float* x2_data = geometryData + planeSize * 2;
float* x3_data = geometryData + planeSize * 3;
float* anglesData = geometryData + planeSize * 4;
for (int y = 0; y < height; ++y)
{
const float* scoresData = scores.ptr<float>(0, 0, y);
const float* x0_data = geometry.ptr<float>(0, 0, y);
const float* x1_data = geometry.ptr<float>(0, 1, y);
const float* x2_data = geometry.ptr<float>(0, 2, y);
const float* x3_data = geometry.ptr<float>(0, 3, y);
const float* anglesData = geometry.ptr<float>(0, 4, y);
for (int x = 0; x < width; ++x)
{
float score = scoresData[x];
......@@ -142,7 +139,6 @@ void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
continue;
// Decode a prediction.
// Multiple by 4 because feature maps are 4 time less than input image.
float offsetX = x * 4.0f, offsetY = y * 4.0f;
float angle = anglesData[x];
......@@ -159,11 +155,5 @@ void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
detections.push_back(r);
confidences.push_back(score);
}
scoresData += width;
x0_data += width;
x1_data += width;
x2_data += width;
x3_data += width;
anglesData += width;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment