-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegementation.py
79 lines (53 loc) · 1.93 KB
/
segementation.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
69
70
71
72
73
74
75
76
77
78
79
'''
This algorithm is usefull for segmenting images into background and
foreground in situations that are difficult for other algorithm
'''
import cv2
import numpy as np
from matplotlib import cm
image = cv2.imread('/mnt/488266AE8266A064/Images/nature4.jpg')
#image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
image = cv2.resize(image,(800,400))
image_copy = image.copy()
marker_image = np.zeros(image.shape[:2],dtype=np.int32)
segments = np.zeros(image.shape,dtype=np.int8)
def selectColor(i):
return tuple(np.array(cm.tab10(i)[:3])*255)
colors=[]
for i in range(10):
colors.append(selectColor(i))
#Global Variables
color_marker=1
num_marker = 10
marks_updated = False
#CALL BACK FUNCTION
def mouseCallBack(action,x,y,flags,params):
global marks_updated
if action == cv2.EVENT_LBUTTONDOWN:
cv2.circle(marker_image,(x,y),10,(color_marker),-1)
cv2.circle(image_copy,(x,y),10,colors[color_marker],-1)
marks_updated = True
cv2.namedWindow('Image')
cv2.setMouseCallback('Image',mouseCallBack)
while True:
cv2.putText(image_copy,'IIIIIIII',(3,15),cv2.FONT_HERSHEY_SIMPLEX,.5,colors[color_marker],2)
cv2.imshow('Segments',segments)
cv2.imshow('Image',image_copy)
k = cv2.waitKey(1)
if k == 27:
break
#CLEARING COLOR
elif k == ord('c'):
image_copy = image.copy()
marker_image = np.zeros(image.shape[:2],dtype=np.int32)
segments = np.zeros(image.shape,dtype=np.int8)
elif k > 0 and chr(k).isdigit():
color_marker = int(chr(k))
if marks_updated:
marker_image_copy = marker_image.copy()
cv2.watershed(image, marker_image_copy)
segments = np.zeros(image.shape,dtype=np.uint8)
for color_idx in range(num_marker):
segments[marker_image_copy == (color_idx)] = colors[color_idx]
marks_updated = False
cv2.destroyAllWindows()