千家信息网

python调用c++动态库dll时的参数传递问题怎么解决

发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,本篇内容介绍了"python调用c++动态库dll时的参数传递问题怎么解决"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细
千家信息网最后更新 2025年11月08日python调用c++动态库dll时的参数传递问题怎么解决

本篇内容介绍了"python调用c++动态库dll时的参数传递问题怎么解决"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

string

C++生成dll代码:

#include extern "C" __declspec(dllexport) int get_str_length(char *str);int get_str_length(char *in_str){        std::string str(in_str);        return str.length();}

将VS_create_dll.dll放在与python相同文件夹下。
python调用代码

import ctypes as Cdll = C.cdll.LoadLibrary('VS_create_dll.dll')#4.1 传入字符串调用demo 方法一p_str = C.c_char_p(b'hello')#或p_str =  b'hello'str_length2 = dll.get_str_length(p_str)print("传入字符串调用demo 方法一:")print (str_length2)#4.1 传入字符串调用demo 方法二get_str_length = dll.get_str_lengthget_str_length.argtypes = [C.c_char_p]get_str_length.restype = C.c_intstr_length3 = get_str_length(p_str)print("传入字符串调用demo 方法二:")print (str_length3)

cv::Mat

python中opencv存储一幅图像的数据类型是array,而在C++中opencv存储一幅图像的数据类型是Mat,这两者之间的转换需要通过unsigned char * 来完成。

数据类型对应关系

python:      C.POINTER(C.c_ubyte)C++:            unsigned char *

python中将array转换成C.POINTER(C.c_ubyte)(对应C++中的unsigned char *)的方法

import ctypes as Cimport cv2img = cv2.imread('ROI0.png')#将img转换成可被传入dll的数据类型img.ctypes.data_as(C.POINTER(C.c_ubyte))

C++中将unsigned char* 转换成Mat的方法

假设传入的变量为unsigned char *src_data

Mat src = Mat(rows,cols,CV_8UC3,src_data);

C++中opencv提供了通过unsigned char*构造Mat类型的API,这个API还需要行数、列数、通道数等信息。
因此python调用dll时,不仅要将src_data传入,还需要将rows,cols等信息传入。

C++中将Mat转换成unsigned char *的方法

src.data

C++中opencv提供了将Mat转换成unsigned char *的API,即Mat.data

C++中将unsigned char*复制的方法

memcp(ret_data,src.data,rows*cols*3);

python中将C.POINTER(C.c_ubyte)(对应C++中的unsigned char *)转换成array的方法

#声明并初始化变量import numpy as npimport cv2ret_img = np.zeros(dtype=np.uint8, shape=(rows, cols, 3))#call dll,ret_img.ctypes.data_as(C.POINTER(C.c_ubyte))作为参数传入cv2.imshow("result",ret_img )

由于在python中ret_img本身就是array类型的,只是在调用dll时将其作为形参转换成了C.POINTER(C.c_ubyte),因此ret_img不需要转换。

C++生成dll代码:

#include "stdafx.h"#include #include #include #include using namespace cv;using namespace std;extern "C" __declspec(dllexport) void draw_circle(int rows, int cols, unsigned char *src_data, unsigned char *ret_data);void draw_circle(int rows, int cols, unsigned char *src_data , unsigned char *ret_data){        //将unsigned char转换成Mat        Mat src = Mat(rows, cols, CV_8UC3, src_data);        //在图像上画一个蓝色的圆        circle(src, Point(60, 60), 10, Scalar(255, 0, 0));        //将Mat转换成unsigned char        memcpy(ret_data, src.data, rows*cols * 3);}

python

import ctypes as Cimport cv2import numpy as npdll = C.cdll.LoadLibrary("draw_circle.dll")img = cv2.imread('ROI0.png')(rows, cols) = (img.shape[0], img.shape[1])ret_img = np.zeros(dtype=np.uint8, shape=(rows, cols, 3))dll.draw_circle(rows, cols, img.ctypes.data_as(C.POINTER(C.c_ubyte)), ret_img.ctypes.data_as(C.POINTER(C.c_ubyte)))cv2.imshow("src with circle",ret_img)cv2.waitKey(0)

"python调用c++动态库dll时的参数传递问题怎么解决"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!

0