bonsai-rx / bonsai

The compiler, IDE, and standard library for the Bonsai visual programming language for reactive systems
https://bonsai-rx.org
MIT License
142 stars 29 forks source link

`SaveImage` does not preserve data type #1979

Open bruno-f-cruz opened 3 months ago

bruno-f-cruz commented 3 months ago

When attempting to save an image/mat using SaveImage, it appears that all data gets clamped to a 0-255 range and saved as U8. This is a pity since LoadImage will honor the data type of the incoming image.

In order to preserve the symmetry of the operation we should look into ways of allowing SaveImage to preserve the bit depth. A quick look at the OpenCV documentation shows a bit of a cryptic parameter array that I can't find documentation for (edit: maybe here https://docs.opencv.org/2.4.8/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=cvsaveimage#int%20cvSaveImage(const%20char*%20filename,%20const%20CvArr*%20image,%20const%20int*%20params)).

https://github.com/horizongir/opencv.net/blob/1702b764e787724899f76cb2fbec1fce78fba43b/src/OpenCV.Net/HighGui.cs#L92.

I also looked into ImageWriter but since it seems to use a BinaryWriter Writer, it doesn't seem to preserve the header necessary to ensure proper symmetry to LoadImage either.

PathogenDavid commented 2 months ago

Poking around in the OpenCV source code, it appears that it should preserve the incoming bit depth as long as the target image format supports it, otherwise it falls back to converting it to 8bpc. (See imwrite_)

What bit depth is your image and what format are you attempting to save?

For the version of OpenCV we use: PNG encoding supports unsigned 16bpc, OpenEXR encoding supports everything supported by OpenCV.

OpenCV also supports encoding unsigned 16bpc with TIFF, PxM, and JPEG2000 (not to be confused with normal JPEG)—but none of those would be my first choice for an image format in 2024.

bruno-f-cruz commented 2 months ago

Trying to use TIFF with single floats. IT seems that TIFF allows for this spec, but perhaps not opencv?

PathogenDavid commented 2 months ago

Correct, OpenCV's TIFF encoder does not support saving floats despite its ability to decode them.

TIFF is an extremely unwieldy format that's too flexible for its own good, so this isn't super surprising to me. Later versions of OpenCV do add support for encoding more formats but I would not hold your breath on us being able to upgrade.

I'd consider giving OpenEXR a try if you can.