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 &lt;&lt; in_img &lt;&lt; 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 &lt;&lt; &quot;tensor_image: &quot; &lt;&lt; tensor_image &lt;&lt; &quot; values.&quot; &lt;&lt; std::endl;

so, I tried looking at just the first element like so:
std::cout &lt;&lt; &quot;input_tensor[0][0][0][0]: &quot; &lt;&lt; tensor_image[0][0][0][0] &lt;&lt; &quot; values.&quot; &lt;&lt; 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, i.e ints. 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.
