8.OpenCV的使用跟踪栏
跟踪栏(Trackbar)是OpenCV为图像窗口提供的交互工具。用户可以通过跟踪栏中的滑块位置获取特定范围内的值。
(1) cv2.createTrackbar()函数用于创建跟踪栏。
(2) cv2.getTrackbarPos()函数用于返回跟踪栏的当前值。
# 创建黑色画布
img = np.zeros((576,1024,3), dtype='uint8')
def doChange(x):
b = cv2.getTrackbarPos('B','tracebar')
g = cv2.getTrackbarPos('G','tracebar')
r = cv2.getTrackbarPos('R','tracebar')
img[:] = [b,g,r]
cv2.namedWindow('tracebar')
cv2.createTrackbar('B','tracebar',0,255,doChange)
cv2.createTrackbar('G','tracebar',0,255,doChange)
cv2.createTrackbar('R','tracebar',0,255,doChange)
while(True):
cv2.imshow('tracebar',img)
k = cv2.waitKey(1)
if k == 27:
break
cv2.destroyAllWindows()
img = img2 = cv2.imread('bee.jpg')
def doChange(x):
global img
bgr = cv2.getTrackbarPos('BGR','ShowBGR')
if bgr == 0:
img = img2
else:
img = img2[:,:,bgr-1]
cv2.namedWindow('ShowBGR')
cv2.createTrackbar('BGR','ShowBGR',0,3,doChange)
while(True):
cv2.imshow('ShowBGR',img)
k = cv2.waitKey(1)
if k == 27:
break
cv2.destroyAllWindows()
以上内容介绍了OpenCV-Python简单的跟踪栏操作,有关Python、数据科学、人工智能等文章后续会不定期发布,请大家多多关注,一键三连哟(●’◡’●)。