前言
? ??OpenCV 中确定矩形 ROI 区域后,可获取矩形区域中四个角点,若进行 solvePnP 测距,角点要一一对应,故需对其进行排序。 ??其次,若矩形发生大于90度的旋转(无论顺时针还是逆时针),写死的 “目标实际点位” 便会与 “测得的角点” 发生错位,影响 solvePnP 测距,所以还需考虑矩形某一特定方向上是长轴还是短轴,下面给出角点排序的 demo 与大家交流。
?
一、C++ 角点排序 demo
?
Point2f verticess[4];
Point2f tempPoint;
verticess[0] = Point2f(50, 10);
verticess[1] = Point2f(100, 10);
verticess[2] = Point2f(100, 30);
verticess[3] = Point2f(50, 30);
double longSide, shortSide;
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 4; j++)
{
if (verticess[i].x > verticess[j].x)
{
tempPoint = verticess[i];
verticess[i] = verticess[j];
verticess[j] = tempPoint;
}
}
}
if (verticess[0].y > verticess[1].y)
{
tempPoint = verticess[0];
verticess[0] = verticess[1];
verticess[1] = tempPoint;
}
if (verticess[2].y > verticess[3].y)
{
tempPoint = verticess[2];
verticess[2] = verticess[3];
verticess[3] = tempPoint;
}
for (int i = 0; i < 4; i++)
{
cout << "verticess: " << verticess[i] << endl;
}
urelpoints.clear();
urelpoints.push_back(verticess[1]);
urelpoints.push_back(verticess[3]);
urelpoints.push_back(verticess[2]);
urelpoints.push_back(verticess[0]);
longSide = sqrt(pow(verticess[2].x - verticess[0].x, 2) + pow(verticess[2].y - verticess[0].y, 2));
shortSide = sqrt(pow(verticess[1].x - verticess[0].x, 2) + pow(verticess[1].y - verticess[0].y, 2));
cout << "longSide: " << longSide << endl;
cout << "shortSide: " << shortSide << endl;
for (int i = 0; i < 4; i++)
{
cout << "urelpoints : " << urelpoints[i] << endl;
}
if (shortSide > longSide)
{
}
else
{
}
总结
??此博客为 C++版 OpenCV 角点排序,Python版参见本分类其他文章。
|