温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-04-06 15:07:16 来源:亿速云 阅读:324 作者:iii 栏目:开发技术

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

string

C++生成dll代码:

#include <iostream>
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 C
dll = 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_length
get_str_length.argtypes = [C.c_char_p]
get_str_length.restype = C.c_int
str_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 C
import cv2
img = 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 np
import cv2
ret_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 <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
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 C
import cv2
import numpy as np
dll = 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时的参数传递问题怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI