繁体   English   中英

OpenCV 拍黑照

[英]OpenCV takes black picture

所以我在互联网上找到了这个应该拍照的代码,并对其进行了一些修改,所以它不会给我一堆错误:

import cv2

videoCaptureObject = cv2.VideoCapture(1,cv2.CAP_DSHOW)
result = True
while(result):
    ret,frame = videoCaptureObject.read()
    cv2.imwrite("NewPicture.jpg",frame)
    result = False
videoCaptureObject.release()
cv2.destroyAllWindows()

但是当我拍照时,照片只是黑色。

我该如何解决?

您可以尝试通过按下键盘上的特定按键来“运行”相机并随时拍摄(帧的)照片。

import cv2

# Video captute
# 0 or 1 based on the type and the number of your cameras
cap = cv2.VideoCapture(1)

while True:
    _, frame = cap.read()      # We don't want ret in this
    cv2.imshow("Frame", frame) # Show the current frame

    key = cv2.waitKey(1)
    if key==27: # If you press Esc then the frame window will close (and the program also)
        break
    elif key==ord('p'): # If you press p/P key on your keyboard
        cv2.imwrite("path.../pic.jpg", frame) # Save current frame as picture with name pic.jpg

cap.release()
cv2.destroyAllWindows()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM