Ask Your Question

imwrite: TIFF header TIFFTAG_SAMPLEFORMAT not written

asked 2018-06-29 20:42:57 -0500

jonathan gravatar image

Writing an OpenCV Mat to a TIFF file using imwrite's default settings, I noticed that a standard TIFF tag, TIFFTAG_SAMPLEFORMAT, is not written.


The OpenCV Mat I'm writing has type CV_8UC3, so it seems as though TIFFTAG_SAMPLEFORMAT should be set to SAMPLEFORMAT_UINT.

Why is this tag missing? Is there a simple method to have OpenCV generate this tag as well? I'd like to avoid rewriting this convenient one-liner with a lot of libtiff code, just to get a standard tag.

edit retag flag offensive close merge delete


please remember, that opencv is a computer-vision library, not photoshop.

berak gravatar imageberak ( 2018-06-30 00:20:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-29 21:44:37 -0500

hellloooooo gravatar image

updated 2018-06-29 21:45:12 -0500

Taking a quick look at the imgcodes module code, looks like it only writes out the TIFFTAG_SAMPLEFORMAT for 32-bit floating point sample types. Otherwise it's not set, in which case an external Tiff reader implementation would interpret the sample format as value 1 (sample format = unsigned number) if it follows the standard.

If you know some C++ and don't mind hacking the source a little, you can probably add a couple lines in this function in the source: to get it to write out the TIFFTAG_SAMPLEFORMAT based on the data type that's being used.

However I suggest creating a feature request on OpenCV's Github ( if you believe this is a standard tag and should be standard.

As a sidenote, isn't TIFFTAG_SAMPLEFORMAT is an extension tag and not a baseline one? I'm not a Tiff genius but it seems like this tag should be standard, although it's not a baseline Tiff tag.

edit flag offensive delete link more


Aha. Thank you, that's the key right there: the TIFF spec specifies a default, so OpenCV is intelligently omitting SampleFormat. In my case, the consuming application is in error by requiring the tag.

Looks like floating-point support was added to the TIFF standard late (TIFF 6.0), so the tag should continue to be an extension, and OpenCV's doing it right.

jonathan gravatar imagejonathan ( 2018-07-02 14:30:19 -0500 )edit

Question Tools

1 follower


Asked: 2018-06-29 20:42:57 -0500

Seen: 772 times

Last updated: Jun 29 '18