You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Automatic inversion of binary image stored as TIFFILE. #1940
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
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.
@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.
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.
@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.
@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.
Activity
soupault commentedon Feb 11, 2016
@cgohlke hi! Any thoughts on the issue?
cgohlke commentedon Feb 11, 2016
Looks like
io.imshowignores the min-is-white photometric interpretation tag. There is no automatic inversion.jni commentedon Feb 11, 2016
@cgohlke Is this an imshow or imread problem? How does imread encode this tag in a numpy array?
cgohlke commentedon Feb 11, 2016
imreadcorrectly 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 andimshowis not aware of color spaces either AFAIK. I think it is up to the application developer to handle color spaces and other metadata.jni commentedon Feb 11, 2016
@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.
jmetz commentedon Feb 17, 2016
I'd recommend using the
matplotlibplugin, and passing an inverted colormap, e.g.cmap = "binary"The updated line above would then read:
JDWarner commentedon Feb 17, 2016
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
Truewe'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
This of course assumes we can read this metadata and catch the case.
jni commentedon Feb 18, 2016
@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:
metadatais a dictionary.)Then we can do:
io.imshow(..., metadata=None)
and use the metadata to determine when to invert the colormap used.
jmetz commentedon Feb 19, 2016
Perhaps this should be relabelled an enhancement, as
imreadis technically performing as expected i.e. without any regard for metadata at present.soupault commentedon Feb 19, 2016
@jmetz I think it should be classified as a
bugfrom theskimageperspective.This way or another, I find this label more "catchy" and suitable for the purpose of prioritizing issues for contributors.
jni commentedon Feb 19, 2016
@soupault priority and type are two different things. I agree with @jmetz that it should be relabelled as an enhancement.