forked from shingo-kagami/ic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththresh_mouse_rect_color.py
67 lines (51 loc) · 1.76 KB
/
thresh_mouse_rect_color.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
import sys
import cv2
import numpy as np
from numba import jit
import ic_utils as ic
@jit
def threshold_impl(src, thresh, maxval):
width = src.shape[1]
height = src.shape[0]
dest = np.zeros_like(src)
for j in range(height):
for i in range(width):
if src[j, i] > thresh:
dest[j, i] = maxval
else:
dest[j, i] = 0
return dest
def main():
cap = ic.select_capture_source(sys.argv)
cv2.namedWindow('result')
cv2.createTrackbar('thresh', 'result', 128, 255, ic.do_nothing)
mstate = {
'selection': 'invalid',
'xybegin': (-1, -1),
'xyend': (-1, -1),
}
cv2.setMouseCallback('result', ic.on_mouse_rect, mstate)
while True:
grabbed, frame = cap.read()
if not grabbed:
break
img_color = frame.copy()
img_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
th = cv2.getTrackbarPos('thresh', 'result')
xbegin, ybegin = mstate['xybegin']
xend, yend = mstate['xyend']
if mstate['selection'] == 'valid':
roi_gray = img_gray[ybegin:yend, xbegin:xend]
thresh_roi = threshold_impl(roi_gray, th, 255)
thresh_roi_color = cv2.cvtColor(thresh_roi, cv2.COLOR_GRAY2BGR)
img_color[ybegin:yend, xbegin:xend] = thresh_roi_color
elif mstate['selection'] == 'ongoing':
cv2.rectangle(img_color, (xbegin, ybegin), (xend, yend),
color=(0, 0, 0), thickness=2)
cv2.imshow('result', img_color)
key = cv2.waitKey(30)
if key == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()