Skip to content
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

SaveImage does not preserve data type #1979

Open
bruno-f-cruz opened this issue Aug 20, 2024 · 3 comments
Open

SaveImage does not preserve data type #1979

bruno-f-cruz opened this issue Aug 20, 2024 · 3 comments

Comments

@bruno-f-cruz
Copy link
Contributor

bruno-f-cruz commented Aug 20, 2024

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.

@bruno-f-cruz bruno-f-cruz changed the title SaveImage does not preserve depth of incoming data SaveImage does not preserve data type Aug 20, 2024
@PathogenDavid
Copy link
Member

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
Copy link
Contributor Author

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

@PathogenDavid
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants