Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Delaunay/delaunay.cpp
3118 views
1
#include <opencv2/imgproc/imgproc.hpp>
2
#include <opencv2/highgui/highgui.hpp>
3
#include <iostream>
4
#include <fstream>
5
6
using namespace cv;
7
using namespace std;
8
9
// Draw a single point
10
static void draw_point( Mat& img, Point2f fp, Scalar color )
11
{
12
circle( img, fp, 2, color, cv::FILLED, cv::LINE_AA, 0 );
13
}
14
15
// Draw delaunay triangles
16
static void draw_delaunay( Mat& img, Subdiv2D& subdiv, Scalar delaunay_color )
17
{
18
19
vector<Vec6f> triangleList;
20
subdiv.getTriangleList(triangleList);
21
vector<Point> pt(3);
22
Size size = img.size();
23
Rect rect(0,0, size.width, size.height);
24
25
for( size_t i = 0; i < triangleList.size(); i++ )
26
{
27
Vec6f t = triangleList[i];
28
pt[0] = Point(cvRound(t[0]), cvRound(t[1]));
29
pt[1] = Point(cvRound(t[2]), cvRound(t[3]));
30
pt[2] = Point(cvRound(t[4]), cvRound(t[5]));
31
32
// Draw rectangles completely inside the image.
33
if ( rect.contains(pt[0]) && rect.contains(pt[1]) && rect.contains(pt[2]))
34
{
35
line(img, pt[0], pt[1], delaunay_color, 1, cv::LINE_AA, 0);
36
line(img, pt[1], pt[2], delaunay_color, 1, cv::LINE_AA, 0);
37
line(img, pt[2], pt[0], delaunay_color, 1, cv::LINE_AA, 0);
38
}
39
}
40
}
41
42
//Draw voronoi diagrams
43
static void draw_voronoi( Mat& img, Subdiv2D& subdiv )
44
{
45
vector<vector<Point2f> > facets;
46
vector<Point2f> centers;
47
subdiv.getVoronoiFacetList(vector<int>(), facets, centers);
48
49
vector<Point> ifacet;
50
vector<vector<Point> > ifacets(1);
51
52
for( size_t i = 0; i < facets.size(); i++ )
53
{
54
ifacet.resize(facets[i].size());
55
for( size_t j = 0; j < facets[i].size(); j++ )
56
ifacet[j] = facets[i][j];
57
58
Scalar color;
59
color[0] = rand() & 255;
60
color[1] = rand() & 255;
61
color[2] = rand() & 255;
62
fillConvexPoly(img, ifacet, color, 8, 0);
63
64
ifacets[0] = ifacet;
65
polylines(img, ifacets, true, Scalar(), 1, cv::LINE_AA, 0);
66
circle(img, centers[i], 3, Scalar(), cv::FILLED, cv::LINE_AA, 0);
67
}
68
}
69
70
int main( int argc, char** argv)
71
{
72
73
// Define window names
74
string win_delaunay = "Delaunay Triangulation";
75
string win_voronoi = "Voronoi Diagram";
76
77
// Turn on animation while drawing triangles
78
bool animate = true;
79
80
// Define colors for drawing.
81
Scalar delaunay_color(255,255,255), points_color(0, 0, 255);
82
83
// Read in the image.
84
Mat img = imread("obama.jpg");
85
86
// Keep a copy around
87
Mat img_orig = img.clone();
88
89
// Rectangle to be used with Subdiv2D
90
Size size = img.size();
91
Rect rect(0, 0, size.width, size.height);
92
93
// Create an instance of Subdiv2D
94
Subdiv2D subdiv(rect);
95
96
// Create a vector of points.
97
vector<Point2f> points;
98
99
// Read in the points from a text file
100
ifstream ifs("obama.txt");
101
int x, y;
102
while(ifs >> x >> y)
103
{
104
points.push_back(Point2f(x,y));
105
}
106
107
// Insert points into subdiv
108
for( vector<Point2f>::iterator it = points.begin(); it != points.end(); it++)
109
{
110
subdiv.insert(*it);
111
// Show animation
112
if (animate)
113
{
114
Mat img_copy = img_orig.clone();
115
// Draw delaunay triangles
116
draw_delaunay( img_copy, subdiv, delaunay_color );
117
imshow(win_delaunay, img_copy);
118
waitKey(100);
119
}
120
}
121
122
// Draw delaunay triangles
123
draw_delaunay( img, subdiv, delaunay_color );
124
125
// Draw points
126
for( vector<Point2f>::iterator it = points.begin(); it != points.end(); it++)
127
{
128
draw_point(img, *it, points_color);
129
}
130
131
// Allocate space for voronoi Diagram
132
Mat img_voronoi = Mat::zeros(img.rows, img.cols, CV_8UC3);
133
134
// Draw voronoi diagram
135
draw_voronoi( img_voronoi, subdiv );
136
137
// Show results.
138
imshow( win_delaunay, img);
139
imshow( win_voronoi, img_voronoi);
140
waitKey(0);
141
142
return 0;
143
}
144
145