pybind11在numpy.h
中有以下方法:
/// Return dtype associated with a C++ type.
template <typename T>
static dtype of() {
return detail::npy_format_descriptor<typename std::remove_cv<T>::type>::dtype();
}
我如何得到相反的結果?來自dtype
類型變量的C++類型?
EDIT
我最初的問題是,我想將OpenCV圖像從python帶到c++,如這里所述,請注意,在這個答案中所做的轉換總是unsigned char*
類型:
cv::Mat img2(rows, cols, type, (unsigned char*)img.data());
我想做的是使用.dtype()
獲得"python類型,然后進行正確的轉換。
因此,我的函數簽名采用py::array
作為參數。
int cpp_callback1(py::array& img)
{
//stuff
cv::Mat img2(rows, cols, type, (unsigned char*)img.data());
^^^ Somehow I want this to be variable
//more stuff
}
也許我可以從我的dtype
得到type_id
?
cv::Mat
有一個接受void*
作為數據指針的構造函數。因此,您不需要將
dtype
轉換為C++類型。您可以簡單地使用: