我正在尝试使用自己编写的 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 代码:
测试.py
import CamLibPy
from numpy import zeros
Anum = zeros((3,3))
print('Start: ', Anum)
CamLibPy.TableCam(Anum)
print('Ended: ', Anum)
在此测试程序中,我尝试将数组中的所有零更改为一。但是一旦我尝试运行它就会得到以下输出:
Start: [[ 0. 0. 0.] [ 0. 0. 0.] [ 0. 0. 0.]]
x: 3 y: 3
Traceback (most recent call last): File "/media/pi/USB DISK/Test/Test.py", line 7, in CamLibPy.TableCam(Anum) File "/media/pi/USB DISK/Test/CamLibPy.py", line 21, in TableCam _CamLib.TableCam(ctypes.c_int(x), ctypes.c_int(y), array) ctypes.ArgumentError: argument 3: : expected LP_c_long instance instead of c_long_Array_3_Array_3
它说它需要一个 c_long 但我显然使用 c_int 来制作 arrayType
谁能告诉我我做错了什么?
最佳答案
c_long
与c_int
相同,但这不是问题。
有很多问题:
numpy.zeros
的类型默认为 float。- 在 Python
TableCam
中,A
永远不会被修改。 - 在C
TableCam
中,第3个参数应该是int* Array
和计算修改的元素i*y+j
。这也符合argtypes
,这是正确的。 - numpy 数组可以强制转换为正确的
ctypes
类型。
更正后的代码(在 Windows 上,用于我的测试):
cam.c
#include <stdio.h>
__declspec(dllexport) void TableCam(const int x, const int y, int *Array)
{
int i,j;
for (i = 0; i < x; i++)
for (j = 0; j < y; j++)
Array[i*y+j] = 1;
}
camlib.py
import ctypes
_CamLib = ctypes.CDLL('cam')
_CamLib.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.POINTER(ctypes.c_int)]
_CamLib.restype = None
def TableCam(A):
x,y = A.shape
array = A.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
_CamLib.TableCam(x,y,array)
测试.py
import camlib
import numpy as np
Anum = np.zeros((3,3),dtype=np.int)
print('Start:')
print(Anum)
camlib.TableCam(Anum)
print('Ended:')
print(Anum)
输出
Start: [[0 0 0] [0 0 0] [0 0 0]] Ended: [[1 1 1] [1 1 1] [1 1 1]]
关于Python 二维数组 i C 使用 ctypes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43148188/