#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
delaunay細分和 Voronoi細分的創建和遍歷
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 博主強烈建議跳過分割線前面的部分,直接看下文更新的那些即可。 最近在學習二叉樹的相關知識,一開始真的是毫無頭緒。本...
- 鄉愁,曾經是余光中膾炙人口的詩篇。 鄉愁,曾經是那一片煙臘肉,那一塊口水雞,那一個茴香餃子。 鄉愁,曾經是真真正正...