简体   繁体   English

如何将 torch::Tensor 转换为 cv::InputArray?

[英]How to Convert a torch::Tensor into a cv::InputArray?

I'm trying to use the cv::getAffineTransform() , and I'm stuck on how to convert the tensors into the corrosponding cv::InputArray .我正在尝试使用cv::getAffineTransform() ,但我一直坚持如何将张量转换为相应的cv::InputArray I have tried these to face access violation:我已经尝试过这些来面对访问冲突:

torch::Tensor src = torch::tensor({ 1.1, 2.0, 3.3 });
torch::Tensor dst = torch::tensor({ 1.1, 2.0, 3.3 });

cv::Mat input_array;
cv::Mat destination_array;
std::memcpy(input_array.data, src.data_ptr<float>(), sizeof(float) * src.numel());
std::memcpy(destination_array.data, dst.data_ptr<float>(), sizeof(float) * dst.numel());

What am I doing wrong here?我在这里做错了什么? Is there a way to share the underlying buffer and avoid the copying at all?有没有办法共享底层缓冲区并完全避免复制?

It truns out specifying the dimensions in the cv::Mat was necessary in order for a successful copy, That is: I needed to do this :事实证明,为了成功复制,必须在cv::Mat中指定尺寸,即:我需要这样做:

cv::Mat input_array (3, 1, CV_32FC1);
cv::Mat destination_array (3, 1, CV_32FC1);

std::memcpy(input_array.data, src.data_ptr<float>(), sizeof(float) * src.numel());
std::memcpy(destination_array.data, dst.data_ptr<float>(), sizeof(float) * dst.numel());

std::cout << input_array << std::endl;
std::cout << destination_array << std::endl;

And this no more results in an Access violation.这不再导致访问冲突。 and I can verify that the values do get copied:我可以验证这些值是否被复制:

[1.1;
 2;
 3.3]
[1.1;
 2;
 3.3]

Since the former example was using a madeup input data, the cv::getAffineTransform() will crash so here is a more realistic input and output you can run and see that it works:由于前一个示例使用了虚构的输入数据,因此cv::getAffineTransform()将崩溃,因此这是一个更真实的输入,您可以运行 output 并查看它是否有效:

Method 1: Using std::memcpy to copy the data:方法一:使用std::memcpy复制数据:

torch::Tensor src = torch::tensor({ {137.47012, 62.52604}, {170.50703, 64.21498}, {154.49675, 80.78379} });
torch::Tensor dst = torch::tensor({ {38.294598, 51.6963}, {73.5318, 51.5014}, {56.0252, 71.7366} });

std::cout << "src.shapes: " << src.sizes() << std::endl;
std::cout << "dst.shapes: " << dst.sizes() << std::endl;

int rows = src.sizes()[0];
int cols = (src.sizes().size() == 1) ? 1 : src.sizes()[1];

cv::Mat input_array (rows, cols, CV_32FC1);
cv::Mat destination_array (rows, cols, CV_32FC1);

std::memcpy(input_array.data, src.data_ptr<float>(), sizeof(float) * src.numel());
std::memcpy(destination_array.data, dst.data_ptr<float>(), sizeof(float) * dst.numel());

std::cout << "input_array:\n" << input_array << std::endl;
std::cout << "destination_array:\n" << destination_array << std::endl;

auto tfm = cv::getAffineTransform(input_array, destination_array);
std::cout << "tfm:\n" << tfm << std::endl;

And
Method 2: Using the underlying buffer instead of copying:方法2:使用底层缓冲区而不是复制:

int height = src.sizes()[0];
int width = src.sizes()[1];
cv::Mat input_array(cv::Size{width, height }, CV_32F, src.data_ptr<float>());
cv::Mat destination_array(cv::Size{ width, height }, CV_32F, dst.data_ptr<float>());

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

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