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

时间:2015-03-02 11:43:32

标签: java c++ opencv swig

我做了什么:

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

我的问题是什么? 我不知道如何使用Swig为非原始数据类型创建python,java等封装器?

1.Below sample cpp file

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.Below代码是接口文件

example.i

%module example

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



extern Mat sample(Mat image);

%}



extern Mat sample(Mat image);

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

1 个答案:

答案 0 :(得分:1)

您必须将您的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);

&#39;例如&#39;是由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); }
...
相关问题