Skip to content

Automatic inversion of binary image stored as TIFFILE.  #1940

Open

Description

Summary of Issue : A binary image stored as an uncompressed TIFF file is automatically inverted upon invoking skimage.io.imread() The issue immediately evaporates when the TIFF file is converted to a JPEG File.

Experiment - 1 (With a Binary image stored as a TIFF File).

NOTE : The file has been taken from an independent location and has not been read or re-written. The issue is the same across any other TIFF image I have tried.

from skimage import io
import matplotlib.pyplot as plt

img = io.imread('mask.tif')
io.imshow(img)
plt.show()

Experiment - 2 (With the same Binary image converted to JPEG).

from skimage import io
import matplotlib.pyplot as plt

img = io.imread('mask.jpg')
io.imshow(img)
plt.show()

NOTE : The conversion was done by convert img.tif img.jpg

To reproduce the issue :

Activity

  1. soupault commented on Feb 11, 2016

    @soupault
    Member

    @cgohlke hi! Any thoughts on the issue?

  2. cgohlke commented on Feb 11, 2016

    @cgohlke
    Contributor

    Looks like io.imshow ignores the min-is-white photometric interpretation tag. There is no automatic inversion.

  3. jni commented on Feb 11, 2016

    @jni
    Member

    @cgohlke Is this an imshow or imread problem? How does imread encode this tag in a numpy array?

  4. cgohlke commented on Feb 11, 2016

    @cgohlke
    Contributor

    imread correctly reads the data stored in the file. It does not invert or convert the data. The photometric interpretation tag defines the color space of the data. In this case False values are white and True values black. Metadata stored in image files are not passed around with numpy arrays and imshow is not aware of color spaces either AFAIK. I think it is up to the application developer to handle color spaces and other metadata.

  5. jni commented on Feb 11, 2016

    @jni
    Member

    @cgohlke I don't know enough about the TIFF metadata format but it seems to me that the least-surprising behaviour in this case would be to invert it...? But I guess that does alter the meaning of the image.

    Another option would be to provide an easy way to get metadata, perhaps as a dictionary. Does tifffile.py provide this option?

    In that case, one could invert the colormap used when calling imshow based on the metadata.

  6. jmetz commented on Feb 17, 2016

    @jmetz
    Contributor

    I'd recommend using the matplotlib plugin, and passing an inverted colormap, e.g. cmap = "binary"

    The updated line above would then read:

    io.imshow(img, plugin="matplotlib", cmap="binary")
    
  7. JDWarner commented on Feb 17, 2016

    @JDWarner
    Contributor

    I have to agree with @jni, the most expected behavior here would be to invert the array. As a user I would find any other approach extremely unintuitive. Inverting the array for display is fine for inspection, but when users assume the white areas are True we'll get into trouble fast as they do follow-up computations.

    My favored approach would be to catch this case, invert the input, and warn the user about what is going on. Something like

    "A binary TIFF image was loaded with an inverted colormap.
    The values in this image were inverted on load, so its appearance matches
    the expected conventions (white is True, black is False)."
    

    This of course assumes we can read this metadata and catch the case.

  8. jni commented on Feb 18, 2016

    @jni
    Member

    @JDWarner I am quite agnostic about this, actually, since you do alter the meaning. Say I imread a binary image called 'true-where-the-black-blobs-are.tif', I probably don't want the result to be False where the black blobs are.

    We've mentioned metadata before and how we don't want to encumber skimage with it, but I think metadata is in fact the solution here.

    Some options:

    • imread_metadata: return metadata associated with an image file
    • imread_with_metadata: return image and metadata as a tuple. (metadata is a dictionary.)
    • imread(return_metadata=False): return an (image, metadata_dict) tuple when return_metadata is True.

    Then we can do:
    io.imshow(..., metadata=None)

    and use the metadata to determine when to invert the colormap used.

  9. jmetz commented on Feb 19, 2016

    @jmetz
    Contributor

    Perhaps this should be relabelled an enhancement, as imread is technically performing as expected i.e. without any regard for metadata at present.

  10. soupault commented on Feb 19, 2016

    @soupault
    Member

    @jmetz I think it should be classified as a bug from the skimage perspective.
    This way or another, I find this label more "catchy" and suitable for the purpose of prioritizing issues for contributors.

  11. jni commented on Feb 19, 2016

    @jni
    Member

    @soupault priority and type are two different things. I agree with @jmetz that it should be relabelled as an enhancement.

  12. added
    ⏩ type: Enhancement
    and removed on Feb 20, 2016
  13. locked and limited conversation to collaborators on Oct 18, 2021
  14. unlocked this conversation on Apr 6, 2022
  15. reopened this on Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ⏩ type: Enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions