title: Convert pytorch tensor to opencv mat and vice versa in C++
body: <p>I want to convert pytorch tensors to opencv mat and vice versa in C++. I have these two functions:</p>
  
  <pre><code>cv::Mat TensorToCVMat(torch::Tensor tensor)
  {
      std::cout &lt;&lt; "converting tensor to cvmat\n";
      tensor = tensor.squeeze().detach().permute({1, 2, 0});
      tensor = tensor.mul(255).clamp(0, 255).to(torch::kU8);
      tensor = tensor.to(torch::kCPU);
      int64_t height = tensor.size(0);
      int64_t width = tensor.size(1);
      cv::Mat mat(width, height, CV_8UC3);
      std::memcpy((void *)mat.data, tensor.data_ptr(), sizeof(torch::kU8) * tensor.numel());
      return mat.clone();
  }
  
  torch::Tensor CVMatToTensor(cv::Mat mat)
  {
      std::cout &lt;&lt; "converting cvmat to tensor\n";
      cv::cvtColor(mat, mat, cv::COLOR_BGR2RGB);
      cv::Mat matFloat;
      mat.convertTo(matFloat, CV_32F, 1.0 / 255);
      auto size = matFloat.size();
      auto nChannels = matFloat.channels();
      auto tensor = torch::from_blob(matFloat.data, {1, size.height, size.width, nChannels});
      return tensor.permute({0, 3, 1, 2});
  }
  </code></pre>
  
  <p>In my code I load two images (<code>image1</code> and <code>image2</code>) and I want to convert them to pytorch tensors and then back to opencv mat to check if it works. The problem is that I get an memory access error on the first call of <code>TensorToCVMat</code> and I cant figure out whats wrong as I do not have much experience with C++ programming.  </p>
  
  <pre><code>cv::Mat image1;
  image1 = cv::imread(argv[1]);
  if (!image1.data)
  {
      std::cout &lt;&lt; "no image data\n";
      return -1;
  }
  cv::Mat image2;
  image2 = cv::imread(argv[2]);
  if (!image2.data)
  {
      std::cout &lt;&lt; "no image data\n";
      return -1;
  }
  
  torch::Tensor tensor1 = CVMatToTensor(image1);
  cv::Mat new_image1 = TensorToCVMat(tensor1); // &lt;&lt;&lt; this is where the memory access error is thrown
  torch::Tensor tensor2 = CVMatToTensor(image2);
  cv::Mat new_image2 = TensorToCVMat(tensor2);
  </code></pre>
  
  <p>It would be great if you could give me hints or an explanation to solve this problem.</p>