开发者社区> 问答> 正文

python调用dll怎么返回多个值

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:UsersAdministratorDesktopcode_and_datacodeHessianFilter\')
dll_path = os.path.join(cur_path,'HessianFilter.dll')
print dll_path
dll = ctypes.windll.LoadLibrary(dll_path)

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

matPath = r'C:UsersAdministratorDesktopcode_and_datadataNodule19664.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(widthSrcheightSrc*sliceNumSrc))()
HessianLine = (ctypes.c_float(widthSrcheightSrc*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_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------------->nn'
print datetime.datetime.now()-start
a

展开
收起
小云葩 2017-08-23 14:01:24 5030 0
1 条回答
写回答
取消 提交回答
  • 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:UsersAdministratorDesktopcode_and_datacodeHessianFilter')
    dll_path = os.path.join(cur_path,'HessianFilter.dll')
    print dll_path
    dll = ctypes.windll.LoadLibrary(dll_path)

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

    matPath = r'C:UsersAdministratorDesktopcode_and_datadataNodule19664.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_pi[k] = ctypes.c_float(imgDataArrayi[k])

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

    需要再声明两个返回值

    HessianDot = (ctypes.c_float(widthSrcheightSrc*sliceNumSrc))()
    HessianLine = (ctypes.c_float(widthSrcheightSrc*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_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------------->nn'
    print datetime.datetime.now()-start

    2019-07-17 21:31:16
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载