Ask Your Question

Revision history [back]

How to convert cv2.cuda_GpuMat to cv::cuda::GpuMat

I am trying to create a wrapper in pybind11 to create part of the code in c ++. The problem is that I have to transfer a cuda_gpuMat image to its namesake cv :: cuda :: GpuMat in c ++.

Is there a way?

at::Tensor gpumat2torch(cv::cuda::GpuMat& frame) {
    at::ScalarType torch_type = get_torch_type(frame.type());
    auto options = torch::TensorOptions().dtype(torch_type).device(torch::kCUDA);
    std::vector<int64_t> sizes = {1,
                                  static_cast<int64_t>(frame.channels()),
                                  static_cast<int64_t>(frame.rows),
                                  static_cast<int64_t>(frame.cols)};
    return torch::from_blob(frame.data, sizes, options);
}
PYBIND11_MODULE(gpumat, m) {
    m.doc() = "gpumat2torch function!";
    m.def("gpumat2torch", &gpumat2torch, "A function to convert GpuMat to CudaTorch");
#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}