delaunay细分和 Voronoi细分的创建和遍历

#include <opencv2/imgproc/imgproc_c.h>
#include <opencv2/legacy/legacy.hpp>
#include "opencv2/highgui/highgui.hpp"
#include<opencv2\opencv.hpp>
#include<iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
static CvSubdiv2D* init_delaunay(CvMemStorage* storage,//初始化三角剖分结构,为其分配单元
    CvRect rect)
{
    CvSubdiv2D* subdiv;//三角剖分的数据单元

    subdiv = cvCreateSubdiv2D(CV_SEQ_KIND_SUBDIV2D, sizeof(*subdiv),
        sizeof(CvSubdiv2DPoint),
        sizeof(CvQuadEdge2D),
        storage);
    cvInitSubdivDelaunay2D(subdiv, rect);

    return subdiv;
}


static void draw_subdiv_point(IplImage* img, CvPoint2D32f fp, CvScalar color)//画出三角剖分的顶点
{
    cvCircle(img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 5, color, CV_FILLED, 8, 0);
}


static void draw_subdiv_edge(IplImage* img, CvSubdiv2DEdge edge, CvScalar color)//画出三角剖分的边
{
    CvSubdiv2DPoint* org_pt;//源顶点
    CvSubdiv2DPoint* dst_pt;//目地顶点
    CvPoint2D32f org;
    CvPoint2D32f dst;
    CvPoint iorg, idst;

    org_pt = cvSubdiv2DEdgeOrg(edge);//通过边获取顶点
    dst_pt = cvSubdiv2DEdgeDst(edge);

    if (org_pt && dst_pt)//如果两个端点不为空
    {
        org = org_pt->pt;
        dst = dst_pt->pt;

        iorg = cvPoint(cvRound(org.x), cvRound(org.y));
        idst = cvPoint(cvRound(dst.x), cvRound(dst.y));

        cvLine(img, iorg, idst, color, 1, CV_AA, 0);
    }
}


static void draw_subdiv(IplImage* img, CvSubdiv2D* subdiv,
    CvScalar delaunay_color, CvScalar voronoi_color)//画出剖分和细分
{
    CvSeqReader  reader;
    int i, total = subdiv->edges->total;//边的数量
    int elem_size = subdiv->edges->elem_size;//边的大小
    cout << typeid(subdiv->edges).name() << endl;

    cvStartReadSeq((CvSeq*)(subdiv->edges), &reader, 0);//使用CvSeqReader遍历Delaunay或者Voronoi边

    for (i = 0; i < total; i++)
    {
        CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);

        if (CV_IS_SET_ELEM(edge))
        {
            // draw_subdiv_edge( img, (CvSubdiv2DEdge)edge + 1, voronoi_color );
            draw_subdiv_edge(img, (CvSubdiv2DEdge)edge, delaunay_color);
        }

        CV_NEXT_SEQ_ELEM(elem_size, reader);
    }

}


static void locate_point(CvSubdiv2D* subdiv, CvPoint2D32f fp, IplImage* img,//遍历三角剖分的边
    CvScalar active_color)
{
    CvSubdiv2DEdge e;
    CvSubdiv2DEdge e0 = 0;
    CvSubdiv2DPoint* p = 0;

    cvSubdiv2DLocate(subdiv, fp, &e0, &p);

    if (e0)
    {
        e = e0;
        do
        {
            draw_subdiv_edge(img, e, active_color);
            e = cvSubdiv2DGetEdge(e, CV_NEXT_AROUND_LEFT);
        } while (e != e0);
    }

    draw_subdiv_point(img, fp, active_color);
}

//@author andme-单目视觉
void dashLine(Mat &img, Point2d& pt1, Point2d& pt2, int n)//n为虚线段数
{
    Point sub = pt2 - pt1;
    for (int i = 0; i < 2 * n; i += 2)
    {
        line(img, Point(pt1.x + sub.x * i / (2 * n - 1), pt1.y + sub.y * i / (2 * n - 1)), Point(pt1.x + sub.x * (i + 1) / (2 * n - 1), pt1.y + sub.y * (i + 1) / (2 * n - 1)), Scalar(0, 255, 0), 2);
    }
}



//调用形式draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 1 ));
static void draw_subdiv_facet(IplImage* img, CvSubdiv2DEdge edge)//画出voronoi面   
{
    //cout<<edge<<endl;//edge低两位表示表示索引,高位表示四方边缘指针。
    //cout<<(edge&3)<<endl;
    CvSubdiv2DEdge t = edge;//当我们按上面的调用形式时,edge为eRot。
    int i, count = 0;
    CvPoint* buf = 0;
    Point2d *buf1 = 0;

    // count number of edges in facet //面内边的计数
    do
    {
        count++;
        t = cvSubdiv2DGetEdge(t, CV_NEXT_AROUND_LEFT);
    } while (t != edge);//我们绕着一个voronoi单元一周,遍历该vornonoi边缘所拥有的边缘数。
    cout << "count=" << count << endl;
    buf = (CvPoint*)malloc(count * sizeof(buf[0]));
    buf1 = (Point2d*)malloc(count*sizeof(buf1[0]));

    // gather points
    t = edge;
    for (i = 0; i < count; i++)
    {
        CvSubdiv2DPoint* pt = cvSubdiv2DEdgeOrg(t);//第一次获取eRot边缘的起始点
        if (!pt) break;//如果得不到该源点,则退出循环
        buf[i] = cvPoint(cvRound(pt->pt.x), cvRound(pt->pt.y));//将该点转换为cvPoint类型点,存储在buf中
        t = cvSubdiv2DGetEdge(t, CV_NEXT_AROUND_LEFT);//然后绕着vornonoi单元,左旋转。
    }

    if (i == count)//如果所有的点都存储起来了。
    {
        CvSubdiv2DPoint* pt = cvSubdiv2DEdgeDst(cvSubdiv2DRotateEdge(edge, 1));//这里eRot的旋转边缘应该是reversed e,那么目的点,就是e的源点。
        // cvFillConvexPoly( img, buf, count, CV_RGB(rand()&255,rand()&255,rand()&255), CV_AA, 0 );//填充凸多边形
        for (i = 0; i<count; i++)
        {
            buf1[i].x = buf[i].x;
            buf1[i].y = buf[i].y;
        }
        Mat mat_img(img);

        cvPolyLine(img, &buf, &count, 1, 1, CV_RGB(0, 100, 0), 1, CV_AA, 0);//画出线。
        for(int i=0;i<count-1;i++)
        {
            cvCircle(img,cvPoint(buf1[i].x,buf1[i].y),5,CV_RGB(0,100,0));
        //dashLine(mat_img,buf1[i],buf1[i+1],100);
        }
        //dashLine(mat_img,buf1[i],buf1[0],100);*/
        //draw_subdiv_point(img, pt->pt, CV_RGB(0, 0, 0));//用黑色画出画出剖分顶点。
    }
    free(buf);
}

static void paint_voronoi(CvSubdiv2D* subdiv, IplImage* img)//画出voronoi面
{
    CvSeqReader  reader;
    int i, total = subdiv->edges->total;//边缘总数
    int elem_size = subdiv->edges->elem_size;//边缘的大小

    cvCalcSubdivVoronoi2D(subdiv);

    cvStartReadSeq((CvSeq*)(subdiv->edges), &reader, 0);

    for (i = 0; i < total; i++)
    {
        CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr);//获取四方边缘

        if (CV_IS_SET_ELEM(edge))//判断边缘是否在边缘集中
        {
            CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge;//edge是四方边缘的指针,而CvSubdiv2DEdge高位表示四方边缘的指针。
            //cout<<(e&3)<<endl;//通过测试e低2位即索引值应该设置为0了,即输入边缘
            // left
            draw_subdiv_facet(img, cvSubdiv2DRotateEdge(e, 1));//e为Delaunay边,获得Delaunay边对应的voronoi边,即e的旋转边缘
            
            // right
            draw_subdiv_facet(img, cvSubdiv2DRotateEdge(e, 3));//反向的旋转边缘
        }

        CV_NEXT_SEQ_ELEM(elem_size, reader);//移动到下一个位置
    }
}


static void run(void)
{
    char win[] = "source";
    int i;
    CvRect rect = { 0, 0, 600, 600 };
    CvMemStorage* storage;
    CvSubdiv2D* subdiv;
    IplImage* img;
    CvScalar active_facet_color, delaunay_color, voronoi_color, bkgnd_color;

    active_facet_color = CV_RGB(255, 0, 0);//红色
    delaunay_color = CV_RGB(0, 0, 0);//黑色
    voronoi_color = CV_RGB(0, 180, 0);//绿色
    bkgnd_color = CV_RGB(255, 255, 255);//白色

    img = cvCreateImage(cvSize(rect.width, rect.height), 8, 3);
    cvSet(img, bkgnd_color, 0);

    cvNamedWindow(win, 1);

    storage = cvCreateMemStorage(0);
    subdiv = init_delaunay(storage, rect);

    printf("Delaunay triangulation will be build now interactively.\n"
        "To stop the process, press any key\n\n");

    vector<CvPoint2D32f> points;
    for (i = 0; i < 5; i++)
    {
        CvPoint2D32f fp = cvPoint2D32f((float)(rand() % (rect.width - 10)),//使点约束在距离边框10像素之内。
            (float)(rand() % (rect.height - 10)));
        points.push_back(fp);

        locate_point(subdiv, fp, img, active_facet_color);//定位点的位置,并画出点所在delaunay面的边。
        cvShowImage(win, img);//刷新显示

    /*  if (cvWaitKey(100) >= 0)
            break;*/ 
        //cvWaitKey(0);

        cvSubdivDelaunay2DInsert(subdiv, fp);//向三角剖分中插入该点,即对该点进行三角剖分
        cvCalcSubdivVoronoi2D(subdiv);//计算Voronoi细分,有时候我们不需要
        cvSet(img, bkgnd_color, 0);//设置图像的背景颜色为白色
        draw_subdiv(img, subdiv, delaunay_color, voronoi_color);
        cvShowImage(win, img);

        //cvWaitKey();
        /*if (cvWaitKey(100) >= 0)
            break;*/
        //cvWaitKey(0);
    }
    for (int i = 0; i<points.size(); i++)
        draw_subdiv_point(img, points[i], active_facet_color);
    cvShowImage(win, img);
    cvWaitKey();

    //  cvSet( img, bkgnd_color, 0 );//重新刷新画布,即设置背景颜色为白色
    paint_voronoi(subdiv, img);//画出细分
    cvShowImage(win, img);//

    cvWaitKey(0);

    cvReleaseMemStorage(&storage);
    cvReleaseImage(&img);
    cvDestroyWindow(win);
}

int main(int argc, char** argv)
{
    (void)argc; (void)argv;
    run();
    return 0;
}

#ifdef _EiC
main(1, "delaunay.c");
#endif
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272

推荐阅读更多精彩内容