使用Python的cv2.createTrackbar
我刚开始学习Python和OpenCV,想要创建一个滑动条来控制函数cv2.findContours的层级,但我不知道怎么把它加到源代码里。以下是我的代码:
import cv2
import cv2.cv as cv
cv2.namedWindow("test")
vc = cv2.VideoCapture(2);
retVal, frame = vc.read();
while True:
if frame is not None:
imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(frame, contours, -1, (0,255,0), 2)
cv2.imshow("test", frame)
rval, frame = vc.read()
if cv2.waitKey(1) & 0xFF == 27:
break
cv.DestroyAllWindows()
提前谢谢你们!
1 个回答
11
我希望你现在已经解决了你的问题,但如果还没解决,我来给你解释一下。你可以使用一个叫做“命名窗口”的函数来创建一个窗口,然后把滑动条和这个窗口关联起来。
cv2.namedWindow('test')
cv2.createTrackbar('thrs1', 'test', 300, 800, callback)
# Do whatever you want with contours
cv2.imshow('test', frame)
你可以在这里找到关于创建滑动条的函数的详细说明:cv2.createTrackbar
callback是一个指针,指向一个函数,这个函数会在滑动条位置改变时被调用。