简体   繁体   English

如何将 opencv mat 灰度图像转换为 pytorch 张量?

[英]How to convert opencv mat grayscale image to pytorch tensor?

I want to convert cv::Mat class into torch::Tensor class and put them into CNN module of libtorch for prediction.我想将 cv::Mat class 转换为 torch::Tensor class 并将它们放入 libtorch 的 CNN 模块中进行预测。 How should I convert Mat class to Tensor class?我应该如何将 Mat class 转换为张量 class? My CNN module is a pt file generated using the mnist training set.我的 CNN 模块是使用 mnist 训练集生成的 pt 文件。 The training image of this mnist training set is single-channel gray image.这个mnist训练集的训练图像是单通道灰度图像。 If I want to test, it should load a gray mat image, so I want to change the single-channel gray image converted to tensor.如果我要测试,它应该加载一个灰度图,所以我想将单通道灰度图转换为张量。 The language I use is c++。我使用的语言是c++。

The idea is to copy memory from cv::Mat object to torch::Tensor object:想法是将 memory 从 cv::Mat object 复制到 torch::Tensor object:

    cv::Mat img =cv::imread("path/to/file.png");
    cv::cvtColor(img,img,cv::COLOR_BGR2GRAY);
    torch::Tensor tensor = torch::empty({img.cols,img.rows},torch::TensorOptions().dtype(torch::kUInt8));
    memcpy(tensor.data<void>(),img.data,img.rows*img.cols);

Thank you for your help.谢谢您的帮助。 Finally, I found a solution.最后,我找到了解决方案。 Before, I didn't know much about the tensor class.之前,我对张量 class 了解不多。 A tensor has four elements, which are the number, height, width, and byte depth of the image.张量有四个元素,分别是图像的数量、高度、宽度和字节深度。

  std::string p1 = "D:";
  std::string p2;
  std::cin >> p2;
  std::string p3 = ".png";
  std::string path = p1 + p2 + p3;
  cv::Mat image = cv::imread(path,cv::IMREAD_GRAYSCALE);
  torch::Tensor img_tensor = torch::from_blob(image.data, { 1, image.rows, image.cols, 1 }, torch::kByte);
  img_tensor = img_tensor.permute({ 0, 3, 1, 2 });
  img_tensor = img_tensor.toType(torch::kFloat);
  img_tensor = img_tensor.div(255);
  auto a = net->predict(img_tensor);
  std::cout << a.argmax() << std::endl;

I use this combination for convert openCV Images to tensor of libtorch.我使用这种组合将 openCV 图像转换为 libtorch 的张量。 I use libtorch-cxx11-abi-shared-with-deps-1.10.0+cu102 and C++ STANDARD 14.我使用 libtorch-cxx11-abi-shared-with-deps-1.10.0+cu102 和 C++ STANDARD 14。

cv::Mat image = cv::imread("test.jpeg")// Any image to load...
cv::Mat input; // Conversion of image in to color format or any format [1].
cv::cvtColor(image, input,  cv::COLOR_RGBGRAY); 
//cv::cvtColor(image, input, cv::COLOR_BGR2RGB);    //or for color  
                               
std::cout << "Check image dimentions" <<std::endl;  // Check conversion
std::cout << "pixel dim x: "<<input.rows<< std::endl;
std::cout << "pixel dim x: "<<input.cols<< std::endl;
//Image to tensor conversion.
torch::Tensor tensor_image = torch::from_blob(input.data, {1, input.rows, input.cols, 3}, torch::kByte);
//Permute data structure to Tensor format.
tensor_image = tensor_image.permute({0, 3, 1, 2});
tensor_image = tensor_image.toType(torch::kFloat);
tensor_image = tensor_image.div(255);
//load to CUDA device if need...
tensor_image = tensor_image.to(torch::kCUDA);
std::cout<<"Img to Tensor converted!"<<std::endl;

[1] https://docs.opencv.org/3.4/d8/d01/group__imgproc__color__conversions.html [1] https://docs.opencv.org/3.4/d8/d01/group__imgproc__color__conversions.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM