tensorflow / io

Dataset, streaming, and file system extensions maintained by TensorFlow SIG-IO
Apache License 2.0
699 stars 281 forks source link

support for 16-bit tiff files #1337

Open rikuturkki opened 3 years ago

rikuturkki commented 3 years ago

Hi, would be great to have support also for 16-bit tiff files.

yongtang commented 3 years ago

@rikuturkki Do you have a sample 16-bit tiff file to share? I might be able to take a look if there is a sample available.

rikuturkki commented 3 years ago

@yongtang Great – here are few examples: https://we.tl/t-GubVjWm2QP

Let me know if you are not able to access them.

yongtang commented 3 years ago

@rikuturkki PR #1349 adds the 16 bit support. Can you give it a try?

rikuturkki commented 3 years ago

Hi @yongtang, I tested the tfsigio/tfio:nightly docker.

tfio.experimental.image.decode_tiff returns an array of shape (520, 696, 4), however, the real shape should be (520, 696) instead.

yongtang commented 3 years ago

@rikuturkki The last dim of 4 is the channel so the shape is 520(h)x690(w)x4(c)

rikuturkki commented 3 years ago

@yongtang the image I am using for testing has only 1 channel. I wonder if the PR you made is included in the nightly docker. I was not able to install tfio otherwise.

yongtang commented 3 years ago

@rikuturkki At the moment decode_tiff will always convert the image to RGBA format. Additional changes might be needed to keep original format.

kvignesh1420 commented 3 years ago

@rikuturkki the nightly image has been pushed to the docker hub. Please check.

rikuturkki commented 3 years ago

Thank you @kvignesh1420 and @yongtang.

I tested tfio.experimental.image.decode_tiff with the nightly image. There seem to be some problems with the functionality. Pixel values do not match and the data type is still uint8.

The expected values are shown here:

>>> real_img = image.imread(filename)
>>> real_img
array([[149, 157, 149, ..., 208, 210, 199],
       [153, 152, 159, ..., 180, 173, 180],
       [153, 158, 148, ..., 164, 161, 170],
       ...,
       [213, 225, 214, ..., 496, 468, 482],
       [204, 194, 201, ..., 506, 488, 461],
       [182, 190, 198, ..., 565, 554, 499]], dtype=uint16)

and img = tfio.experimental.image.decode_tiff(tf.io.read_file(filename)) returns following:

TIFFReadDirectory: Warning, Unknown field with tag 317 (0x13d) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33628 (0x835c) encountered.
TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.
TIFFReadDirectory: Warning, Unknown field with tag 317 (0x13d) encountered.
TIFFReadDirectory: Warning, Unknown field with tag 33628 (0x835c) encountered.
TIFFFetchNormalTag: Warning, ASCII value for tag "DateTime" contains null byte in value; value incorrectly truncated during reading due to implementation limitations.
>>> img 
<tf.Tensor: shape=(520, 696, 4), dtype=uint8, numpy=
array([[[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255]],

       ...,

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  1,   1,   1, 255],
        [  1,   1,   1, 255],
        [  1,   1,   1, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  1,   1,   1, 255],
        [  1,   1,   1, 255],
        [  1,   1,   1, 255]],

       [[  0,   0,   0, 255],
        [  0,   0,   0, 255],
        [  0,   0,   0, 255],
        ...,
        [  2,   2,   2, 255],
        [  2,   2,   2, 255],
        [  1,   1,   1, 255]]], dtype=uint8)>
kvignesh1420 commented 3 years ago

@rikuturkki I think this is due to the functionality as mentioned by @yongtang above https://github.com/tensorflow/io/issues/1337#issuecomment-814263334

rikuturkki commented 3 years ago

Thanks @kvignesh1420. Does that also explain the wrong type? What do you think @yongtang?

yongtang commented 3 years ago

@rikuturkki Yes currently tiff convert to RGBA 8bit by default. Keep the original format is possible though additional changes will be needed.