本篇內容介紹了“python調用c++動態庫dll時的參數傳遞問題怎么解決”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
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)
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時的參數傳遞問題怎么解決”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。