C ++-ObjC OpenCV约束Delaunay [英] C++-ObjC OpenCV Constrained Delaunay

查看:135
本文介绍了C ++-ObjC OpenCV约束Delaunay的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我成功地在OpenCV 2.3.1中实现了轮廓的Delaunay三角剖分.

I succesfully implemented a Delaunay triangulation of a contour in OpenCV 2.3.1.

使用cvPointPolygonTest,我可以获取凸包中的所有三角形,然后尝试对三角形质心执行另一个cvPointPolygonTest,以了解它们是否在主轮廓中,因此我可以约束轮廓的三角剖分.

With cvPointPolygonTest I can get all the triangles in the convex hull, then I tried to perform another cvPointPolygonTest on triangles centroid to know if they are in the main contour or not, so I can have the constrained triangulation of the contour.

但是,它不能很好地工作,因为有些三角形(例如,一个走路的人双腿分开)在一个洞上.

But, it doesn't work well, as some triangles are (eg. with a walking man who has his two legs distant) 'over' a hole.

有人知道执行约束三角剖分的方法吗?我想到了凸度缺陷,但无法理解如何开始.

Does anyone know a way to perform a constrained triangulation. I thought about convexityDefects, but can't manage to understand how to begin with this.

提前谢谢!

实际上,这不是凸包缺陷问题,而是三角剖分问题.此图像将向您显示问题:

In fact, it is not a Convex Hull defects problem, but a triangulation one. This image will show you the trouble :

尤其是在三角剖分的船壳的底部,您可以看到三角剖分是在与AND轮廓之外,因为OpenCV在对凸面船体进行三角剖分.我想找到一种对轮廓本身进行三角测量的方法.

Particularly in the bottom of the triangulated hull, you can see that the triangulation is in AND out of the contour, because OpenCV is triangulating the convex hull. I would like to find a way to triangulate the contour itself.

我发现了一些在轮廓本身中添加Steiner点的想法,但是找不到从OpenCV开始的地方.

I found some ideas about adding Steiner Points in the contour itself, but can't find where to begin with OpenCV.

我的想法是:

  • 测试三角形是否在轮廓内且在轮廓外;
  • 如果为true:获取交点;否则为false.
  • 并将其添加到cvSubdiv2D.

我对吗?

感谢您的耐心和回答!

Thanks for your patience and your answers !

推荐答案

最后,我在程序中实现了poly2tri库.

Finally I implemented the poly2tri library in my program.

这是结果(OpenCV获取轮廓,然后poly2tri执行三角​​剖分):

Here is the result (OpenCV to get the contours, then poly2tri to perform triangulation) :

// -------------------- poly2tri --------------------


NSMutableArray* temp = [[NSMutableArray alloc] init];

vector<p2t::Triangle*> triangles;
vector< vector<p2t::Point*> > polylines;
vector<p2t::Point*> polyline;

for(int i = 0; i < contour32->total; i++ ) {
    CvPoint2D32f* pt32 = CV_GET_SEQ_ELEM(CvPoint2D32f, contour32, i);
    polyline.push_back(new p2t::Point((double)pt32->x, (double)pt32->y));
}

polylines.push_back(polyline);


p2t::CDT* cdt = new p2t::CDT(polyline);



// TODO -> holes with CV_RETR_TREE

// Triangulation !
cdt->Triangulate();

// On exporte nos triangles
triangles = cdt->GetTriangles();

for (int i = 0; i < triangles.size(); i++) {

    p2t::Triangle& t = *triangles[i];
    p2t::Point& a = *t.GetPoint(0);
    p2t::Point& b = *t.GetPoint(1);
    p2t::Point& c = *t.GetPoint(2);

    double x1 = (width / rWidth * (double)a.x) - (width / 2.f);
    double y1 = (height / rHeight * (double)a.y) - (height / 2.f);                   
    double x2 = (width / rWidth * (double)b.x) - (width / 2.f);
    double y2 = (height / rHeight * (double)b.y) - (height / 2.f);
    double x3 = (width / rWidth * (double)c.x) - (width / 2.f);
    double y3 = (height / rHeight * (double)c.y) - (height / 2.f);

    [temp addObject:[[NSArray arrayWithObjects:
                      [NSArray arrayWithObjects:
                       [NSNumber numberWithDouble:x1],
                       [NSNumber numberWithDouble:y1],
                       [NSNumber numberWithDouble:0.],
                       nil], 
                      [NSArray arrayWithObjects:
                       [NSNumber numberWithDouble:x2],
                       [NSNumber numberWithDouble:y2],
                       [NSNumber numberWithDouble:0.],
                       nil], 
                      [NSArray arrayWithObjects:
                       [NSNumber numberWithDouble:x3],
                       [NSNumber numberWithDouble:y3],
                       [NSNumber numberWithDouble:0.],
                       nil], 
                      nil] autorelease]];

}

[outDelaunay addObject:temp];                       

其中contour32是使用cvFindContours找到的OpenCV轮廓,然后将其转换为float32.

where contour32 is your OpenCV contour found with cvFindContours, then converted to float32.

这篇关于C ++-ObjC OpenCV约束Delaunay的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆