OpenCV for C ++中的getTriangleList()如何工作?特别是,我们得到许多额外的点作为三角形点,这些点从哪里突然出现?点数(-1800,-1800)等 矢量vi只包含(1,1)。
This is the image of point 1 1 being sent in and the triangle has many points.
cout << "Enter first images' tie points:\t";
for (int i = 0; i < n; ++i)
{
int a, b;
cin >> a >> b;
Point2f p(a, b);
vi.push_back(p);
}
生成三角形的逻辑如下:
Size size = I.size();
Rect R(0, 0, size.width, size.height);
Subdiv2D a(R);
//a.initDelaunay(R);
for( vector<Point2f>::iterator it = vi.begin(); it != vi.end(); it++)
{
a.insert(*it);
}
vector<Vec6f> triangle;
a.getTriangleList(triangle);
答案 0 :(得分:0)
我不知道为什么,但是getTriangleList()还会返回图像外部的三角形。它可能与Voronoi图的制作方式有关,但我不了解这些三角形如何为我们提供帮助。因此,您需要删除这些三角形。我做了对我有用的功能。约有20%的三角形,但完全或部分脱离了图像。
std::vector<Vec6f> FaceMorpher::pointsToTriangles(std::vector<Point2f> points)
{
Rect rect(0, 0, _widthImage, _heightmage); //choose the space we want to work in, in this case all the image
Subdiv2D subdiv(rect); //object representing the points is a Voronoi diagram so it can easily triangulise the points
for (auto& point : points)
subdiv.insert(point); //subdiv updates the Voronoi diagram each time a point is inserted
std::vector<Vec6f> triangles;
subdiv.getTriangleList(triangles); //generate the triangles by splitting polygons formed by Voronoi diagram into triangles
//Remove triangles that are not completly inside the image
std::vector<Vec6f> trianglesInsideImg;
for (size_t i = 0; i < triangles.size(); i++)
{
Vec6f t = triangles[i];
auto p1 = Point(cvRound(t[0]), cvRound(t[1]));
auto p2 = Point(cvRound(t[2]), cvRound(t[3]));
auto p3 = Point(cvRound(t[4]), cvRound(t[5]));
if (rect.contains(p1) && rect.contains(p2) && rect.contains(p3))
{
Vec6f triangle;
triangle[0] = p1.x;
triangle[1] = p1.y;
triangle[2] = p2.x;
triangle[3] = p2.y;
triangle[4] = p3.x;
triangle[5] = p3.y;
trianglesInsideImg.push_back(triangle);
}
}
return trianglesInsideImg;
}