欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(guān)于python調(diào)用c++動態(tài)庫dll時的參數(shù)傳遞問題

 更新時間:2022年04月06日 13:59:46   作者:點(diǎn)PY  
這篇文章主要介紹了python調(diào)用c++動態(tài)庫dll時的參數(shù)傳遞,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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調(diào)用代碼

import ctypes as C
dll = C.cdll.LoadLibrary('VS_create_dll.dll')
#4.1 傳入字符串調(diào)用demo 方法一
p_str = C.c_char_p(b'hello')#或p_str =  b'hello'
str_length1 = dll.get_str_length(p_str)
print("傳入字符串調(diào)用demo 方法一:")
print (str_length1)
#4.1 傳入字符串調(diào)用demo 方法二
get_str_length = dll.get_str_length
get_str_length.argtypes = [C.c_char_p]
get_str_length.restype = C.c_int
str_length2 = get_str_length(p_str)
print("傳入字符串調(diào)用demo 方法二:")
print (str_length2)

cv::Mat

python中opencv存儲一幅圖像的數(shù)據(jù)類型是array,而在C++中opencv存儲一幅圖像的數(shù)據(jù)類型是Mat,這兩者之間的轉(zhuǎn)換需要通過unsigned char * 來完成。

數(shù)據(jù)類型對應(yīng)關(guān)系

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

python中將array轉(zhuǎn)換成C.POINTER(C.c_ubyte)(對應(yīng)C++中的unsigned char *)的方法

import ctypes as C
import cv2
img = cv2.imread('ROI0.png')
#將img轉(zhuǎn)換成可被傳入dll的數(shù)據(jù)類型
img.ctypes.data_as(C.POINTER(C.c_ubyte))

C++中將unsigned char* 轉(zhuǎn)換成Mat的方法

假設(shè)傳入的變量為unsigned char *src_data

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

C++中opencv提供了通過unsigned char*構(gòu)造Mat類型的API,這個API還需要行數(shù)、列數(shù)、通道數(shù)等信息。
因此python調(diào)用dll時,不僅要將src_data傳入,還需要將rows,cols等信息傳入。

C++中將Mat轉(zhuǎn)換成unsigned char *的方法

src.data

C++中opencv提供了將Mat轉(zhuǎn)換成unsigned char *的API,即Mat.data

C++中將unsigned char*復(fù)制的方法

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

python中將C.POINTER(C.c_ubyte)(對應(yīng)C++中的unsigned char *)轉(zhuǎn)換成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))作為參數(shù)傳入
cv2.imshow("result",ret_img )

由于在python中ret_img本身就是array類型的,只是在調(diào)用dll時將其作為形參轉(zhuǎn)換成了C.POINTER(C.c_ubyte),因此ret_img不需要轉(zhuǎn)換。

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轉(zhuǎn)換成Mat
	Mat src = Mat(rows, cols, CV_8UC3, src_data);
	//在圖像上畫一個藍(lán)色的圓
	circle(src, Point(60, 60), 10, Scalar(255, 0, 0));
	//將Mat轉(zhuǎn)換成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)

參考

https://blog.csdn.net/wolfcsharp/article/details/103754514

到此這篇關(guān)于python調(diào)用c++動態(tài)庫(dll)時的參數(shù)傳遞的文章就介紹到這了,更多相關(guān)python調(diào)用c++動態(tài)庫dll內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論