-
Notifications
You must be signed in to change notification settings - Fork 12
/
delaunay.py
executable file
·157 lines (115 loc) · 4.37 KB
/
delaunay.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
import cv2
import numpy as np
import random
import argparse
import os
# Check if a point is inside a rectangle
def rect_contains(rect, point) :
if point[0] < rect[0] :
return False
elif point[1] < rect[1] :
return False
elif point[0] > rect[2] :
return False
elif point[1] > rect[3] :
return False
return True
# Draw a point
def draw_point(img, p, color ) :
cv2.circle( img, p, 2, color, cv2.FILLED, cv2.LINE_AA, 0 )
# Draw delaunay triangles
def draw_delaunay(img, subdiv, delaunay_color ) :
triangleList = subdiv.getTriangleList();
size = img.shape
r = (0, 0, size[1], size[0])
for t in triangleList :
pt1 = (t[0], t[1])
pt2 = (t[2], t[3])
pt3 = (t[4], t[5])
if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3) :
cv2.line(img, pt1, pt2, delaunay_color, 1, cv2.LINE_AA, 0)
cv2.line(img, pt2, pt3, delaunay_color, 1, cv2.LINE_AA, 0)
cv2.line(img, pt3, pt1, delaunay_color, 1, cv2.LINE_AA, 0)
# Draw voronoi diagram
def draw_voronoi(img, subdiv) :
( facets, centers) = subdiv.getVoronoiFacetList([])
for i in xrange(0,len(facets)) :
ifacet_arr = []
for f in facets[i] :
ifacet_arr.append(f)
ifacet = np.array(ifacet_arr, np.int)
color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
cv2.fillConvexPoly(img, ifacet, color, cv2.LINE_AA, 0);
ifacets = np.array([ifacet])
cv2.polylines(img, ifacets, True, (0, 0, 0), 1, cv2.LINE_AA, 0)
cv2.circle(img, (centers[i][0], centers[i][1]), 3, (0, 0, 0), cv2.FILLED, cv2.LINE_AA, 0)
if __name__ == '__main__':
# Input arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())
filename = args["image"]
out_dir, basename = os.path.split(filename)
name, extension = os.path.splitext(basename)
# Define window names
win_delaunay = "Delaunay Triangulation"
win_voronoi = "Voronoi Diagram"
# Turn on animation while drawing triangles
animate = False
# Define colors for drawing.
delaunay_color = (255,255,255)
points_color = (0, 0, 255)
# Read in the image.
img = cv2.imread(filename);
# Keep a copy around
img_orig = img.copy();
# Rectangle to be used with Subdiv2D
size = img.shape
rect = (0, 0, size[1], size[0])
# Create an instance of Subdiv2D
subdiv = cv2.Subdiv2D(rect);
# Create an array of points.
points = [];
# Read in the points from a text file
points_filename = out_dir + '/' + name + '.txt'
with open(points_filename) as file :
for line in file :
x, y = line.split()
points.append((int(x), int(y)))
# Insert points into subdiv
for p in points :
subdiv.insert(p)
# Show animation
if animate :
img_copy = img_orig.copy()
# Draw delaunay triangles
draw_delaunay( img_copy, subdiv, (255, 255, 255) );
cv2.imshow(win_delaunay, img_copy)
cv2.waitKey(100)
# Draw delaunay triangles
draw_delaunay( img, subdiv, (255, 255, 255) );
# Draw points
for p in points :
draw_point(img, p, (0,0,255))
# Saves delaunay points
delaunay_file = out_dir + '/' + name + '_triangles.txt'
f = open(delaunay_file, 'wb')
f.truncate(0)
triangleList = subdiv.getTriangleList();
for t in triangleList :
if rect_contains(rect, (t[0], t[1])): f.write(str(int(t[0])) + ' ' + str(int(t[1])) + '\n')
if rect_contains(rect, (t[2], t[3])): f.write(str(int(t[2])) + ' ' + str(int(t[3])) + '\n')
if rect_contains(rect, (t[4], t[5])): f.write(str(int(t[4])) + ' ' + str(int(t[5])) + '\n')
# Allocate space for voronoi Diagram
img_voronoi = np.zeros(img.shape, dtype = img.dtype)
# Draw voronoi diagram
draw_voronoi(img_voronoi,subdiv)
# Show results
#cv2.imshow(win_delaunay,img)
#cv2.imshow(win_voronoi,img_voronoi)
#cv2.waitKey(0)
# Save results
cv2.imwrite(out_dir + '/' + name + '_delaunay.jpg', img)
cv2.imwrite(out_dir + '/' + name + '_voronoi.jpg', img_voronoi)
print('Done! results exported to ' + points_filename)