【发布时间】:2017-08-26 04:02:47
【问题描述】:
我正在尝试使用自写的 c 库从 python 处理二维数组,但收效甚微。
这是我的 c 代码:
CamLibC.c
int TableCam(const int x, const int y, int **Array) {
int i = 0;
int j = 0;
for (i; i < x; i++) {
for (j; j < y; j++) {
Array[i][j] = 1;
};
};
}
cc -nostartfiles -shared -fPIC -o CamLibOS.os CamLibC.c
现在这是我的 python 包装器:
CamLibPy.py
import os,sys
import ctypes
dirname = os.path.dirname(os.path.realpath(sys.argv[0]))
CamLibFile = dirname + '/CamLibOS.os'
_CamLib = ctypes.CDLL(CamLibFile)
_CamLib.TableCam.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
def TableCam (A) :
global _CamLib
x = len(A)
y = len(A[0])
print('x: ', x, ' y: ', y);
arrayType = ((ctypes.c_int * x) * y)
array = arrayType()
_CamLib.TableCam(ctypes.c_int(x), ctypes.c_int(y), array)
print(array)
以及我使用该函数的python代码:
Test.py
import CamLibPy
from numpy import zeros
Anum = zeros((3,3))
print('Start: ', Anum)
CamLibPy.TableCam(Anum)
print('Ended: ', Anum)
在这个测试程序中,我尝试将数组中的所有零更改为一。但是一旦我尝试运行,就会得到以下输出:
开始:[[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]]
x: 3 y: 3
Traceback(最近一次调用最后一次):文件“/media/pi/USB DISK/Test/Test.py”,第 7 行,在 CamLibPy.TableCam(Anum) 文件“/media/pi/USB DISK/Test/CamLibPy.py”,第 21 行,在 TableCam _CamLib.TableCam(ctypes.c_int(x),ctypes.c_int(y),数组)ctypes.ArgumentError:参数3::预期 LP_c_long 实例而不是 c_long_Array_3_Array_3
它是说它需要一个 c_long 但我显然使用 c_int 来制作 arrayType
谁能告诉我我做错了什么?
【问题讨论】:
标签: python c arrays testing ctypes