In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to solve the parameter transfer problem when python calls C++ dynamic library dll". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
String
C++ generates dll code:
# 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 ();}
Put VS_create_dll.dll in the same folder as python.
Python calling code
Import ctypes as Cdll = C.cdll.LoadLibrary ('VS_create_dll.dll') # 4.1 incoming string calls demo method-p_str = C.c_char_p (bounded hello') # or p_str = b'hello'str_length2 = dll.get_str_length (p_str) print ("incoming string calls demo method 1:") print (str_length2) # 4.1 incoming string calls demo method II 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 ("incoming string calls demo method 2:") print (str_length3) cv::Mat
The data type for opencv to store an image in python is array, while the data type for opencv to store an image in C++ is Mat. The conversion between the two needs to be completed through unsigned char *.
Data type correspondence
Python: C.POINTER (C.c_ubyte) Clippers: unsigned char *
The method of converting array to C.POINTER (C.c_ubyte) in python (corresponding to unsigned char * in C++)
Import ctypes as Cimport cv2img = cv2.imread ('ROI0.png') # converts img to a data type img.ctypes.data_as (C.POINTER (C.c_ubyte)) that can be passed in to dll
The method of converting unsigned char* to Mat in C++
Suppose the passed variable is unsigned char * src_data
Mat src = Mat (rows,cols,CV_8UC3,src_data)
Opencv in C++ provides API for constructing Mat type through unsigned char*. This API also needs information such as the number of rows, columns, channels and so on.
Therefore, when python calls dll, you need to pass in not only src_data, but also information such as rows,cols.
The method of converting Mat to unsigned char * in C++
Src.data
Opencv in C++ provides API for converting Mat to unsigned char *, namely Mat.data.
The method of copying unsigned char* in C++
Memcp (ret_data,src.data,rows*cols*3)
The method of converting C.POINTER (C.c_ubyte) (corresponding to unsigned char * in C++) into array in python
# declare and initialize the variable 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)) as a parameter to pass cv2.imshow ("result", ret_img)
Because ret_img itself is of type array in python, it is only converted to C.POINTER (C.c_ubyte) as a formal parameter when calling dll, so ret_img does not need to be converted.
C++ generates dll code:
# include "stdafx.h" # 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) {/ / convert unsigned char to Mat Mat src = Mat (rows, cols, CV_8UC3, src_data) / draw a blue circle circle (src, Point (60,60), 10, Scalar (255,0,0)) on the image; / / convert Mat to 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) "how to solve the parameter passing problem when python calls C++ dynamic library dll" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.