简体   繁体   中英

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 . Created Java, python, android, C# wrappers for C++ code and tested it. 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?

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 code is interface file

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?

You must convert your Mat to SWIGTYPE_p_Mat, that was generated by SWIG:

// 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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