Open ghost opened 4 years ago
Done! Working with VC 19 and not CMake is really hard :)
Code: https://github.com/QuantScientist/Siv3DTorch/blob/master/src/readpng004.cpp
You can use s3d::Image
class for image I/O and image processing.
For now, I am converting the tensor to a PNG and writing the PNG to disk and then reading it with siv3d. It is not very fast bu that Is what I have now. I looked at s3d::Image but spent a day trying to figure out how to do s3d::Image to torch::tensor
and vice versa.
This is my torch to PNG and vice versa code:
https://github.com/QuantScientist/PngTorch/blob/master/include/utils/vision_utils.hpp
Thanks,
Conversion functions will be like this:
torch::Tensor ImageToTensor(const Image& image)
{
Array<uint8> buffer(image.num_pixels() * 3);
uint8* pDst = buffer.data();
for (const auto& pixel : image)
{
*pDst++ = pixel.r;
*pDst++ = pixel.g;
*pDst++ = pixel.b;
}
const int32 width = image.width();
const int32 height = image.height();
// ...
}
Image TensorToImage(const torch::Tensor& tensor)
{
size_t width = ??;
size_t height = ??;
const uint8* pSrc = tensor.data_ptr<uint8>();
Image image(width, height);
for (auto& pixel : image)
{
pixel.r = *pSrc++;
pixel.g = *pSrc++;
pixel.b = *pSrc++;
pixel.a = 255;
}
return image;
}
Dear team, After integrating Siv3D with Libtorch (https://github.com/QuantScientist/Siv3DTorch) I am now trying to read and write images from and to Siv3D. The way it works is:
torch::tensor
This is one example where they used stb_image to this, avoiding the use of OpenCV. https://github.com/prabhuomkar/pytorch-cpp/blob/master/utils/image_io/src/image_io.cpp
I did something similar and was able to read the image, but I am not sure how to proceed and display it on Siv3D. I don't mind using libpng / libjpg that you are linking to if you think I should do so.
Code for reading an image and displaying its dimensions on Siv3D (https://github.com/QuantScientist/Siv3DTorch/blob/master/src/loadmodel003.cpp):
For reference this is the OpenCV to Libtorch conversion utils:
Many thanks,