简体   繁体   English

如何将cv :: mat对象从python模块传递到c ++函数,并返回返回的cv :: mat类型的对象?

[英]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. 我尝试使用Django启动一个项目,其中一部分是显示一些图像。 As probably you know, c++ is so faster than python. 您可能知道,c ++比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. 因此,我编写了一个c ++函数,该函数接收两个Mat类型的输入,并对它们进行一些预处理,最后返回一个cv :: mat变量作为其输出。

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. 我想在我的python模块中调用此函数,并从我的python代码中发送两个图像作为输入参数,并在django项目中显示c ++函数的结果。

I tried to call my c++ function with ctypes.CDLL, ctypes work with simple functions but for this c++ code gives a memory error. 我试图用ctypes.CDLL调用我的c ++函数,ctypes使用简单的函数,但是为此c ++代码提供了内存错误。

this is my c++ function: 这是我的C ++函数:

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. 我在网上进行了大量搜索,找到了一些有关为Python包装C / C ++的说明。 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? 因为我是Django的新手,有人可以帮助我如何从我的python代码(我有两个带有c ++函数的图像)进行协商以对图像进行一些操作,然后将返回的输出保存在Django中吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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