简体   繁体   English

OpenCV 网络摄像头图像损坏

[英]OpenCV webcam image broken

I wrote a script in Python3 and OpenCV 3.1 for timelapse photos on Odroid C1:我在 Python3 和 OpenCV 3.1 中为 Odroid C1 上的延时照片编写了一个脚本:

Unfortunately sometimes images from the webcam are messed up: Here's are some example photographs.不幸的是,有时来自网络摄像头的图像会混乱:这是一些示例照片。

在此处输入图像描述 在此处输入图像描述

And here's the source code:这是源代码:

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()

For reference, the first argument in the cap.set() command refers to the enumeration of the camera properties, listed below:作为参考, 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.

Is there any way to fix it?有什么办法可以解决吗?

You are not working with a video file.您没有使用视频文件。 It is not clear to me what options you are setting but it could be a problem with your set calls on your VideoCapture object.我不清楚您正在设置哪些选项,但这可能是您对VideoCapture对象的set调用存在问题。 Keep in mind that you are working with a camera so there is no index to skip if I remember correctly.请记住,您正在使用相机,所以如果我没记错的话,没有索引可以跳过。

EDIT: Jack, I think you should be able to access the correct constants by using cv2.cv.CV_CAP_PROP_FPS etc. Assuming your camera driver implemented these ioctls you would receive 3 frames per seconds.编辑:杰克,我认为您应该能够通过使用cv2.cv.CV_CAP_PROP_FPS等访问正确的常量。假设您的相机驱动程序实现了这些 ioctl,您将每秒收到 3 帧。 However only one picture per second would be written by the imwrite() call as you are overwriting the other two.但是,当您覆盖其他两张时, imwrite()调用每秒只会写入一张图片。

You should test your received Mat without writing them down directly with the highgui imshow if possible.如果可能的话,你应该测试你收到的Mat而不是直接用 highgui imshow写下来。 Test that without setting anything and report back.在不设置任何内容的情况下进行测试并报告。

It happens for first few frames, after starting a stream.它发生在开始流之后的前几帧。 I will suggest discard first few frames, and then start reading the frames.我会建议丢弃前几帧,然后开始阅读这些帧。

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)

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

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