SoFunction
Updated on 2025-03-02

Regarding the issue of parameter passing when python calls C++ dynamic library dll

string

C++ generates dll code:

#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 ();
}

Place VS_create_dll.dll in the same folder as python.
python call code

import ctypes as C
dll = ('VS_create_dll.dll')
#4.1 Passing in a string to call demo method onep_str = C.c_char_p(b'hello')# or p_str = b'hello'str_length1 = dll.get_str_length(p_str)
print("Passing in a string to call demo method one:")
print (str_length1)
#4.1 Passing in a string to call demo method twoget_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("Passing in a string to call demo method two:")
print (str_length2)

cv::Mat

In python, opencv stores an image with an image with an array, while in C++, opencv stores an image with an image with an image with an image with an image with an opencv is Mat. The conversion between the two needs to be accomplished through unsigned char *.

Data type correspondence

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

Methods of converting array into (C.c_ubyte) in python (corresponding to unsigned char * in C++)

import ctypes as C
import cv2
img = ('')
#Convert img to data type that can be passed into dll.data_as((C.c_ubyte))

Methods for converting unsigned char* to Mat in C++

Assume that the variable passed in is unsigned char *src_data

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

Opencv in C++ provides an API of Mat type constructed through unsigned char*. This API also requires information such as row number, column number, channel number and other information.
Therefore, when python calls dll, it not only needs to pass src_data in, but also needs to pass rows, cols and other information in.

Methods for converting Mat to unsigned char* in C++

Opencv in C++ provides an API for converting Mat to unsigned char*, i.e.

Method for copying unsigned char* in C++

memcp(ret_data,,rows*cols*3);

Methods for converting (C.c_ubyte) (corresponding to unsigned char * in C++) into array in python

#Declare and initialize variablesimport numpy as np
import cv2
ret_img = (dtype=np.uint8, shape=(rows, cols, 3))
#call dll,ret_img.ctypes.data_as((C.c_ubyte)) is passed in as parameters("result",ret_img )

Since ret_img itself is of array type in python, it is converted as a formal parameter to (C.c_ubyte) when calling dll, ret_img does not need to be converted.

C++ generates dll code:

#include ""
#include &lt;iostream&gt;
#include &lt;opencv2/&gt;
#include &lt;opencv2/highgui/&gt;
#include &lt;opencv2/imgproc/&gt;
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)
{
	//Convert unsigned char to Mat	Mat src = Mat(rows, cols, CV_8UC3, src_data);
	//Draw a blue circle on the image	circle(src, Point(60, 60), 10, Scalar(255, 0, 0));
	//Convert Mat to unsigned char	memcpy(ret_data, , rows*cols * 3);
}

python

import ctypes as C
import cv2
import numpy as np
dll = ("draw_circle.dll")
img = ('')
(rows, cols) = ([0], [1])
ret_img = (dtype=np.uint8, shape=(rows, cols, 3))
dll.draw_circle(rows, cols, .data_as((C.c_ubyte)), ret_img.ctypes.data_as((C.c_ubyte)))
("src with circle",ret_img)
(0)

refer to

/wolfcsharp/article/details/103754514

This is the article about the parameter passing of python when calling c++ dynamic library (dll). For more related contents of python calling c++ dynamic library dll, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!