New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help with reading and writing images #534
Comments
Done! Code: |
You can use |
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 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,
The text was updated successfully, but these errors were encountered: