2016-10-09 51 views
1

我有一个USB连接的摄像头,并希望将捕获的帧保存到文本文件。帧是numpy数组,我只需要获得红色的颜色值。所以,这里是我的代码:OpenCV:将帧矩阵保存到文本文件(Python)

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 func不工作,因为我想。这里是test.txt的开头: 1.270000000000000000e + 02 1.250000000000000000e + 02 1.250000000000000000e + 02

等等。

我使用OpenCv 3.1和Python 2.7.12

任何帮助吗?

回答

1

savetxt默认格式是'%.18e'它解释了你得到的格式。

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

更改格式说明与fmt参数打印整数,而不是花车:

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

它工作得很好,非常感谢! – askrav