Skip to content

Commit c3f12c2

Browse files
authored
Update area_select.py
1 parent e8753a9 commit c3f12c2

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

area_select.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
import cv2
33

44
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
79
shapes = [] #list to store all drawn areas
810
curr_shape_index = [] #list to store the indices of the polygon currently drawing
911
def draw_polygon(event, x, y, flags, params):
10-
global shapes, curr_shape_index
12+
global shapes, curr_shape_index, colors, color_i
1113
color = (0, 0, 255) #red
1214
thickness = 5
1315
if event == cv2.EVENT_LBUTTONDOWN: #for all points except last, left click
@@ -18,7 +20,8 @@ def draw_polygon(event, x, y, flags, params):
1820
pts = np.array(curr_shape_index, np.int32)
1921

2022
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
2225
shapes.append(curr_shape_index.copy()) #save polygon
2326
curr_shape_index = [] #reset current indices list
2427

0 commit comments

Comments
 (0)