首页 > 解决方案 > 如何使用 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;
}

标签: c++pybind11

解决方案


推荐阅读