python - 在 pybind11 中使用 opencv 类型

标签 python c++ opencv pybind11

我想使用 pybind11 为使用 opencv 类型的函数创建一个包装器。

这是我的尝试:

例子.hpp

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

class MyClass
{
public:
    MyClass();

    void func1(cv::Rect &rc);
    cv::Rect func2(cv::Rect &rc);
    static void func3(cv::Rect &rc);
    static cv::Rect func4(cv::Rect &rc);
    void func5(int f);
};

例子.cpp

#include "example.hpp"

#include <iostream>

    MyClass::MyClass()
    {
        std::cout << "MyClass::MyClass() called! " << std::endl;
    }

    void MyClass::func1(cv::Rect &rc)
    {

    }

    cv::Rect MyClass::func2(cv::Rect &rc)
    {
        cv::Rect rc2(10,20,30,40);
        return rc2;
    }

    void MyClass::func3(cv::Rect &rc)
    {

    }

    cv::Rect MyClass::func4(cv::Rect &rc)
    {
        cv::Rect rc2(10,20,30,40);
        return rc2;
    }


    void MyClass::func5(int f)
    {

    }

pybind11_example.cpp

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#include "example.hpp"

namespace py = pybind11;

PYBIND11_MODULE(pybind11_example, m) {
    py::class_<MyClass>(m, "MyClass")
    .def(py::init<>())
    .def("func1", &MyClass::func1, "some func1", py::arg("rc"))
    .def("func5", &MyClass::func5, "some func5", py::arg("f"));


    // Static function binding with passing custom type and return custom type
    m.def("func4", [](std::vector<int> &v) -> py::array {
        if (v.size()!=4)
        {
            throw std::runtime_error("v.size()!=4");
        }

        cv::Rect rc;
        cv::Rect rc2 = MyClass::func4(rc);
        std::vector<int> v2;
        v2.push_back(rc2.x);
        v2.push_back(rc2.y);
        v2.push_back(rc2.width);
        v2.push_back(rc2.height);
        return py::array(v2.size(), v2.data());
    });
}

CMakeLists.txt

project("pybind11_example")
cmake_minimum_required(VERSION 2.8)

find_package(PythonLibs)
execute_process(
    COMMAND "python3" "-c" "import os, numpy.distutils; print(os.pathsep.join(numpy.distutils.misc_util.get_numpy_include_dirs()))"
    RESULT_VARIABLE _numpy_process
    OUTPUT_VARIABLE _numpy_include_dirs
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
if (NOT _numpy_process EQUAL 0)
    message(FATAL_ERROR "Can't locate numpy include dirs")
endif()
include_directories(${PYTHON_INCLUDE_DIRS} ${_numpy_include_dirs})
message("Python libs: ${PYTHON_LIBRARIES}")

find_package(pybind11 REQUIRED)
pybind11_add_module(pybind11_example pybind11_example.cpp example.cpp)
target_link_libraries(pybind11_example PRIVATE ${OpenCV_LIBS} ${Boost_LIBRARIES})

这是我的问题:

func5 绑定(bind)有什么问题?当我执行 from pybind11_example import MyClass 并且例如 MyClass.func5(3) 我得到错误:

MyClass.func5(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: func5(): incompatible function arguments. The following argument types are supported:
    1. (self: pybind11_example.MyClass, f: int) -> None

Invoked with: 3

我能够使用一个技巧(参见 func4)将静态函数与 opencv 类型 cv::Rect 绑定(bind),但是如何绑定(bind)非静态函数,即 func2?以及如何确保调用 MyClass 构造函数?

最佳答案

这很有用。

我今天尝试使用 OpenCV 使用 Python 调用 C++ API。并遇到错误消息:

Expected in: flat namespace.

按照你的代码,我在 target_link_libraries 中添加了 PRIVATE,错误已解决。

关于python - 在 pybind11 中使用 opencv 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54685951/

相关文章:

python - 选择Group By参数的最大值

python - Pandas Dataframe 检查交集并填写新的 dataframe

c++ - 位字段结构分配意外行为

c++ - vtune - 没有可用的符号

python - 如何使用 anaconda3 获取 python3 的 opencv?

opencv - 使用 OpenCV 和 Tesseract 的摩洛哥车牌识别 (LPR)

python - 我应该把 shebang 行放在每个 python 文件中吗?

python - 启动时启动 logme.py

c++ - 嵌套for循环解决方案

运行 make CentOS 时出现 OpenCV 错误