-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultipleRoiSelector.py
131 lines (102 loc) · 3.66 KB
/
multipleRoiSelector.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import numpy as np
import argparse
import glob
import json
import sys
import cv2
def parseArgs():
parser = argparse.ArgumentParser(description='Roi Selector with OpenCV')
parser.add_argument( # Path to image Files
'--path',
dest='imagePath',
help='path to image files',
default='images',
type=str
)
parser.add_argument( # Image extensions
'--ext',
dest='imageExt',
help='image extension',
default='jpg',
type=str
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
return parser.parse_args()
def printInfos():
print('[INFO] This is a Multiple ROI selector script for multiple images')
print('[INFO] Click the left button: select the point, right click: delete the last selected point')
print('[INFO] Press ‘S’ to save all ROIs')
print('[INFO] Press ‘D’ to delete last selected ROI')
print('[INFO] Press ESC to quit')
def getImagesFromFile(args):
images = glob.glob(args.imagePath + '/*.' + args.imageExt)
return images
def plotSegmentedRois(imTemp, imMask, rois, colors):
for qq, rs in enumerate(rois):
roiPoints = rs['Points']
roiPoints = np.array(roiPoints, np.int32)
roiPoints = roiPoints.reshape((-1, 1, 2))
imMask = cv2.fillPoly(imMask, [roiPoints], colors[qq%len(colors)])
imTemp = cv2.addWeighted(imTemp, 0.5, imMask, 0.5, 0)
return imTemp, imMask
def checkRoiClosed(pts,distTh=5):
points = np.array(pts, np.int32)
dist = np.linalg.norm(points[0] - points[-1])
if dist < distTh:
return True
return False
def drawRoi(event, x, y, flags, param):
global pts, id
colors = [(255,0,0),(0,255,0), (0,0,255), (204,78,1), (79,0,128), (255,255,85), (79,186,218), (229,152,230), (88,0,144)]
imTemp = im.copy()
imMask = im.copy()
if event == cv2.EVENT_LBUTTONDOWN:
pts.append((x, y))
if event == cv2.EVENT_RBUTTONDOWN:
if len(pts)>0:
pts.pop()
imTemp, imMask = plotSegmentedRois(imTemp, imMask, rois, colors)
if len(pts) > 0:
cv2.circle(imTemp, pts[-1], 1, (0, 0, 255), -1)
if len(pts) > 1:
if checkRoiClosed(pts,distTh=5):
rois.append({'Id': id, 'Points': pts})
id += 1
pts = []
for i in range(len(pts) - 1):
cv2.circle(imTemp, pts[i], 1, (0, 0, 255), -1)
cv2.line(imTemp, pt1=pts[i], pt2=pts[i + 1], color=(255, 0, 0), thickness=1)
cv2.imshow('image', imTemp)
def roiSelector(image):
global im, key, pts, rois, id
id = 1
pts = []
rois = []
im = cv2.imread(image)
cv2.namedWindow('image',cv2.WINDOW_FREERATIO)
cv2.imshow('image', im)
cv2.setMouseCallback('image', drawRoi)
while True:
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
if chr(key).lower() == 'd':
if len(rois) < 1: continue
rois.pop()
pts = []
id -= 1
if chr(key).lower() == 's':
rois.append({'Id': id, 'Points': pts})
jsonName = image.split('.')[0] + '.json'
with open(jsonName,'w') as jsonRoi:
json.dump(rois, jsonRoi)
break
cv2.destroyAllWindows()
if __name__ == '__main__':
printInfos()
args = parseArgs() # Parse input parameters
imageFiles = getImagesFromFile(args) # Get image names from given path and extension
for image in imageFiles: # Loop all images and run roi selector
roiSelector(image) # Function to select and save rois