Libtorch tensor to image with OpenCV

I’m trying to plot an image using OpenCV, from a torch::tensor with rgb values (dim=100x100x3), but the output image is all black. The rgb tensor has float values between 0 and 1.

I’m using the following code:

auto img = cv::Mat(100, 100, CV_32FC3, rgb.data_ptr<float>());
cv::cvtColor(img, img, cv::COLOR_BGR2RGB);
cv::imwrite("../new_img.jpg", img);

Am I missing something?

imwrite expects int8 or int16 images as explained in the docs:

The function imwrite saves the image to the specified file. The image format is chosen based on the filename extension (see imread() for the list of extensions). Only 8-bit (or 16-bit unsigned (CV_16U) in case of PNG, JPEG 2000, and TIFF) single-channel or 3-channel (with ‘BGR’ channel order) images can be saved using this function. If the format, depth or channel order is different, use Mat::convertTo() , and cvtColor() to convert it before saving. Or, use the universal FileStorage I/O functions to save the image to XML or YAML format.