正在将numpy同形ndarray保存到文件中



我正在尝试使用将numpy ndarray同形表保存到文件中(如果需要,稍后使用(

h.tofile("h.h", sep=",", format="%s")

但当我稍后加载它并尝试在计算中使用它时,使用:

h = np.fromfile('h.h')

我得到以下错误:

OpenCV Error: Assertion failed (scn + 1 == m.cols) in perspectiveTransform, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/src/matmul.cpp, line 2268
Traceback (most recent call last):
File "...camera_to_point.py", line 166, in draw_circle2
pointOut = cv2.perspectiveTransform(a, h)
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/core/src/matmul.cpp:2268: error: (-215) scn + 1 == m.cols in function perspectiveTransform

有趣的是,当我在保存到文件之前和从文件中检索它之后检查同形矩阵时,我得到:

保存前:

h
array([[ 1.86326937e-02, -1.16700086e-02,  2.66963340e+00],
[-7.51402803e-03, -3.88364336e-02,  1.69502899e+01],
[-1.05249671e-03,  1.47791921e-02,  1.00000000e+00]])
[0:3] : [array([ 0.01863269, ...6696334 ]), array([-7.51402803e-...2899e+01]), array([-0.0010525 , ...        ])]
dtype: dtype('float64')
max: 16.950289865517334
min: -0.038836433627212195
shape: (3, 3)
size: 9
__internals__: {'T': array([[ 1.86326937e...000e+00]]), 'base': None, 'ctypes': <numpy.core._interna...ff8179b90>, 'data': <read-write buffer f...ff81797f0>, 'dtype': dtype('float64'), 'flags':   C_CONTIGUOUS : Tru...PY : False, 'flat': <numpy.flatiter obje...0x2f69850>, 'imag': array([[0., 0., 0.],... 0., 0.]]), 'itemsize': 8, 'nbytes': 72, 'ndim': 2, 'real': array([[ 1.86326937e...000e+00]]), 'shape': (3, 3), 'size': 9, ...}

检索后:

h
array([7.12605079e-67, 1.18069470e-95, 9.95130728e-43, 9.95309333e-43,
5.40222957e-62, 4.32553942e-91, 2.74137239e-57, 3.06246782e-57,
7.11172728e-38, 8.16641713e-43, 1.83288526e-76, 9.92883300e-96,
1.69053846e-52, 9.34287548e-67, 9.05446937e-43, 7.11877246e-67])
[0:16] : [7.126050789796848e-67, 1.1806946993563433e-95, 9.951307279141174e-43, 9.95309332763986e-43, 5.402229572720159e-62, 4.325539416797926e-91, 2.741372385066056e-57, 3.0624678193742925e-57, 7.111727282221548e-38, 8.16641712557458e-43, 1.8328852622133153e-76, 9.928833002794128e-96, 1.690538456980108e-52, 9.342875479816443e-67, ...]
dtype: dtype('float64')
max: 7.111727282221548e-38
min: 9.928833002794128e-96
shape: (16,)
size: 16
__internals__: {'T': array([7.12605079e-6...7246e-67]), 'base': None, 'ctypes': <numpy.core._interna...ff8179790>, 'data': <read-write buffer f...ff8179470>, 'dtype': dtype('float64'), 'flags':   C_CONTIGUOUS : Tru...PY : False, 'flat': <numpy.flatiter obje...0x2f68e00>, 'imag': array([0., 0., 0., 0..., 0., 0.]), 'itemsize': 8, 'nbytes': 128, 'ndim': 1, 'real': array([7.12605079e-6...7246e-67]), 'shape': (16,), 'size': 16, ...}

它显示了两个不同的矩阵。

那么,如何将numpy ndmatrix保存到文件中并成功检索它呢?

我正在使用opencv2和python2.7

使用h.tofile("h.h", sep=",", format="%s"),会存储一个包含所有数字(用逗号分隔(的文本文件,但数组维度的信息会丢失
h = np.fromfile('h.h')结果会出现在输出文件中。如果您使用
h = np.fromfile('h.h', sep=","),然后像
h.resize((3, 3))那样调整h的大小你会得到想要的结果。

如果使用二进制文件格式,则可以检索数组信息。

np.array可以使用pickle库以二进制文件格式存储:
脚本在python 2.7和3.8 中运行

from pickle import dump, load
from numpy import array
def save_h():
h = array([[1.86326937e-02, -1.16700086e-02,  2.66963340e+00],
[-7.51402803e-03, -3.88364336e-02,  1.69502899e+01],
[-1.05249671e-03,  1.47791921e-02,  1.00000000e+00]])
with open("save.h", "wb") as f:
dump(h, f)
with open("save.h", "rb") as f:
h1 = load(f)
return h, h1
if __name__ == "__main__":
stored, loaded = save_h()
print(stored)
print(loaded)

输出:

[[ 1.86326937e-02 -1.16700086e-02  2.66963340e+00]
[-7.51402803e-03 -3.88364336e-02  1.69502899e+01]
[-1.05249671e-03  1.47791921e-02  1.00000000e+00]]
[[ 1.86326937e-02 -1.16700086e-02  2.66963340e+00]
[-7.51402803e-03 -3.88364336e-02  1.69502899e+01]
[-1.05249671e-03  1.47791921e-02  1.00000000e+00]]

最新更新