如何将 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?
我尝试用Django启动一个项目,其中一部分是关于显示一些图像。 你可能知道,c++比python快得多。所以我写了一个 c++ 函数,它接收两个 Mat 类型的输入并对它们进行一些预处理,最后返回一个 cv::mat 变量作为输出。
我想在我的 python 模块中调用这个函数,并从我的 python 代码发送两个图像作为输入参数,并在我的 django 项目中显示 c++ 函数的结果。
我尝试用ctypes调用我的c ++函数。CDLL,ctypes 使用简单的函数,但对于此 c++ 代码会给出内存错误。
这是我的 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;
}
如您所见,这是一个简单的函数,不会使用大量内存。我测试了一些非常小的图片,我看到了同样的错误。
我在网上搜索了很多,找到了一些关于Wrapping C/C++ for Python的说明。 但我不确定它是否能帮助我。
因为我是 Django 的新手,任何人都可以帮助我如何从我的 python 代码中协商,我有两个带有 c++ 函数的图像来对图像进行一些操作并将返回的输出保存在我的 Django 中?
也许可以考虑使用 Boost-Python 库来连接 Python 和 C++。
相关文章:
- 如何通过另一个对象中的命令正确地从一个对象返回数据
- 如何访问从 COM 对象返回的 VARIANT 数据类型中的安全数组C++?
- 从我的对象返回静态数组
- 将unique_ptr作为<Object>unique_ptr<常量对象返回>
- 从右值对象返回成员
- 视觉对象 返回 C++ 中的双精度值
- 为什么类型为 sf::Text 的对象返回不同的 getPosition().y 和 getLocalBounds().
- std::min_element 从类对象返回意外结果
- C++无效的对象返回语义
- const引用是否延长临时对象返回的临时对象的寿命
- 将 NULL 作为对象返回时未收到任何警告
- 如何在 C# 中从 com 对象返回数组(double[])
- 从重载运算符返回引用,并使用临时对象返回表达式
- Cin 对象返回值 c++
- 将变量作为类对象返回
- 使用可更改对象返回只读的最佳方法是什么
- 如何在Cython中从另一个包装对象返回包装的c++对象
- 当对象返回时,c++动态数组被清除
- 从带有动态字段的函数、对象返回
- 从堆栈上的匿名对象返回对*this的引用