C++键入dtype(如何从numpy数组初始化cv ::垫子)

发布于 2025-02-10 23:51:06 字数 925 浏览 1 评论 0 原文

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 ++类型?

编辑

我最初的问题是我想将OpenCV图像从Python带到C ++,如所述>请注意,此答案中完成的演员始终为 unsigned char*

cv::Mat img2(rows, cols, type, (unsigned char*)img.data());

我想做的是使用 .dtype()获取“ Python type “然后做正确的演员。

因此,我的功能签名将 py ::数组作为参数。

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

pybind11 has the following method in 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();
    }

How do I get the inverse? the C++ type from a variable of type dtype ?

EDIT

My original problem is that I want to bring an OpenCV image from python to c++ as described here, note that the cast done in this answer is always of type unsigned char*:

cv::Mat img2(rows, cols, type, (unsigned char*)img.data());

What I want to do is to use .dtype() to get the "python type" and then do the right cast.

Because of this, my function signature takes an py::array as parameter.

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

}

Maybe I can get the type_id from my dtype?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

°如果伤别离去 2025-02-17 23:51:06

cv :: mat 具有接受数据指针的 void*

因此,您无需将 dtype 转换为C ++类型。
您可以简单地使用:

cv::Mat img2(rows, cols, type, (void*)img.data());

cv::Mat has a constructor that accepts a void* for the data pointer.

Therefore you don't need to convert the dtype into a C++ type.
You can simply use:

cv::Mat img2(rows, cols, type, (void*)img.data());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文