Skip to content

exposure.equalize_hist doesn't output the same type as the input #2677

@TheShadow29

Description

@TheShadow29

I am not completely sure if it should be a feature request or a bug.
Currently exposure.equalize_hist given an image of input of type uint8 outputs an image of type float64. The proposal is to give the output in uint8 format itself.
Current implementation:

img1 = io.imread(path_to_file)
img11 = skimage.exposure.equalize_hist(img1)
print('Original Image is of dtype ', img1.dtype)
print('Histogram Equalized Image is of dtype', img11.dtype)

Output:

('Original Image is of dtype ', dtype('uint8'))
('Histogram Equalized Image is of dtype', dtype('float64'))

#1192 mentions this, but the issue is not really addressed.
To be honest I am not even sure why the output image is of type 'float64'. At no point of time should image of type uint8 should be changed to float64, i.e. there should be no case where we should be working with floats, we should be in integer domain itself. Here is a nice text describing histogram equalization https://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf

Histogram equalization would simply be a transformation curve, mapping one intensity to another corresponding intensity (the formulae given in the mentioned link) and it should be all in the integer domain when the input is of type uint8.

Also as far as I know, MATLAB also does the same thing, that is given an input of uint8 returns output in uint8 itself. Link for the same https://www.mathworks.com/help/images/ref/histeq.html

In short, the current implementation of the equalize_hist is doing something similar but will give a different result from what a normal histogram equalization would return at least for an uint8 image. Also I think it is more sensible to return image of the same dtype.
Here is a matlab vs skimage comparison. Code for matlab

img = imread('rice.png');
im1 = histeq(img);
imwrite(img, 'rice.png');
imwrite(im1, 'rice_eq.png');

Now for the skimage code.

%matplotlib
import skimage
from skimage import io
import matplotlib.pyplot as plt
io.use_plugin('matplotlib')
img1 = io.imread('rice.png')
img11 = skimage.exposure.equalize_hist(img1)
img12 = skimage.img_as_ubyte(img11)
img2 = io.imread('rice_eq.png')
print('Skimage hist eq', img12);
print('Matlab hist eq', img2);
plt.figure();
plt.subplot(1,3,1); plt.title('Skimage hist eq'); plt.imshow(img12, cmap='gray');
plt.subplot(1,3,2); plt.title('Matlab hist eq'); plt.imshow(img2, cmap='gray');
plt.subplot(1,3,3); plt.title('Difference image'); plt.imshow(abs(img12 - img2), cmap='gray');

Here are the two matrices
git_post_skimage_2

Here is the output image
skimage_git_post

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions