2016-02-29 291 views
2

未運行,我需要了解在python cv2.waitkey()與CV2cv2.waitkey(1)OpenCV的蟒蛇

faceCascade = cv2.CascadeClassifier(cascPath) 

video_capture = cv2.VideoCapture(0) 

while True: 
    # Capture frame-by-frame 
ret, frame = video_capture.read() 

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

faces = faceCascade.detectMultiScale(
    gray, 
    scaleFactor=1.1, 
    minNeighbors=8, 
    minSize=(40, 40), 
    #flags=cv2.cv.CV_HAAR_SCALE_IMAGE 
    flags = 0 
) 

# Draw a rectangle around the faces 
for (x, y, w, h) in faces: 
    cv2.rectangle(gray, (x, y), (x+w, y+h), (0, 255, 0), 2) 

# Display the resulting frame 
cv2.imshow('Video', gray) 
if cv2.waitKey(1) & 0xFF == ord('q'): 
    break 
if cv2.waitKey(1) & 0xFF == ord('b'): 
    cv2.imwrite('example.png',gray) 

cv2.waitKey()當我按b鍵不保存圖片,但按q作品。請幫忙!

回答

3

您正在調用waitKey()兩次。用您的密碼,按任意鍵,然後按b,然後保存圖像。

只有調用waitKey(1)一次,並將結果保存在一個變量,然後測試該變量,例如:

keypress = cv2.waitKey(1) 
if keypress & 0xFF == ord('q'): 
    break 
if keypress & 0xFF == ord('b'): 
    cv2.imwrite('example.png',gray) 
+0

@bamyThanks人! –