简体   繁体   English

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

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

What i did: 我做了什么:

I installed the swig 3.0.5 on my ubuntu machine . 我在ubuntu机器上安装了swig 3.0.5 Created Java, python, android, C# wrappers for C++ code and tested it. 为C ++代码创建了Java,python,android,C#包装器并对其进行了测试。 It works well. 它运作良好。

What is my problem? 我怎么了 I don't know how to create python, java, etc wrappers for non primitive data types with Swig? 我不知道如何使用Swig为非原始数据类型创建python,java等包装程序?

1.Below sample cpp file 1.下面的示例cpp文件

example.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.Below code is interface file 2.下面的代码是接口文件

example.i example.i

%module example

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



extern Mat sample(Mat image);

%}



extern Mat sample(Mat image);

How to create a wrappers in Swig for non primitive data types? 如何在Swig中为非原始数据类型创建包装器?

You must convert your Mat to SWIGTYPE_p_Mat, that was generated by 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);

'example' is your class generated by SWIG (example.java): “示例”是由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