[英]reading signed ints from openCV in/to libtorch tensor c++
I have a cv::Mat which is CV_32SC3 type, it stores both positive and negative values.我有一个 cv::Mat 它是 CV_32SC3 类型,它存储正值和负值。
When convert it to tensor, the values are messed up:将其转换为张量时,值会混乱:
cout << in_img << endl;
auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, torch::kByte);
The in_img has negative values, while after print out tensor_image, the values were all totally different than in_img. in_img 有负值,而打印出 tensor_image 后,这些值都与 in_img 完全不同。
the negative values are gone (it somehow seems to normilise it 255 range).负值消失了(它似乎以某种方式将其标准化为 255 范围)。 I tried converting to Long like so:
我尝试像这样转换为 Long:
auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, torch::kLong);
but when I print the values like so, I get seg fault:但是当我像这样打印值时,我得到了段错误:
std::cout << "tensor_image: " << tensor_image << " values." << std::endl;
so, I tried looking at just the first element like so:所以,我试着只看第一个元素,如下所示:
std::cout << "input_tensor[0][0][0][0]: " << tensor_image[0][0][0][0] << " values." << std::endl;
and the value is not the same as I see in the python implementation:((并且该值与我在 python 实现中看到的不同:((
The type 32SC3
means that your data are 32bits (4 bytes) signed integers, ie int
s.类型
32SC3
表示您的数据是 32 位(4 字节)有符号整数,即int
s。 Pytorch kByte
type means unsigned char
(1 byte, values between 0 and 255). Pytorch
kByte
类型表示unsigned char
(1 个字节,值介于 0 和 255 之间)。 Therefore you are actually reading a matrix of ints as if it were a matrix of uchars.因此,您实际上是在读取整数矩阵,就好像它是 uchar 矩阵一样。
Try with尝试
auto tensor_image = torch::from_blob(in_img.data, {1, in_img.rows, in_img.cols, 3}, torch::kInt32);
The conversion to kLong
was bound to fail because long
means int64
.转换为
kLong
必然会失败,因为long
表示int64
。 So there are just not enough bytes in your opencv int32
matrix to read it as a int64
matrix with the same size.因此,您的 opencv
int32
矩阵中没有足够的字节将其读取为具有相同大小的int64
矩阵。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.