Cristian
Cristian

Reputation: 115

Call C++ function from Python by reference

I have successfully wrapped my C++ code with SWIG and it loads fine into Python. I am using the Olena library for image processing.

However, I don't know how to call my functions that require a pointer to an image!

For instance, my function for eroding an image is prototyped as follows:

mln::image2d<mln::value::int_u8> imErossion(
    const mln::image2d<mln::value::int_u8> *img, int size, int nbh
);

Result of running my code within Python:

    from swilena import *
    from algol import *

    image = image2d_int_u8
    ima = image.load("micro24_20060309_grad_mod.pgm")

    eroded_ima = imErossion(ima,1,8) 
    >>>> Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: in method 'imErossion', argument 1 of type 
       'mln::image2d<mln::value::int_u8 > const *'

I have been looking all around the web to try and solve this myself, but it turns to be harder than I expected.

I'm not sure how to pass a pointer from Python -- the equivalent of this C++ code:

eroded_ima = imErossion(&ima,1,8)

Upvotes: 1

Views: 419

Answers (1)

Cristian
Cristian

Reputation: 115

I checked with my professor at university, and we decided it would be better to implemente a function that would return the pointer to the image when this one was loaded and declared it global:

mln::image2d<mln::value::int_u8> working_img;

mln::image2d<mln::value::int_u8> *imLoad(const std::string path){
    mln::io::pgm::load(working_img, path);
    return &working_img;
}

void imSave(const std::string path){

    mln::io::pgm::save(working_img, path);

}

What do you think about this?

Upvotes: 3

Related Questions