Skip to content

Commit a449115

Browse files
committed
Implemented CallBack Function and Loop
1 parent f80cd00 commit a449115

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

Diff for: segementation.py

+52
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,56 @@ def selectColor(i):
2424
colors.append(selectColor(i))
2525

2626

27+
#Global Variables
28+
color_marker=1
29+
num_marker = 10
30+
marks_updated = False
2731

32+
#CALL BACK FUNCTION
33+
def mouseCallBack(action,x,y,flags,params):
34+
global marks_updated
35+
36+
if action == cv2.EVENT_LBUTTONDOWN:
37+
cv2.circle(marker_image,(x,y),10,(color_marker),-1)
38+
39+
cv2.circle(image_copy,(x,y),10,colors[color_marker],-1)
40+
41+
marks_updated = True
42+
43+
cv2.namedWindow('Image')
44+
cv2.setMouseCallback('Image',mouseCallBack)
45+
46+
while True:
47+
cv2.putText(image_copy,'IIIIIIII',(3,15),cv2.FONT_HERSHEY_SIMPLEX,.5,colors[color_marker],2)
48+
cv2.imshow('Segments',segments)
49+
cv2.imshow('Image',image_copy)
50+
51+
k = cv2.waitKey(1)
52+
53+
if k == 27:
54+
break
55+
56+
#CLEARING COLOR
57+
elif k == ord('c'):
58+
image_copy = image.copy()
59+
marker_image = np.zeros(image.shape[:2],dtype=np.int32)
60+
segments = np.zeros(image.shape,dtype=np.int8)
61+
62+
elif k > 0 and chr(k).isdigit():
63+
color_marker = int(chr(k))
64+
65+
66+
if marks_updated:
67+
68+
marker_image_copy = marker_image.copy()
69+
70+
cv2.watershed(image, marker_image_copy)
71+
72+
segments = np.zeros(image.shape,dtype=np.uint8)
73+
74+
for color_idx in range(num_marker):
75+
segments[marker_image_copy == (color_idx)] = colors[color_idx]
76+
77+
marks_updated = False
78+
79+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)