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();
}
我如何得到逆?來自型別變數的 C 型別dtype?
編輯
我最初的問題是我想將 OpenCV 影像從 python 帶到 c ,如此處所述,請注意,在此答案中完成的轉換始終是型別unsigned char*:
cv::Mat img2(rows, cols, type, (unsigned char*)img.data());
我想要做的是用來.dtype()獲取“python 型別”,然后進行正確的轉換。
因此,我的函式簽名采用py::arrayas 引數。
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
}
也許我可以type_id從我的dtype?
uj5u.com熱心網友回復:
cv::Mat有一個接受資料指標的建構式。void*
因此,您不需要將其dtype轉換為 C 型別。
您可以簡單地使用:
cv::Mat img2(rows, cols, type, (void*)img.data());
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/497356.html
