0

While performing the Contrast Limited Adaptive Histogram Equalization, I got the following warning message. How to avoid it and what does it indicate?

from skimage import exposure
img_adapteq = exposure.equalize_adapthist(image_gray, clip_limit=0.03)

C:\Users\ugwz\AppData\Local\Continuum\anaconda3\lib\site-packages\skimage\util\dtype.py:135: UserWarning: Possible precision loss when converting from float64 to uint16
  .format(dtypeobj_in, dtypeobj_out))
user288609
  • 12,465
  • 26
  • 85
  • 127
  • As it says, you possibly lost some precision when you applied CLAHE to a `float64` image that you didn't share with us. What's to say? Don't expect the results of applying an operation to a 64-bit float image to fit in a 16-bit image. You can't store 1.8E308 in a space that can only hold up to 65,536. – Mark Setchell Oct 04 '19 at 20:35
  • Hi Mark, thanks, but how can I know whether the image is float64 or not. It is just a jpg file, and I read it this way, image = imread("C:\\test8.jpg") image_gray = rgb2gray(image) – user288609 Oct 04 '19 at 20:39
  • A JPG is normally inherently `uint8` in each channel. – Mark Setchell Oct 04 '19 at 20:47
  • Try `print(image_gray.dtype)` and `print(image.dtype)` to check the actual types involved. – Mark Setchell Oct 05 '19 at 07:44

1 Answers1

1

rgb2gray converts your image to float because it is computing relative luminance according to the formula on this page. Note that, due to the conventions of the Scientific Python ecosystem, it also rescales the values to be in [0, 1], so .astype(np.uint16) will not do what you want. Instead, use skimage.util.img_as_{ubyte,uint}, as detailed in the scikit-image documentation on data types:

from skimage import color, util, exposure

image = io.imread(<your-filename>)
image_gray = color.rgb2gray(image)
image16 = util.img_as_uint(image_gray)
img_adapteq = exposure.equalize_adapthist(image_gray, clip_limit=0.03)

Unfortunately, with version 0.15, you'll still see the warning, but it's been removed in version 0.16 and higher, which should be out in the next few days.

Juan
  • 5,433
  • 21
  • 23
  • Thank you for drawing that document to my attention. I will delete any of my comments above which are incorrect and may mislead others. – Mark Setchell Oct 05 '19 at 07:42