【问题标题】:What datatype is captured with cv2.VideoCapture.read() method in OpenCV?在 OpenCV 中使用 cv2.VideoCapture.read() 方法捕获什么数据类型?
【发布时间】:2019-02-24 11:10:02
【问题描述】:

我想知道使用 cv2.VideoCapture.read() 方法正在捕获哪种数据类型。我一直在阅读 OpenCV 文档,发现了这样的解释:

我已经学习了一些关于 OpenCV 的基本教程,其中网络摄像头可以捕获数据并输出帧。下图中显示的是帧数据。

下面是输出这些帧的代码:

import cv2, time, base64

framesCaptured = 0;
video=cv2.VideoCapture(0) <====== Video capture for the webcam

# Save camera frames as movie
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('recording.avi', fourcc, 20.0, (640, 480))

while True:
    framesCaptured += 1

    check, frame = video.read() <====== Grabs and retrieves frames and decode
    # Write video frames
    out.write(frame)

    # Print out statements
    #print(check)
    print(frame) <====== Print out frame data (as shown in the cmd picture below)

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

    cv2.imshow("frame", gray)   

    key=cv2.waitKey(1)

    if key == ord('q'):
        break

#print('Frames captured: ' + str(framesCaptured))
video.release()
out.release()
cv2.destroyAllWindows

所以我想知道从“框架”变量中打印出什么样的数据类型?

最后我想使用这个“帧”数据在 C# 应用程序中将图像重新构造为视频。

【问题讨论】:

  • 在 Python 中,OpenCV 的图像存储为 numpy 数组。在 OpenCV for C++ 中,它们被存储为自己的自定义类型,Mat
  • IPython 中开发代码非常快速和容易,您只需键入每一行代码,就像在编辑器或 IDE 中编写脚本一样,但是当你想知道一个变量是什么样子时,你只需输入它的名字并点击Enter,它的格式很适合你,或者如果你想知道它的类型,你只需输入type(frame)它会告诉你,然后你继续创建您的代码。当一切看起来正确时,您只需键入 %history 并复制工作、调试过的代码并将其粘贴到文件中以供下次保存。

标签: python opencv types cv2


【解决方案1】:

您可以通过添加print(type(frame))自行检查框架类型。
当我使用print(type(frame)) 执行脚本时,会打印numpy.ndarray

【讨论】:

  • 就这么简单。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-07-29
  • 1970-01-01
  • 2020-03-24
  • 2018-06-11
  • 2013-12-25
  • 1970-01-01
  • 2021-11-13
  • 2012-07-16
相关资源
最近更新 更多