简体   繁体   中英

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.

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.

the negative values are gone (it somehow seems to normilise it 255 range). I tried converting to Long like so:

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:((

The type 32SC3 means that your data are 32bits (4 bytes) signed integers, ie int s. Pytorch kByte type means unsigned char (1 byte, values between 0 and 255). Therefore you are actually reading a matrix of ints as if it were a matrix of uchars.

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 . So there are just not enough bytes in your opencv int32 matrix to read it as a int64 matrix with the same size.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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