非常感谢!!< /P> < P>用C++ 17的代码>代码::可选的< /C> >,这里有一个例子应该是有用的。对于C++的早期版本,你可能需要一个后端的可选的.h < /C> >并且实现你自己的 opthalalxgaster < /C> >类似于 PybDun11/STL.H./C>,python,c++,numpy,binding,pybind11,Python,C++,Numpy,Binding,Pybind11" /> 非常感谢!!< /P> < P>用C++ 17的代码>代码::可选的< /C> >,这里有一个例子应该是有用的。对于C++的早期版本,你可能需要一个后端的可选的.h < /C> >并且实现你自己的 opthalalxgaster < /C> >类似于 PybDun11/STL.H./C>,python,c++,numpy,binding,pybind11,Python,C++,Numpy,Binding,Pybind11" />

Pybind11默认参数numpy数组或无 我正在包装一些C++代码,以便从Python中使用它。我想用一个参数调用C++函数,它可以取一个与另一个输入变量相同大小的 NOT/值或 NUMPY。例如: 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) 如何在C++中用PybDun11编码它?我有以下函数签名和代码: 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? ); void-fit(常量py::数组和输入, int k, 常量py::数组(掩码和掩码) { ... } PYBIND11_模块(示例,m) { m、 定义(“适合”和“适合”, py::arg(“输入”), py::arg(“k”), py::arg(“mask”)=nullptr//不知道在这里放什么? ); >非常感谢!!< /P> < P>用C++ 17的代码>代码::可选的< /C> >,这里有一个例子应该是有用的。对于C++的早期版本,你可能需要一个后端的可选的.h < /C> >并且实现你自己的 opthalalxgaster < /C> >类似于 PybDun11/STL.H./C>

Pybind11默认参数numpy数组或无 我正在包装一些C++代码,以便从Python中使用它。我想用一个参数调用C++函数,它可以取一个与另一个输入变量相同大小的 NOT/值或 NUMPY。例如: 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) 如何在C++中用PybDun11编码它?我有以下函数签名和代码: 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? ); void-fit(常量py::数组和输入, int k, 常量py::数组(掩码和掩码) { ... } PYBIND11_模块(示例,m) { m、 定义(“适合”和“适合”, py::arg(“输入”), py::arg(“k”), py::arg(“mask”)=nullptr//不知道在这里放什么? ); >非常感谢!!< /P> < P>用C++ 17的代码>代码::可选的< /C> >,这里有一个例子应该是有用的。对于C++的早期版本,你可能需要一个后端的可选的.h < /C> >并且实现你自己的 opthalalxgaster < /C> >类似于 PybDun11/STL.H./C>,python,c++,numpy,binding,pybind11,Python,C++,Numpy,Binding,Pybind11,假设您需要此功能: def add(a,b=None): #假设a,b是int。 如果b为无: 归还 其他: 返回a+b 这里是等效的C++ PyBin实现: m.def(“添加”, [](内部a,标准::可选b){ 如果(!b.具有_值()){ 返回a; }否则{ 返回a+b.值(); } }, py::arg(“a”),py::arg(“b”)=py::none() ); 在python中,可以通过以下方式调用此函数: 添加(1) 加(1、2) 加(1,b=2) 添加(1,b=无) 对

假设您需要此功能:

def add(a,b=None):
#假设a,b是int。
如果b为无:
归还
其他:
返回a+b

这里是等效的C++ PyBin实现:

m.def(“添加”,
[](内部a,标准::可选b){
如果(!b.具有_值()){
返回a;
}否则{
返回a+b.值();
}
},
py::arg(“a”),py::arg(“b”)=py::none()
);
在python中,可以通过以下方式调用此函数:

添加(1)
加(1、2)
加(1,b=2)
添加(1,b=无)

对于numpy数组,只需使用示例中的
std::optional
std::optional
修改
std::optional

我认为您可以使用与