forked from hibyby/GrandmaCan_python_opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial5.py
42 lines (32 loc) · 1.27 KB
/
tutorial5.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
import cv2
import numpy as np
def empty(v):
pass
img = cv2.imread('XiWinnie.jpg')
img = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
cv2.namedWindow('TrackBar')
cv2.resizeWindow('TrackBar', 640, 320)
cv2.createTrackbar('Hue Min', 'TrackBar', 0, 179, empty)
cv2.createTrackbar('Hue Max', 'TrackBar', 179, 179, empty)
cv2.createTrackbar('Sat Min', 'TrackBar', 0, 255, empty)
cv2.createTrackbar('Sat Max', 'TrackBar', 255, 255, empty)
cv2.createTrackbar('Val Min', 'TrackBar', 0, 255, empty)
cv2.createTrackbar('Val Max', 'TrackBar', 255, 255, empty)
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
while True:
h_min = cv2.getTrackbarPos('Hue Min', 'TrackBar')
h_max = cv2.getTrackbarPos('Hue Max', 'TrackBar')
s_min = cv2.getTrackbarPos('Sat Min', 'TrackBar')
s_max = cv2.getTrackbarPos('Sat Max', 'TrackBar')
v_min = cv2.getTrackbarPos('Val Min', 'TrackBar')
v_max = cv2.getTrackbarPos('Val Max', 'TrackBar')
print(h_min, h_max, s_min, s_max, v_min, v_max)
lower = np.array([h_min, s_min, v_min])
upper = np.array([h_max, s_max, v_max])
mask = cv2.inRange(hsv, lower, upper)
result = cv2.bitwise_and(img, img, mask=mask)
cv2.imshow('img', img)
cv2.imshow('hsv', hsv)
cv2.imshow('mask', mask)
cv2.imshow('reslut', result)
cv2.waitKey(1)