[英]Using opencv types with 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 绑定有什么问题? 当我执行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
静态函数绑定,但是如何绑定非静态函数,即 func2? 以及如何确保调用MyClass
构造函数?
这是非常有用的。
我今天尝试用OpenCV
用 Python 调用C++
API。 并遇到错误消息:
Expected in: flat namespace.
按照您的代码,我在target_link_libraries
添加PRIVATE ,该错误已解决。
MyClass::func5
不是静态成员函数。 在调用它之前将其设为静态或创建一个实例:
from pybind11_example import MyClass
m = MyClass()
m.func5(3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.