Convert torch::tensor to cv::Mat

Hi @JeeLee

you should be able to use a std::memcpy like so

#include <torch/torch.h>
#include <opencv2/core/core.hpp>

int main()
{
   cv::Mat cv_mat = cv::Mat::eye(3,3,CV_32F);
   torch::Tensor tensor = torch::zeros({3, 3}, torch::kF32);

   std::memcpy(tensor.data_ptr(), cv_mat.data, sizeof(float)*tensor.numel());

   std::cout << cv_mat << std::endl;
   std::cout << tensor << std::endl;

   return 0;
}

and your CMakeLists.txt

cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project(torch_to_cv)

find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)

include_directories(${TORCH_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(main main.cpp)
target_link_libraries(main ${TORCH_LIBRARIES} ${OpenCV_LIBRARIES})

of course this also means that you’ll lose track of the gradients. And of course if you are dealing with higher dimensional tensors, as you already pointed out, you’ll need to be aware that OpenCV stores images HxWxC, while for Pytorch they are stored as CxHxW. Using permute() for that is perfectly fine.

4 Likes