C++ opencv矩阵和pytorch tensor的互相转换

矩阵和tensor相互转换

cvmat到tensor

tips:
这里主要要注意的就是在opencv和pytorch中存储顺序的差异


 cv::cvtColor(frame, frame, CV_BGR2RGB);
 //normalization
 frame.convertTo(frame, CV_32FC3, 1.0f / 255.0f);
 //opencv format H*W*C
 auto input_tensor = torch::from_blob(frame.data, {1, frame_h, frame_w, kCHANNELS});
 //pytorch format N*C*H*W
 input_tensor = input_tensor.permute({0, 3, 1, 2});

tensor 到cvmat

tips:
1.squeeze只用于batchsize为1的场景
2.permute 是将存储格式从pytorch形式转成opencv格式
3.因为在处理前对cvmat中的值做了归一化,所以现在要*255恢复,同时对于不在0-255范围内的数据,需要做限制
4.因为cvmat的数据格式是8UC3,所以torch tensor要提前转换成kU8

//send tensor to cpu
input_tensor = input_tensor.to(at::kCUDA);
//inference
torch::Tensor out_tensor = module->forward({input_tensor}).toTensor();
//sequeeze trans tensor shape from 1*C*H*W to C*H*W
//permute C*H*W to H*W*C
out_tensor = out_tensor.squeeze().detach().permute({1, 2, 0});
//see tip3,tip4
out_tensor = out_tensor.mul(255).clamp(0, 255).to(torch::kU8);
out_tensor = out_tensor.to(torch::kCPU);
cv::Mat resultImg(frame_h, frame_w, CV_8UC3);
//copy the data from out_tensor to resultImg
std::memcpy((void *) resultImg.data, out_tensor.data_ptr(), sizeof(torch::kU8) * out_tensor.numel());

如有错误,感谢指正!

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值