c++ - 如何使用 Pybind11 将图像(cv::Mat)从 C++ 发送到 Python?
问题描述
我正在尝试将图像(cv::Mat
确切地说)发送到 Python 方法。此处解释了反向操作,但是,我不确定如何将其发送到 Python!。stl
和标numpy
头包括在内,所以我认为应该发生自动转换,但似乎我错了。
这里缺少什么,我应该如何将图像发送到 Python?这是我现在失败的简单演示:
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/embed.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
namespace py = pybind11;
using namespace py::literals;
#include <opencv2/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
void show_image(cv::Mat img, std::string title)
{
cv::namedWindow(title, cv::WINDOW_NORMAL); // Create a window for display.
cv::imshow(title, img);
cv::waitKey(0);
}
int main()
{
py::scoped_interpreter guard{};
auto serviceUtilsModule = py::module::import("MyPackage.MyModule");
auto aligner = serviceUtilsModule.attr("aligner");
auto pil_converter = serviceUtilsModule.attr("cv_to_pil");
auto currentPath = "D:\\imgs\\img1.jpg";
auto img1 = cv::imread(currentPath);
//PyErr_Print();
auto img_pil = pil_converter(img1);
auto lst = aligner(img_pil);
py:print(lst);
return 0;
}
解决方案
推荐阅读
- postgresql - COPY 命令运行缓慢
- alfresco - Alfresco 如何使用 java 客户端在 DB 中存储元数据
- wordpress - 如何在插件 Polylang 中显示所有语言的帖子?
- clion - CLion 的 CMake GUI
- linux - Keybase "keybased.sock: 连接无 osuch 文件或目录"
- c# - Selenium C# - 将表值与可用值进行比较
- mysql - SQL如何在同一个数据库的不同表上的相同列名中添加相同的注释
- python - 无法从 Flask 执行 python 脚本
- mysql - 如果特定月份没有记录,如何获得 0 条记录?
- python - 使用 ImageDataGenerator 的预处理函数转换颜色空间