3 the script demostrates iterative construction of
4 delaunay triangulation and voronoi tesselation
6 Original Author (C version): ?
7 Converted to Python by: Roman Stanchak
9 from opencv.cv import *
10 from opencv.highgui import *
11 from random import random,randint
13 def draw_subdiv_point( img, fp, color ):
14 cvCircle( img, cvPoint(cvRound(fp.x), cvRound(fp.y)), 3, color, CV_FILLED, 8, 0 );
18 def draw_subdiv_edge( img, edge, color ):
19 org_pt = cvSubdiv2DEdgeOrg(edge);
20 dst_pt = cvSubdiv2DEdgeDst(edge);
22 if org_pt and dst_pt :
27 iorg = cvPoint( cvRound( org.x ), cvRound( org.y ));
28 idst = cvPoint( cvRound( dst.x ), cvRound( dst.y ));
30 cvLine( img, iorg, idst, color, 1, CV_AA, 0 );
33 def draw_subdiv( img, subdiv, delaunay_color, voronoi_color ):
35 total = subdiv.edges.total;
36 elem_size = subdiv.edges.elem_size;
38 for edge in subdiv.edges:
39 edge_rot = cvSubdiv2DRotateEdge( edge, 1 )
41 if( CV_IS_SET_ELEM( edge )):
42 draw_subdiv_edge( img, edge_rot, voronoi_color );
43 draw_subdiv_edge( img, edge, delaunay_color );
46 def locate_point( subdiv, fp, img, active_color ):
48 [res, e0, p] = cvSubdiv2DLocate( subdiv, fp );
53 draw_subdiv_edge( img, e, active_color );
54 e = cvSubdiv2DGetEdge(e,CV_NEXT_AROUND_LEFT);
58 draw_subdiv_point( img, fp, active_color );
61 def draw_subdiv_facet( img, edge ):
66 # count number of edges in facet
67 while count == 0 or t != edge:
69 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
75 for i in range(count):
77 pt = cvSubdiv2DEdgeOrg( t );
80 buf.append( cvPoint( cvRound(pt.pt.x), cvRound(pt.pt.y) ) );
81 t = cvSubdiv2DGetEdge( t, CV_NEXT_AROUND_LEFT );
83 if( len(buf)==count ):
84 pt = cvSubdiv2DEdgeDst( cvSubdiv2DRotateEdge( edge, 1 ));
85 cvFillConvexPoly( img, buf, CV_RGB(randint(0,255),randint(0,255),randint(0,255)), CV_AA, 0 );
86 cvPolyLine( img, [buf], 1, CV_RGB(0,0,0), 1, CV_AA, 0);
87 draw_subdiv_point( img, pt.pt, CV_RGB(0,0,0));
89 def paint_voronoi( subdiv, img ):
90 total = subdiv.edges.total;
91 elem_size = subdiv.edges.elem_size;
93 cvCalcSubdivVoronoi2D( subdiv );
95 for edge in subdiv.edges:
97 if( CV_IS_SET_ELEM( edge )):
99 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( edge, 1 ));
102 draw_subdiv_facet( img, cvSubdiv2DRotateEdge( edge, 3 ));
104 if __name__ == '__main__':
106 rect = cvRect( 0, 0, 600, 600 );
108 active_facet_color = CV_RGB( 255, 0, 0 );
109 delaunay_color = CV_RGB( 0,0,0);
110 voronoi_color = CV_RGB(0, 180, 0);
111 bkgnd_color = CV_RGB(255,255,255);
113 img = cvCreateImage( cvSize(rect.width,rect.height), 8, 3 );
114 cvSet( img, bkgnd_color );
116 cvNamedWindow( win, 1 );
118 storage = cvCreateMemStorage(0);
119 subdiv = cvCreateSubdivDelaunay2D( rect, storage );
121 print "Delaunay triangulation will be build now interactively."
122 print "To stop the process, press any key\n";
125 fp = cvPoint2D32f( random()*(rect.width-10)+5, random()*(rect.height-10)+5 )
127 locate_point( subdiv, fp, img, active_facet_color );
128 cvShowImage( win, img );
130 if( cvWaitKey( 100 ) >= 0 ):
133 cvSubdivDelaunay2DInsert( subdiv, fp );
134 cvCalcSubdivVoronoi2D( subdiv );
135 cvSet( img, bkgnd_color );
136 draw_subdiv( img, subdiv, delaunay_color, voronoi_color );
137 cvShowImage( win, img );
139 if( cvWaitKey( 100 ) >= 0 ):
143 cvSet( img, bkgnd_color );
144 paint_voronoi( subdiv, img );
145 cvShowImage( win, img );
149 cvDestroyWindow( win );