动态连接库在Windows中为.dll文件,在linux中为.so文件。以linux平台为例说明python调用.so文件的使用方法。
本例中默认读者已经掌握动态连接库的生成方法,若是不太清楚的能够参考动态连接库的使用
调用上例动态连接库的使用中的sum.sohtml
import ctypes so = ctypes.CDLL('./sum.so') print "so.sum(50) = %d" % so.sum(50) so.display("hello world!") print "so.add() = %d" % so.add(ctypes.c_float(2), ctypes.c_float(2010))
outputpython
so.sum(50) = 1275 hello world! so.add() = 2012
注意:linux
import ctypes pyarray = [1, 2, 3, 4, 5] carrary = (ctypes.c_int * len(pyarray))(*pyarray) //有点相似malloc的方式生成carray print so.sum_array(carray, len(pyarray))
refergit
pyarray = [1,2,3,4,5,6,7,8] carray = (ctypes.c_int*len(pyarray))(*pyarray) so.modify_array(carray, len(pyarray)) print np.array(carray)
outputgithub
[10 20 30 40 50 60 70 80]
import ctypes import numpy as np from numpy.ctypeslib import ndpointer so = ctypes.CDLL('./sum.so') pyarray = np.array([1,2,3,4,5,6,7,8], dtype="int32") fun = so.modify_array fun.argtypes = [ndpointer(ctypes.c_int), ctypes.c_int] fun.restype = None fun(pyarray, len(pyarray)) print np.array(pyarray)
注意:numpy中的数据类型指定很重要,即dtype的设定数组
背景知识:
python中的opencv图片是用numpy的方式保存的,而opencv3 C语言的图片数据结构为cvMat (IplImage已经逐弃用)
因此须要把python中numpy图片转换为ctypes.POINTER(ctypes.c_ubyte)的指针转入其数据域,再将其行列信息传入,就能够在C中从最底层初始化一个CvMat,若是要初始化一个别数据结构的图片也是同理(如darknet的image,caffe的blob)
python numpy image 转换为 C pointer的方法数据结构
python_frm.ctypes.data_as(C.POINTER(ctypes.c_ubyte))
注意:传入numpy image前必定要确保numpy image是numpy array数据类型
好比我遇到的bug函数
image = cv2.imread("xxx.jpg");
image传入ctypes_so.fun之中图片是有效的,但3d
image = whl_img[y1:y2, x1:x2]
这时候进入ctypes_so.fun的图片会变成一个乱码
即,crop以后的numpy image的type虽然也为numpy array,但实际传入的image data却不正确
解决方法:
不管是何种方式获得的numpy image,都强行转换为numpy array,再传入ctypes_so.fun指针
image = numpy.array(image)
能够解决这个bug
refence
opencv3 python已经不支持cv2.cv的函数了
但Opencv2可能还能够尝试如下方法
numpy image to iplimage
由于python不能直接调用C++中的类,因此必须把C++中的类转换为C的接口
转换原则
#ifdef __cplusplus extern "C" { #endif xxxxxx function declaration xxxxx #ifdef __cplusplus } #endif