This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
placing_markers.py
68 lines (48 loc) · 1.58 KB
/
placing_markers.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
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
def make_colors(n):
colors = []
for i in range(n):
colors.append(tuple(np.array(cm.tab10(i))[:3] * 255))
return colors
# print(make_colors(10))
# exit(0)
road = cv2.imread("./data/IMG_0121.jpeg")
road = cv2.resize(road, (0,0), fx=0.2, fy=0.2)
road_copy = road.copy()
markers = np.zeros(road.shape[:-1], dtype="int32")
segments = np.zeros_like(road)
cv2.namedWindow("Road Image", cv2.WINDOW_NORMAL)
cv2.namedWindow("Segments", cv2.WINDOW_NORMAL)
current_marker = 1
updated = False
colors = make_colors(10)
def on_mouse_click(event, x, y, flags, param):
global updated
if event == cv2.EVENT_LBUTTONDOWN:
cv2.circle(markers, (x, y), 10, (current_marker), -1)
cv2.circle(road_copy, (x, y), 10, colors[current_marker], -1)
updated = True
cv2.setMouseCallback("Road Image", on_mouse_click)
while True:
cv2.imshow("Road Image", road_copy)
cv2.imshow("Segments", segments)
k = cv2.waitKey(1)
if k == 27:
break
elif k == ord('c'):
road_copy = road.copy()
markers = np.zeros(road.shape[:-1], dtype="int32")
segments = np.zeros_like(road)
elif k > 0 and chr(k).isdigit():
current_marker = int(chr(k))
if updated:
markers_copy = markers.copy()
cv2.watershed(road, markers_copy)
segments = np.zeros(road.shape, dtype=np.uint8)
for color_ind in range(10):
segments[markers_copy == (color_ind)] = colors[color_ind]
updated = False
cv2.destroyAllWindows()