简体   繁体   中英

How to pass cv::mat objects from a python module to a c++ function and give back returned object which is type cv::mat?

I try to start a project with Django which a part of that is about showing some images. As probably you know, c++ is so faster than python. so I wrote a c++ function which receives two Mat type input and do some pre processing on them and finally return a cv::mat variable as it's output.

I want to call this function inside my python module and send from my python code, two images as input argument and show the result of c++ function in my django project.

I tried to call my c++ function with ctypes.CDLL, ctypes work with simple functions but for this c++ code gives a memory error.

this is my c++ function:

extern "C" Mat watermark2(Mat source_img, Mat logo)
{
        // Simple watermark

        double alpha = 0.5;

        int width = logo.size().width;
        int height = logo.size().height;
        int x_pos = rand() % (source_img.size().width - width);
        int y_pos = rand() % (source_img.size().height - height);

        cv::Rect pos = cv::Rect(x_pos, y_pos, width, height);
        addWeighted(source_img(pos), alpha, logo, 1 - alpha, 0.0, source_img(pos));

        return source_img;
}

as you see, this is a simple function and don't use a lot of memory. I test it for some very small pictures and I saw the same error.

I search a lot in net and found some instructions about Wrapping C/C++ for Python. but I don't sure that it can help me.

because I'm new in Django, can anybody help me how to negotiate from my python code which I have two images with my c++ function to some manipulate on images and save the returned output in my Django?

也许考虑使用Boost-Python库在Python和C ++之间进行接口。

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