1
#include <opencv2/imgproc/imgproc.hpp>
2
#include <opencv2/highgui/highgui.hpp>
10
cout << "\nThis program demostrates iterative construction of\n"
11
"delaunay triangulation and voronoi tesselation.\n"
12
"It draws a random set of points in an image and then delaunay triangulates them.\n"
15
"\nThis program builds the traingulation interactively, you may stop this process by\n"
19
static void draw_subdiv_point( Mat& img, Point2f fp, Scalar color )
21
circle( img, fp, 3, color, FILLED, LINE_8, 0 );
24
static void draw_subdiv( Mat& img, Subdiv2D& subdiv, Scalar delaunay_color )
27
vector<Vec6f> triangleList;
28
subdiv.getTriangleList(triangleList);
31
for( size_t i = 0; i < triangleList.size(); i++ )
33
Vec6f t = triangleList[i];
34
pt[0] = Point(cvRound(t[0]), cvRound(t[1]));
35
pt[1] = Point(cvRound(t[2]), cvRound(t[3]));
36
pt[2] = Point(cvRound(t[4]), cvRound(t[5]));
37
line(img, pt[0], pt[1], delaunay_color, 1, LINE_AA, 0);
38
line(img, pt[1], pt[2], delaunay_color, 1, LINE_AA, 0);
39
line(img, pt[2], pt[0], delaunay_color, 1, LINE_AA, 0);
42
vector<Vec4f> edgeList;
43
subdiv.getEdgeList(edgeList);
44
for( size_t i = 0; i < edgeList.size(); i++ )
46
Vec4f e = edgeList[i];
47
Point pt0 = Point(cvRound(e[0]), cvRound(e[1]));
48
Point pt1 = Point(cvRound(e[2]), cvRound(e[3]));
49
line(img, pt0, pt1, delaunay_color, 1, LINE_AA, 0);
54
static void locate_point( Mat& img, Subdiv2D& subdiv, Point2f fp, Scalar active_color )
58
subdiv.locate(fp, e0, vertex);
66
if( subdiv.edgeOrg(e, &org) > 0 && subdiv.edgeDst(e, &dst) > 0 )
67
line( img, org, dst, active_color, 3, LINE_AA, 0 );
69
e = subdiv.getEdge(e, Subdiv2D::NEXT_AROUND_LEFT);
74
draw_subdiv_point( img, fp, active_color );
78
static void paint_voronoi( Mat& img, Subdiv2D& subdiv )
80
vector<vector<Point2f> > facets;
81
vector<Point2f> centers;
82
subdiv.getVoronoiFacetList(vector<int>(), facets, centers);
85
vector<vector<Point> > ifacets(1);
87
for( size_t i = 0; i < facets.size(); i++ )
89
ifacet.resize(facets[i].size());
90
for( size_t j = 0; j < facets[i].size(); j++ )
91
ifacet[j] = facets[i][j];
94
color[0] = rand() & 255;
95
color[1] = rand() & 255;
96
color[2] = rand() & 255;
97
fillConvexPoly(img, ifacet, color, 8, 0);
100
polylines(img, ifacets, true, Scalar(), 1, LINE_AA, 0);
101
circle(img, centers[i], 3, Scalar(), FILLED, LINE_AA, 0);
106
int main( int argc, char** argv )
108
cv::CommandLineParser parser(argc, argv, "{help h||}");
109
if (parser.has("help"))
115
Scalar active_facet_color(0, 0, 255), delaunay_color(255,255,255);
116
Rect rect(0, 0, 600, 600);
118
Subdiv2D subdiv(rect);
119
Mat img(rect.size(), CV_8UC3);
121
img = Scalar::all(0);
122
string win = "Delaunay Demo";
125
for( int i = 0; i < 200; i++ )
127
Point2f fp( (float)(rand()%(rect.width-10)+5),
128
(float)(rand()%(rect.height-10)+5));
130
locate_point( img, subdiv, fp, active_facet_color );
133
if( waitKey( 100 ) >= 0 )
138
img = Scalar::all(0);
139
draw_subdiv( img, subdiv, delaunay_color );
142
if( waitKey( 100 ) >= 0 )
146
img = Scalar::all(0);
147
paint_voronoi( img, subdiv );