【发布时间】:2019-12-10 15:29:13
【问题描述】:
我正在包装一些 C++ 代码以在 Python 中使用它。我想调用一个带有参数的 C++ 函数,该参数可以采用与另一个输入变量相同大小的 None 值或 numpy.array。这是一个例子:
import example
# Let I a numpy array containing a 2D or 3D image
M = I > 0
# Calling the C++ function with no mask
example.fit(I, k, mask=None)
# Calling the C++ function with mask as a Numpy array of the same size of I
example.fit(I, k, mask=M)
如何使用 pybind11 在 C++ 中对其进行编码?我有以下函数签名和代码:
void fit(const py::array_t<float, py::array::c_style | py::array::forcecast> &input,
int k,
const py::array_t<bool, py::array::c_style | py::array::forcecast> &mask)
{
...
}
PYBIND11_MODULE(example, m)
{
m.def("fit", &fit,
py::arg("input"),
py::arg("k"),
py::arg("mask") = nullptr // Don't know what to put here?
);
非常感谢!
【问题讨论】:
-
我认为您可以使用与this question相同的解决方案
标签: python c++ numpy binding pybind11