Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Delaunay/delaunay.py
3118 views
1
#!/usr/bin/python
2
3
import cv2
4
import numpy as np
5
import random
6
7
# Check if a point is inside a rectangle
8
def rect_contains(rect, point) :
9
if point[0] < rect[0] :
10
return False
11
elif point[1] < rect[1] :
12
return False
13
elif point[0] > rect[2] :
14
return False
15
elif point[1] > rect[3] :
16
return False
17
return True
18
19
# Draw a point
20
def draw_point(img, p, color ) :
21
cv2.circle( img, p, 2, color, cv2.cv.CV_FILLED, cv2.CV_AA, 0 )
22
23
24
# Draw delaunay triangles
25
def draw_delaunay(img, subdiv, delaunay_color ) :
26
27
triangleList = subdiv.getTriangleList();
28
size = img.shape
29
r = (0, 0, size[1], size[0])
30
31
for t in triangleList :
32
33
pt1 = (t[0], t[1])
34
pt2 = (t[2], t[3])
35
pt3 = (t[4], t[5])
36
37
if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3) :
38
39
cv2.line(img, pt1, pt2, delaunay_color, 1, cv2.CV_AA, 0)
40
cv2.line(img, pt2, pt3, delaunay_color, 1, cv2.CV_AA, 0)
41
cv2.line(img, pt3, pt1, delaunay_color, 1, cv2.CV_AA, 0)
42
43
44
# Draw voronoi diagram
45
def draw_voronoi(img, subdiv) :
46
47
( facets, centers) = subdiv.getVoronoiFacetList([])
48
49
for i in xrange(0,len(facets)) :
50
ifacet_arr = []
51
for f in facets[i] :
52
ifacet_arr.append(f)
53
54
ifacet = np.array(ifacet_arr, np.int)
55
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
56
57
cv2.fillConvexPoly(img, ifacet, color, cv2.CV_AA, 0);
58
ifacets = np.array([ifacet])
59
cv2.polylines(img, ifacets, True, (0, 0, 0), 1, cv2.CV_AA, 0)
60
cv2.circle(img, (centers[i][0], centers[i][1]), 3, (0, 0, 0), cv2.cv.CV_FILLED, cv2.CV_AA, 0)
61
62
63
if __name__ == '__main__':
64
65
# Define window names
66
win_delaunay = "Delaunay Triangulation"
67
win_voronoi = "Voronoi Diagram"
68
69
# Turn on animation while drawing triangles
70
animate = True
71
72
# Define colors for drawing.
73
delaunay_color = (255,255,255)
74
points_color = (0, 0, 255)
75
76
# Read in the image.
77
img = cv2.imread("obama.jpg");
78
79
# Keep a copy around
80
img_orig = img.copy();
81
82
# Rectangle to be used with Subdiv2D
83
size = img.shape
84
rect = (0, 0, size[1], size[0])
85
86
# Create an instance of Subdiv2D
87
subdiv = cv2.Subdiv2D(rect);
88
89
# Create an array of points.
90
points = [];
91
92
# Read in the points from a text file
93
with open("obama.txt") as file :
94
for line in file :
95
x, y = line.split()
96
points.append((int(x), int(y)))
97
98
# Insert points into subdiv
99
for p in points :
100
subdiv.insert(p)
101
102
# Show animation
103
if animate :
104
img_copy = img_orig.copy()
105
# Draw delaunay triangles
106
draw_delaunay( img_copy, subdiv, (255, 255, 255) );
107
cv2.imshow(win_delaunay, img_copy)
108
cv2.waitKey(100)
109
110
# Draw delaunay triangles
111
draw_delaunay( img, subdiv, (255, 255, 255) );
112
113
# Draw points
114
for p in points :
115
draw_point(img, p, (0,0,255))
116
117
# Allocate space for voronoi Diagram
118
img_voronoi = np.zeros(img.shape, dtype = img.dtype)
119
120
# Draw voronoi diagram
121
draw_voronoi(img_voronoi,subdiv)
122
123
# Show results
124
cv2.imshow(win_delaunay,img)
125
cv2.imshow(win_voronoi,img_voronoi)
126
cv2.waitKey(0)
127
128
129
130
131
132
133