python - OpenCV:将帧矩阵保存到文本文件

标签 python numpy opencv

我有一个USB 连接的网络摄像头,并且希望将捕获的帧保存到文本文件中。

框架是一个NumPy数组,我只需要获取红色值。

所以,这是我的代码:

vc = cv2.VideoCapture(1)

if vc.isOpened():
    rval, frame = vc.read()
    frame = imutils.resize(frame, width = 640, height = 480)
    print(frame[..., ..., 2])
    savetxt('../test.txt', frame[..., ..., 2])

打印让我得到这个:

[[127 125 125 ..., 114 118 101]

 [123 126 125 ..., 111 112 100]

 [129 124 122 ..., 116 116 100]

 ..., 

 [121 120 121 ...,  97 104  88]

 [118 121 121 ...,  96 103  90]

 [116 122 120 ...,  97 105  90]]

但即使我可以打印整个数组,它也不适合终端窗口。

所以我想将其保存到文件中,但 savetxt() 函数无法按我想要的方式工作。

这是test.txt的开头:

1.270000000000000000e+02 1.250000000000000000e+02 1.250000000000000000e+02

等等。

我正在使用 OpenCV 3.1Python 2.7.12

有什么帮助吗?

最佳答案

savetxt 默认格式为 '%.18e',它解释了您所获得的格式。

numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ')[source]¶

使用 fmt 参数更改格式说明符以打印整数而不是 float :

savetxt('../test.txt', frame[...,...,2],fmt="%d")

关于python - OpenCV:将帧矩阵保存到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39947903/

相关文章:

python - 无法将列表从机器人框架传递到Python

python - 我正在尝试运行 Ipython 笔记本,除了它给我一个 "astype timedelta64"错误

python - Numpy 就地设置绝对值

python - 如何在Python中使用OpenCV在检测部分绘制矩形?

python - 属性错误: module 'urllib3' has no attribute 'urlopen' in python

python - GEKKO 中 MPC 的自由度警告

python - Python 中的马尔可夫转移概率矩阵实现

c++ - 对拍摄照片并将其保存到文件的 opencv c++ 程序进行单元测试

安卓 : How can I open a file using OpenCV C++ ?

python - 在 Django 教程中使用模板