我用swig包装一个c++库,它需要以char*的形式获取图像数据。我可以用python阅读图像。但是如何将其转换为c++?在
我知道我可能需要使用typemap。我试了好几种方法,但我总是得到一张只有条纹的照片。在
这是我的接口文件:
/* np2char */
%module np2char
%{
#define SWIG_FILE_WITH_INIT
#include <opencv2/opencv.hpp>
using namespace cv;
%}
%inline %{
typedef char* Image_Data_Type;
typedef int Image_Width_Type;
typedef int Image_Height_Type;
struct Image_Info {
Image_Data_Type imageData;
Image_Width_Type imageWidth;
Image_Height_Type imageHeight;
};
int Imageshow(Image_Info ImageInfo) {
Mat img(ImageInfo.imageHeight, ImageInfo.imageWidth, CV_8UC3, ImageInfo.imageData);
imshow("img_in_cpp", img);
waitKey(0);
destroyAllWindows();
return 0;
}
%}
这是我的设置.py公司名称:
^{pr2}$这是我的python文件:
import np2char
import cv2
img1 = cv2.imread("1.jpg")
img_info = np2char.Image_Info()
img_info.imageData = img1.data
img_info.imageWidth = img1.shape[1]
img_info.imageHeight = img1.shape[0]
np2char.Imageshow(img_info)
我试过了
%typemap(in) Image_Data_Type{
$1 = reinterpret_cast<char*>(PyLong_AsLongLong($input));
}
,在python端
img_info.imageData=img1.ctypes.data
但我还是只有条纹。似乎imagedata被复制到内存中的其他位置。在此过程中,它被“\0”截断。在
哈哈,我自己想出来的。
在SWIG Documentation 5.5.2中
所以,我需要的是“typemap(memberin)”:
使用整数来传输指针有点难看。有更好的方法吗?在
相关问题 更多 >
编程相关推荐