鱼C论坛

 找回密码
 立即注册
查看: 2768|回复: 2

python 调用dll 怎么返回多个值

[复制链接]
发表于 2017-8-23 12:05:17 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
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_float*sliceNumSrc)*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*(widthSrc*heightSrc*sliceNumSrc))()
HessianLine = (ctypes.c_float*(widthSrc*heightSrc*sliceNumSrc))()

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_float*len(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                           

被调用dll中函数的声明

被调用dll中函数的声明
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2017-8-23 16:01:14 | 显示全部楼层
貌似可以用指针,但是那个dll里面是什么参数需要清楚
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-8-23 16:07:34 | 显示全部楼层
郭华 发表于 2017-8-23 16:01
貌似可以用指针,但是那个dll里面是什么参数需要清楚

dll中有个六参数分别是:const short* imgData, const float Sigma, int* ImageLength,
        float* HessianDot, float* HessianLine = NULL, const int NumOfThreads = 1 。然后我在用python调用函数的时候分别传入了六个参数:ctypes.byref(imgDataArray_p),sigma,ctypes.byref(imageSizeP),ctypes.byref(HessianDot),ctypes.byref(HessianLine),4。分别表示三维数据的指针,常数,一维数组的指针,一维数组的指针,一维数组的指针,常数。程序可以正常运行,就是不知道为什么进行了调用后HessianDot和HessianLine这两个参数为什么没有任何变化
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-23 09:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表