Search code examples
pythonimageimage-processingscikit-image

How to save a tif image with skimage.exposure.equalize_adapthist()


I am working on a set of microscopic images, and i am trying to find the right threshold for them. I use these lines of codes:

from skimage import io, img_as_float
from skimage.restoration import denoise_nl_means, estimate_sigma
from skimage import exposur
from skimage.io import imsave

img = img_as_float(io.imread(image))
sigma_est = np.mean(estimate_sigma(img, multichannel = True))
patch_kw = dict(patch_size = 5, patch_distance = 6, multichannel = True)
denoise_img = denoise_nl_means(img, h = 15 * sigma_est, fast_mode = True,
              ** patch_kw)
eq_img = exposure.equalize_adapthist(denoise_img)
imsave("Contrast_DAPI.tif", eq_img)

when i run this code, i get a file that is four times bigger than my original image in size, and it can not be open by anything including Python. I can only save it in case if i change the format from tif to png. But, then i get this warning message: "Lossy conversion from float64 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning."

Can someone help, please? How can i save a tif file in this condition?

Thanks


Solution

  • The issue is that you are saving the image as a float64 datatype. For more on data types, read "Image data types and what they mean" from the scikit-image documentation. The tifffile library will happily save float64, but many tiff readers won't read them.

    Depending on your downstream application, you can use skimage.util.img_as_ubyte or skimage.util.img_as_uint to convert to an 8-bit or 16-bit integer array, then save in the same way you were doing it. Downstream libraries will be happier and you almost certainly don't need the full precision of the 64-bit float — that is a format useful to chain together many operations without losing precision, but it's not necessary if you are going to compute a threshold in the next step.