OpenCV网络摄像头图像损坏

6za6bjd0  于 2023-03-13  发布在  其他
关注(0)|答案(3)|浏览(265)

我在Python3和OpenCV 3.1中为Odroid C1上的延时照片编写了一个脚本:
不幸的是,有时从网络摄像头的图像是混乱的:下面是一些示例照片。
第一节第一节第一节第一节第一次
下面是源代码:

import numpy as np
import cv2
import time
import datetime
import sys

cap = cv2.VideoCapture(0) # USB webcam

cap.set(3,2304.0) #  Resolution: Width
cap.set(4,1296.0) # Resolution: Height
cap.set(5, 6) #webcam capture FPS 
print(str(cap.get(3)),str(cap.get(4)),str(cap.get(5)))

def captureImage():
         ret, frame = cap.read()
         curr_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
         filename = 'littleGarden_'+curr_time +'.jpg'
         file_to_save = folder_to_save + filename
         cv2.imwrite(file_to_save,frame)
         print("File saved ", file_to_save)

ret, frame = cap.read()
while(True):
    # Capture frame-by-frame
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    captureImage()
    time.sleep(delay)

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

作为参考,cap.set()命令中的第一个参数指的是摄像头属性的枚举,如下所示:

0. CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds.
1. CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
2. CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file
3. CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
4. CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
5. CV_CAP_PROP_FPS Frame rate.

有什么办法可以补救吗?

xytpbqjk

xytpbqjk1#

您没有使用视频文件。我不清楚您设置了哪些选项,但可能是VideoCapture对象上的set调用有问题。请记住,您使用的是摄像机,因此如果我没记错的话,没有索引可跳过。
编辑:Jack,我认为你应该能够通过cv2.cv.CV_CAP_PROP_FPS等来访问正确的常量。假设你的相机驱动程序实现了这些ioctl,你会收到每秒3帧的图像。但是,当你覆盖其他两个图像时,imwrite()调用每秒只会写入一张图像。
如果可能的话,你应该测试你收到的Mat,而不是直接用highgui imshow写下来,测试时不要设置任何东西,然后报告。

v1uwarro

v1uwarro2#

这种情况发生在开始流后的前几帧。我建议丢弃前几帧,然后开始阅读帧。

for discard_frame in range(20):
    ret, frame = cap.read()
while(True):
    # Capture frame-by-frame
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    captureImage()
    time.sleep(delay)
2sbarzqh

2sbarzqh3#

USB兼容性应为1.1。如果您使用的是VMware

相关问题