开发者

Is OpenCV's Subdiv2D / Delaunay broken when the point given to locate is one of the triangle's vertices?

开发者 https://www.devze.com 2023-03-26 09:42 出处:网络
The code for building a triangulation: CvSubdiv2D *subdiv; CvMemStorage *storage = cvCreateMemStorage(0);

The code for building a triangulation:

CvSubdiv2D *subdiv;
CvMemStorage *storage = cvCreateMemStorage(0);
CvRect rectangle = cvRect(0, 0, 100开发者_高级运维, 100);
subdiv = cvCreateSubdivDelaunay2D(rectangle, storage);
CvPoint2D32f p1 = cvPoint2D32f(10, 10);
CvPoint2D32f p2 = cvPoint2D32f(50, 10);
CvPoint2D32f p3 = cvPoint2D32f(10, 50);
cvSubdivDelaunay2DInsert(subdiv, p1);
cvSubdivDelaunay2DInsert(subdiv, p2);
cvSubdivDelaunay2DInsert(subdiv, p3);

After that, make a query using one of the points:

CvSubdiv2DEdge edge;
CvSubdiv2DPoint *pp;
CvSubdiv2DPointLocation loc = cvSubdiv2DLocate(subdiv, p1, &edge, &pp);

Once you have the results, you have to check if the point falls on:

  • facet
  • edge
  • vertex
  • outside of the defined triangulation/rectangle

In this case, it's a vertex:

if(loc == CV_PTLOC_VERTEX) {
//        CvSubdiv2DPoint *tp = cvSubdiv2DEdgeOrg(edge);
//        CvPoint2D32f point = tp->pt;
//        std::cout << point.x << ", " << point.y << std::endl;

//        CvSubdiv2DPoint *tp = cvSubdiv2DEdgeOrg(pp->first);
//        CvPoint2D32f point = tp->pt;
//        std::cout << point.x << ", " << point.y << std::endl;
}

However, both my approaches to it have failed. In the first 3 lines, I tried seeing if the locate had put the correct edge on the parameter. It didn't and I got a segmentation fault. On the second block, I tried accessing the first element in the CvSubdiv2DPoint struct but it also doesn't work -- segmentation fault. I can't find the points on first nor can I use it iterate over the other edges.

The reason CvSubdiv2DPoint is useless is because, in order to iterate through the structure and actually find the triangles, I need a CvSubdiv2DEdge, but since I can't convert from a point to an edge, the result of the function is useless.

I may have overlooked something, but to me it seems broken. Here's the documentation. Any thoughts?


Edges can be iterated using sequence functionality here is the code:

CvMemStorage* storage = cvCreateMemStorage();
CvSubdiv2D* subdivision = cvCreateSubdivDelaunay2D(rect, storage);
for (int i = 0; i < points.size(); ++i)
{
    cvSubdivDelaunay2DInsert(subdivision, points[i]);
}

cvCalcSubdivVoronoi2D(subdivision);
CvSeqReader reader;
CvSeq* seq = (CvSeq*) subdivision->edges;
cvStartReadSeq(seq, &reader);
for (int i = 0; i < seq->total; ++i)
{
    CvQuadEdge2D* edge = (CvQuadEdge2D*)reader.ptr;
    if (CV_IS_SET_ELEM(edge))
    {
               //Do something...
    }
    CV_NEXT_SEQ_ELEM(seq->elem_size, reader);
}

if (storage != 0)
{
    cvReleaseMemStorage(&storage);
}

Although I didn't find a way to iterate edges connected to some vertex, by the way vertexes are iterated in the same way using the subdivision itself as sequence.

0

精彩评论

暂无评论...
验证码 换一张
取 消