mirror of
https://github.com/opencv/opencv.git
synced 2025-06-07 17:44:04 +08:00
Return a convex hull from rotatedRectangleIntersection
This commit is contained in:
parent
8488f2e265
commit
07dc6d2b45
@ -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)
|
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);
|
int res = rotatedRectangleIntersection(a, b, inter);
|
||||||
if (inter.empty() || res == INTERSECT_NONE)
|
if (inter.empty() || res == INTERSECT_NONE)
|
||||||
return 0.0f;
|
return 0.0f;
|
||||||
if (res == INTERSECT_FULL)
|
if (res == INTERSECT_FULL)
|
||||||
return 1.0f;
|
return 1.0f;
|
||||||
convexHull(inter, hull);
|
float interArea = contourArea(inter);
|
||||||
float interArea = contourArea(hull);
|
|
||||||
return interArea / (a.size.area() + b.size.area() - interArea);
|
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++ )
|
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++ )
|
for( size_t j = i+1; j < intersection.size(); j++ )
|
||||||
{
|
{
|
||||||
float dx = intersection[i].x - intersection[j].x;
|
float dx = intersection[j].x - intersection[i].x;
|
||||||
float dy = intersection[i].y - intersection[j].y;
|
float dy = intersection[j].y - intersection[i].y;
|
||||||
double d2 = dx*dx + dy*dy; // can be a really small number, need double here
|
double d2 = dx*dx + dy*dy; // can be a really small number, need double here
|
||||||
|
|
||||||
if( d2 < samePointEps*samePointEps )
|
if( d2 < samePointEps*samePointEps )
|
||||||
@ -235,6 +237,12 @@ int rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& r
|
|||||||
intersection.pop_back();
|
intersection.pop_back();
|
||||||
j--; // restart check
|
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 test7();
|
||||||
void test8();
|
void test8();
|
||||||
void test9();
|
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)
|
void CV_RotatedRectangleIntersectionTest::run(int)
|
||||||
{
|
{
|
||||||
// See pics/intersection.png for the scenarios we are testing
|
// See pics/intersection.png for the scenarios we are testing
|
||||||
@ -92,28 +111,20 @@ void CV_RotatedRectangleIntersectionTest::run(int)
|
|||||||
test7();
|
test7();
|
||||||
test8();
|
test8();
|
||||||
test9();
|
test9();
|
||||||
|
test10();
|
||||||
|
test11();
|
||||||
|
test12();
|
||||||
|
test13();
|
||||||
|
test14();
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test1()
|
void CV_RotatedRectangleIntersectionTest::test1()
|
||||||
{
|
{
|
||||||
// no intersection
|
// no intersection
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_NONE);
|
CV_Assert(ret == INTERSECT_NONE);
|
||||||
@ -123,375 +134,243 @@ void CV_RotatedRectangleIntersectionTest::test1()
|
|||||||
void CV_RotatedRectangleIntersectionTest::test2()
|
void CV_RotatedRectangleIntersectionTest::test2()
|
||||||
{
|
{
|
||||||
// partial intersection, rectangles translated
|
// partial intersection, rectangles translated
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 4);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(4);
|
vector<Point2f> targetVertices(4);
|
||||||
|
targetVertices[0] = Point2f(1.0f, 0.0f);
|
||||||
possibleVertices[0] = Point2f(0.0f, 0.0f);
|
targetVertices[1] = Point2f(1.0f, 1.0f);
|
||||||
possibleVertices[1] = Point2f(1.0f, 1.0f);
|
targetVertices[2] = Point2f(0.0f, 1.0f);
|
||||||
possibleVertices[2] = Point2f(0.0f, 1.0f);
|
targetVertices[3] = Point2f(0.0f, 0.0f);
|
||||||
possibleVertices[3] = Point2f(1.0f, 0.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test3()
|
void CV_RotatedRectangleIntersectionTest::test3()
|
||||||
{
|
{
|
||||||
// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
|
// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
|
RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f);
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 3);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(3);
|
vector<Point2f> targetVertices(3);
|
||||||
|
targetVertices[0] = Point2f(1.0f, 0.0f);
|
||||||
possibleVertices[0] = Point2f(1.0f, 1.0f);
|
targetVertices[1] = Point2f(1.0f, 1.0f);
|
||||||
possibleVertices[1] = Point2f(0.0f, 1.0f);
|
targetVertices[2] = Point2f(0.0f, 1.0f);
|
||||||
possibleVertices[2] = Point2f(1.0f, 0.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test4()
|
void CV_RotatedRectangleIntersectionTest::test4()
|
||||||
{
|
{
|
||||||
// full intersection, rectangles of same size directly on top of each other
|
// full intersection, rectangles of same size directly on top of each other
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_FULL);
|
CV_Assert(ret == INTERSECT_FULL);
|
||||||
CV_Assert(vertices.size() == 4);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(4);
|
vector<Point2f> targetVertices(4);
|
||||||
|
targetVertices[0] = Point2f(-1.0f, 1.0f);
|
||||||
possibleVertices[0] = Point2f(-1.0f, 1.0f);
|
targetVertices[1] = Point2f(-1.0f, -1.0f);
|
||||||
possibleVertices[1] = Point2f(1.0f, -1.0f);
|
targetVertices[2] = Point2f(1.0f, -1.0f);
|
||||||
possibleVertices[2] = Point2f(-1.0f, -1.0f);
|
targetVertices[3] = Point2f(1.0f, 1.0f);
|
||||||
possibleVertices[3] = Point2f(1.0f, 1.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test5()
|
void CV_RotatedRectangleIntersectionTest::test5()
|
||||||
{
|
{
|
||||||
// partial intersection, rectangle on top rotated 45 degrees
|
// partial intersection, rectangle on top rotated 45 degrees
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 8);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(8);
|
vector<Point2f> targetVertices(8);
|
||||||
|
targetVertices[0] = Point2f(-1.0f, -0.414214f);
|
||||||
possibleVertices[0] = Point2f(-1.0f, -0.414214f);
|
targetVertices[1] = Point2f(-0.414214f, -1.0f);
|
||||||
possibleVertices[1] = Point2f(-1.0f, 0.414214f);
|
targetVertices[2] = Point2f(0.414214f, -1.0f);
|
||||||
possibleVertices[2] = Point2f(-0.414214f, -1.0f);
|
targetVertices[3] = Point2f(1.0f, -0.414214f);
|
||||||
possibleVertices[3] = Point2f(0.414214f, -1.0f);
|
targetVertices[4] = Point2f(1.0f, 0.414214f);
|
||||||
possibleVertices[4] = Point2f(1.0f, -0.414214f);
|
targetVertices[5] = Point2f(0.414214f, 1.0f);
|
||||||
possibleVertices[5] = Point2f(1.0f, 0.414214f);
|
targetVertices[6] = Point2f(-0.414214f, 1.0f);
|
||||||
possibleVertices[6] = Point2f(0.414214f, 1.0f);
|
targetVertices[7] = Point2f(-1.0f, 0.414214f);
|
||||||
possibleVertices[7] = Point2f(-0.414214f, 1.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test6()
|
void CV_RotatedRectangleIntersectionTest::test6()
|
||||||
{
|
{
|
||||||
// 6 - partial intersection, rectangle on top of different size
|
// 6 - partial intersection, rectangle on top of different size
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 4);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(4);
|
vector<Point2f> targetVertices(4);
|
||||||
|
targetVertices[0] = Point2f(-1.0f, -1.0f);
|
||||||
possibleVertices[0] = Point2f(1.0f, 1.0f);
|
targetVertices[1] = Point2f(1.0f, -1.0f);
|
||||||
possibleVertices[1] = Point2f(1.0f, -1.0f);
|
targetVertices[2] = Point2f(1.0f, 1.0f);
|
||||||
possibleVertices[2] = Point2f(-1.0f, -1.0f);
|
targetVertices[3] = Point2f(-1.0f, 1.0f);
|
||||||
possibleVertices[3] = Point2f(-1.0f, 1.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test7()
|
void CV_RotatedRectangleIntersectionTest::test7()
|
||||||
{
|
{
|
||||||
// full intersection, rectangle fully enclosed in the other
|
// full intersection, rectangle fully enclosed in the other
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_FULL);
|
CV_Assert(ret == INTERSECT_FULL);
|
||||||
CV_Assert(vertices.size() == 4);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(4);
|
vector<Point2f> targetVertices(4);
|
||||||
|
targetVertices[0] = Point2f(-1.0f, 1.0f);
|
||||||
possibleVertices[0] = Point2f(1.0f, 1.0f);
|
targetVertices[1] = Point2f(-1.0f, -1.0f);
|
||||||
possibleVertices[1] = Point2f(1.0f, -1.0f);
|
targetVertices[2] = Point2f(1.0f, -1.0f);
|
||||||
possibleVertices[2] = Point2f(-1.0f, -1.0f);
|
targetVertices[3] = Point2f(1.0f, 1.0f);
|
||||||
possibleVertices[3] = Point2f(-1.0f, 1.0f);
|
compare(vertices, targetVertices);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test8()
|
void CV_RotatedRectangleIntersectionTest::test8()
|
||||||
{
|
{
|
||||||
// full intersection, rectangle fully enclosed in the other
|
// intersection by a single vertex
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f);
|
||||||
|
|
||||||
rect1.center.x = 0;
|
|
||||||
rect1.center.y = 0;
|
|
||||||
rect1.size.width = 2;
|
|
||||||
rect1.size.height = 2;
|
|
||||||
rect1.angle = 0;
|
|
||||||
|
|
||||||
rect2.center.x = 2;
|
|
||||||
rect2.center.y = 2;
|
|
||||||
rect2.size.width = 2;
|
|
||||||
rect2.size.height = 2;
|
|
||||||
rect2.angle = 0;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 1);
|
compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f)));
|
||||||
|
|
||||||
double dx = vertices[0].x - 1;
|
|
||||||
double dy = vertices[0].y - 1;
|
|
||||||
double r = sqrt(dx*dx + dy*dy);
|
|
||||||
|
|
||||||
CV_Assert(r < ACCURACY);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CV_RotatedRectangleIntersectionTest::test9()
|
void CV_RotatedRectangleIntersectionTest::test9()
|
||||||
{
|
{
|
||||||
// full intersection, rectangle fully enclosed in the other
|
// full intersection, rectangle fully enclosed in the other
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
|
||||||
RotatedRect rect1, rect2;
|
RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f);
|
||||||
|
|
||||||
rect1.center.x = 0;
|
|
||||||
rect1.center.y = 0;
|
|
||||||
rect1.size.width = 2;
|
|
||||||
rect1.size.height = 2;
|
|
||||||
rect1.angle = 0;
|
|
||||||
|
|
||||||
rect2.center.x = 2;
|
|
||||||
rect2.center.y = 0;
|
|
||||||
rect2.size.width = 2;
|
|
||||||
rect2.size.height = 123.45f;
|
|
||||||
rect2.angle = 0;
|
|
||||||
|
|
||||||
vector<Point2f> vertices;
|
vector<Point2f> vertices;
|
||||||
|
|
||||||
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
CV_Assert(ret == INTERSECT_PARTIAL);
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
CV_Assert(vertices.size() == 2);
|
|
||||||
|
|
||||||
vector<Point2f> possibleVertices(2);
|
vector<Point2f> targetVertices(2);
|
||||||
|
targetVertices[0] = Point2f(1.0f, -1.0f);
|
||||||
|
targetVertices[1] = Point2f(1.0f, 1.0f);
|
||||||
|
compare(vertices, targetVertices);
|
||||||
|
}
|
||||||
|
|
||||||
possibleVertices[0] = Point2f(1.0f, 1.0f);
|
void CV_RotatedRectangleIntersectionTest::test10()
|
||||||
possibleVertices[1] = Point2f(1.0f, -1.0f);
|
{
|
||||||
|
// 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);
|
||||||
|
|
||||||
for( size_t i = 0; i < vertices.size(); i++ )
|
vector<Point2f> vertices;
|
||||||
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
|
|
||||||
|
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::test11()
|
||||||
|
{
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(4, 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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
void CV_RotatedRectangleIntersectionTest::test13()
|
||||||
|
{
|
||||||
|
RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f);
|
||||||
|
RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f);
|
||||||
|
|
||||||
|
vector<Point2f> vertices;
|
||||||
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
|
||||||
|
|
||||||
|
CV_Assert(ret == INTERSECT_PARTIAL);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
double bestR = DBL_MAX;
|
for (int j = 0; j < 2; ++j)
|
||||||
|
|
||||||
for( size_t j = 0; j < possibleVertices.size(); j++ )
|
|
||||||
{
|
{
|
||||||
double dx = vertices[i].x - possibleVertices[j].x;
|
rects[j].center = Point2f((float)(rand() % kWidth), (float)(rand() % kHeight));
|
||||||
double dy = vertices[i].y - possibleVertices[j].y;
|
rects[j].size = Size2f(rand() % kWidth + 1.0f, rand() % kHeight + 1.0f);
|
||||||
double r = sqrt(dx*dx + dy*dy);
|
rects[j].angle = (float)(rand() % 360);
|
||||||
|
|
||||||
bestR = std::min(bestR, r);
|
|
||||||
}
|
}
|
||||||
|
rotatedRectangleIntersection(rects[0], rects[1], inter);
|
||||||
CV_Assert(bestR < ACCURACY);
|
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 height = scores.size[2];
|
||||||
const int width = scores.size[3];
|
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)
|
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)
|
for (int x = 0; x < width; ++x)
|
||||||
{
|
{
|
||||||
float score = scoresData[x];
|
float score = scoresData[x];
|
||||||
@ -142,7 +139,6 @@ void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
// Decode a prediction.
|
// Decode a prediction.
|
||||||
|
|
||||||
// Multiple by 4 because feature maps are 4 time less than input image.
|
// Multiple by 4 because feature maps are 4 time less than input image.
|
||||||
float offsetX = x * 4.0f, offsetY = y * 4.0f;
|
float offsetX = x * 4.0f, offsetY = y * 4.0f;
|
||||||
float angle = anglesData[x];
|
float angle = anglesData[x];
|
||||||
@ -159,11 +155,5 @@ void decode(const Mat& scores, const Mat& geometry, float scoreThresh,
|
|||||||
detections.push_back(r);
|
detections.push_back(r);
|
||||||
confidences.push_back(score);
|
confidences.push_back(score);
|
||||||
}
|
}
|
||||||
scoresData += width;
|
|
||||||
x0_data += width;
|
|
||||||
x1_data += width;
|
|
||||||
x2_data += width;
|
|
||||||
x3_data += width;
|
|
||||||
anglesData += width;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user