python opencv trackbar 调整参数
由于最近需要利用到Python 版本的opencv 但是之前又没有接触过 这个,在此纪念下利用 trackbar 去调整参数的方法。
mport cv2
import numpy as np
#optional argument
def nothing(x):
pass
cv2.namedWindow('image')
#easy assigments
hh='Hue High'
hl='Hue Low'
sh='Saturation High'
sl='Saturation Low'
vh='Value High'
vl='Value Low'
cv2.createTrackbar(hl, 'image',0,179,nothing)
cv2.createTrackbar(hh, 'image',0,179,nothing)
cv2.createTrackbar(sl, 'image',0,255,nothing)
cv2.createTrackbar(sh, 'image',0,255,nothing)
cv2.createTrackbar(vl, 'image',0,255,nothing)
cv2.createTrackbar(vh, 'image',0,255,nothing)
while(1):
frame = cv2.imread("55.png")
frame=cv2.GaussianBlur(frame,(5,5),0)
#convert to HSV from BGR
hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#read trackbar positions for all
hul=cv2.getTrackbarPos(hl, 'image')
huh=cv2.getTrackbarPos(hh, 'image')
sal=cv2.getTrackbarPos(sl, 'image')
sah=cv2.getTrackbarPos(sh, 'image')
val=cv2.getTrackbarPos(vl, 'image')
vah=cv2.getTrackbarPos(vh, 'image')
print "hul is :",hul
print "huh is :",huh
print "sal is:",sal
print "sah is:",sah
print "val is:",val
print "vah is:",vah
#make array for final values
HSVLOW=np.array([hul,sal,val])
HSVHIGH=np.array([huh,sah,vah])
#apply the range on a mask
mask = cv2.inRange(hsv,HSVLOW, HSVHIGH)
res = cv2.bitwise_and(frame,frame, mask =mask)
cv2.imshow('mask',mask)
cv2.imshow('dst', res)
cv2.imshow('yay', frame)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()