Py学习  »  问与答

python 调用dll怎么返回多个值

小鱼叉 • 6 年前 • 1004 次点击  

dll中的函数是:RunHessianMultiThread(imgData, Sigma,ImageSize,HessianDot,HessianLine,4) c头文件的声明是: EXTERN_C bool RunHessianMultiThread (const short imgData, const float Sigma, int ImageLength, float HessianDot, float HessianLine = NULL, const int NumOfThreads = 1 ); 怎么可以让这个函数返回多个值呢?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/3147
 
1004 次点击  
文章 [ 1 ]  |  最新文章 6 年前
小鱼叉
Reply   •   1 楼
小鱼叉    6 年前

调用函数代码如下--->

import scipy.io as scio import os import ctypes import datetime import numpy as np

读取dll文件

这里的地址目录是HessianFilter.dll所在的文件夹

start = datetime.datetime.now() cur_path = os.path.dirname(r'C:\Users\Administrator\Desktop\code_and_data\code\HessianFilter\') dll_path = os.path.join(cur_path,'HessianFilter.dll') print dll_path dll = ctypes.windll.LoadLibrary(dll_path)

读取mat文件,Mat文件所在的文件夹中读取

matPath = r'C:\Users\Administrator\Desktop\code_and_data\data\Nodule19664.mat' imgData = scio.loadmat(matPath)

从mat文件中我们取到了整个三维数组

imgDataArray = imgData['imagetest1']

得到数组文件的参数:宽,高,层数

widthSrc,heightSrc,sliceNumSrc = imgDataArray.shape

声明一个三维的c_float类型的数组,用于存放mat数据,并将数据转化为c_float

imgDataArray_p = (((ctypes.c_floatsliceNumSrc)heightSrc)*widthSrc)()

for i in range(widthSrc): for j in range(heightSrc): for k in range(sliceNumSrc): imgDataArray_p[i][j][k] = ctypes.c_float(imgDataArray[i][j][k])

imgDataP = ctypes.POINTER(ctypes.c_float)(imgDataArray_p) print '---------->'

需要再声明两个返回值

HessianDot = (ctypes.c_float(widthSrcheightSrcsliceNumSrc))() HessianLine = (ctypes.c_float(widthSrcheightSrcsliceNumSrc))()

HessianDot_p = ctypes.POINTER(ctypes.c_float)(HessianDot) HessianLine_p = ctypes.POINTER(ctypes.c_float)(HessianLine)

定义一个常数

sigma = ctypes.c_float(8)

定义一个三维数组

imgSize = [widthSrc,heightSrc,sliceNumSrc] imageSize = (ctypes.c_floatlen(imgSize))(imgSize)

定义一个指向三维数组的指针

imageSizeP = ctypes.POINTER(ctypes.c_float)(imageSize)

这个就是调用dll中的函数了

dll.RunHessianMultiThread(ctypes.byref(imgDataArray_p),sigma,ctypes.byref(imageSizeP),ctypes.byref(HessianDot_p),ctypes.byref(HessianLine_p),4) print '--调用后-点数据--' print HessianDot_p print HessianDot_p[0:24]

print HessianDot_p.contents

print '--调用后-线数据--' print HessianLine_p print HessianLine_p[0:24]

print HessianLine_p.contents

print '<---------Over------------->\n\n' print datetime.datetime.now()-start