将C ++中的3D数组转换为numpy数组

时间:2018-05-19 16:29:19

标签: python c++ numpy

import numpy as np
from ctypes import *

cpp = CDLL('./Dll1.dll')
cMakeVolumeFunc = cpp.makeVolumeArray
cMakeVolumeFunc.restype = POINTER(POINTER(POINTER(c_ushort * nz)*ny)*nx)
vol_ptr= cMakeVolumeFunc()

print(cpp.getElement(10, 10, 10))
print(cpp.getElement(243, 234, 100))
print(cpp.getElement(355, 234, 100))

arr = (c_ushort * nz * ny * nx).from_address(addressof(vol_ptr.contents))
img = np.ndarray(buffer=arr, dtype=np.ushort, shape=(nz, ny, nx))

print(img.shape)
print(img[10][10][10])
print(img[243][234][100])
print(img[355][234][100])

我的C ++函数(makeVolumeArray)的返回类型是unsigned short ***。上面的Python代码通常可以运行,但每个numpy数组元素(img)的值是随机的。有谁能告诉我如何解决这个问题?

0 个答案:

没有答案