繁体   English   中英

如何在Swig中为非原始数据类型创建包装器?

[英]How to create a wrappers in Swig for non primitive data types?

我做了什么:

我在ubuntu机器上安装了swig 3.0.5 为C ++代码创建了Java,python,android,C#包装器并对其进行了测试。 它运作良好。

我怎么了 我不知道如何使用Swig为非原始数据类型创建python,java等包装程序?

1.下面的示例cpp文件

example.cpp

  #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>

    using namespace cv;
    using namespace std;

    Mat sample(Mat image)
    {


      //  Mat image;
     //   image = imread("MyPic.jpg",1);   // Read the file

        if(! image.data )                              // Check for invalid input
        {
            cout <<  "Could not open or find the image" << std::endl ;
            return -1;
        }

      rectangle(image,Point(200,250),Point(500,600),Scalar(255,0,0));
           return image;
    }

2.下面的代码是接口文件

example.i

%module example

%{
/* Put header files here or function declarations like below */



extern Mat sample(Mat image);

%}



extern Mat sample(Mat image);

如何在Swig中为非原始数据类型创建包装器?

您必须将Mat转换为SWIG生成的SWIGTYPE_p_Mat:

// Read your image
Mat imgSrc = Highgui.imread("source_img.jpg");      
// Convert to Swig Object
SWIGTYPE_p_Mat swigMatIn = new SWIGTYPE_p_Mat(imgSrc.nativeObj, true);
// Call the function
SWIGTYPE_p_Mat swigMatOut = example.sample(swigMatIn);
// Convert the result to Mat from Swig Obj
Mat resultMat = new Mat(SWIGTYPE_p_Mat.getCPtr(swigMatOut));
// Write to disk
Highgui.imwrite("target_img.jpg", resultMat);

“示例”是由SWIG(example.java)生成的类:

...
public static SWIGTYPE_p_Mat sample(SWIGTYPE_p_Mat arg0) {
return new SWIGTYPE_p_Mat(symbolsdetectJNI.sample(SWIGTYPE_p_Mat.getCPtr(arg0)), true); }
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM