2
2
import cv2
3
3
4
4
img = cv2 .imread ('cafe.jpg' ) #import example image
5
- mask = np .zeros_like (cv2 .imread ('cafe.jpg' , 0 )) #black
6
-
5
+ mask = np .zeros_like (img ) #black
6
+ # red, orange, yellow, green, blue - can add more/adjust
7
+ colors = [(0 , 0 , 255 ), (28 , 172 , 255 ), (15 , 196 , 241 ), (0 , 255 , 0 ), (255 , 0 , 0 )]
8
+ color_i = 0 #keep track of which color to use
7
9
shapes = [] #list to store all drawn areas
8
10
curr_shape_index = [] #list to store the indices of the polygon currently drawing
9
11
def draw_polygon (event , x , y , flags , params ):
10
- global shapes , curr_shape_index
12
+ global shapes , curr_shape_index , colors , color_i
11
13
color = (0 , 0 , 255 ) #red
12
14
thickness = 5
13
15
if event == cv2 .EVENT_LBUTTONDOWN : #for all points except last, left click
@@ -18,7 +20,8 @@ def draw_polygon(event, x, y, flags, params):
18
20
pts = np .array (curr_shape_index , np .int32 )
19
21
20
22
cv2 .polylines (img , [pts ], isClosed , color , thickness ) #draw polygon on img
21
- cv2 .fillPoly (mask , [pts ], 255 ) #fill in white where areas are
23
+ cv2 .fillPoly (mask , [pts ], colors [color_i ]) #fill in where areas are w/diff colors
24
+ color_i = (1 + color_i ) % len (colors ) #to prevent index from going out of bounds
22
25
shapes .append (curr_shape_index .copy ()) #save polygon
23
26
curr_shape_index = [] #reset current indices list
24
27
0 commit comments