python调用C++动态链接库(Dll)(包含char *、int、float)

python调用C++动态链接库(Dll)

python调用dll总结:
1.需要装Visual Studio 2017环境,不装会报“找不到模块”
2.调用存在依赖的dll,需要把所依赖dll文件存放python环境的安装目录中(和python3.exe一个目录)
3.定义入参和出参类型,不定义会报错

C++动态链接库函数接口
float SealDet_surf_ncc(char * wide, char * tele, int Cha_point_threshold, float ncc_threshold);
wide:路径  tele:路径 Cha_point_threshold=20     ncc_threshold=0.1,返回为匹配系数
from ctypes import *
import os
os.environ['path'] += ';D:\Program Files (x86)\python3\opencv_dll' #添加dll依赖库目录到系统环境或把所依赖dll文件存放python环境的安装目录中
def testdll(wide,tele):
    dll = CDLL(os.getcwd()+"/"+"SealDet_surf_ncc_V22_vs2017_akaze_deng_gener_dll.dll")#初始化dll,加载dll
    dll.SealDet_surf_ncc.argtypes = [POINTER(c_char), POINTER(c_char), c_int, c_float] #定义dll入参类型
    dll.SealDet_surf_ncc.restype = c_float   #定义dll出参类型,不定义程序不知道类型会报错
    wide1 = (c_char * 100)(*bytes(wide, 'utf-8'))  # 把一组100个的字符定义为STR
    tele1 = (c_char * 100)(*bytes(tele, 'utf-8'))  # 把一组100个的字符定义为STR
    #cast(wide1, POINTER(c_char))
    #cast(tele1, POINTER(c_char))
    pchar = dll.SealDet_surf_ncc(wide1, tele1, 20, 0.1)
    return (pchar)



wide = r"F:\python\xxxdll\1号\1\TC_01_01_001.jpg"
tele = r"F:\python\xxxdll\1号\1\TC_01_01_001.jpg"
print(testdll(wide,tele))
"D:\Program Files (x86)\python3\python.exe" F:/python/xxdll/xxxdll_/xxxdll_/xxxxdll.py
0.998168408870697
[ INFO:0] VIDEOIO: Enabled backends(5, sorted by priority): FFMPEG(1000); MSMF(990); DSHOW(980); CV_IMAGES(970); CV_MJPEG(960)
发布了58 篇原创文章 · 获赞 18 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42846555/article/details/103335867
今日推荐