Why is cv2.imwrite 1 step behind?

My code is:

import cv2

cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
    key = cv2.waitKey(0) & 0xFF
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    if key == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
    if key == ord('c'):
        cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()
  • I am running this code.
  • A gray display is displayed.
  • I point the camera at the object and press the 'c' key.
  • This does not show the image of the object, but the image of what the camera points to when I run the code and saves it.
  • I point the camera somewhere else and press the c key.
  • It displays the image of the object that it saw in 3. and saves it.

The camera is 1 step behind. Why?

+4
source share
1 answer

This may be due to the absence cv::waitKey(0), and the window does not refresh, although it is odd.

Try adding a command cv::waitKeyafter imshow like this

import cv2

cap = cv2.VideoCapture(0)
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
while True:
    key = cv2.waitKey(0) & 0xFF
    ret, frame = cap.read()
    cv2.imshow('frame', frame)
    cv2.waitKey(0)
    if key == ord('q'):
        cap.release()
        cv2.destroyAllWindows()
        break
    if key == ord('c'):
        cv2.imwrite('capture.jpg', frame)
cap.release()
cv2.destroyAllWindows()

, , imwrite, while ( ), - opencv.

+1

Source: https://habr.com/ru/post/1695199/


All Articles