0
votes

I'm tryng to get some statics from some images, and when I tryed to perform histogram equalization I get confused.

Because I tryed this:

img = io.imread(file);
img = exposure.equalize_hist(img);

And I get the warning warn("This might be a color image. The histogram will be "

Then I tryed to perform the equalization in each channel like this:

img = io.imread(file);
#img = exposure.equalize_hist(img);    
height, width = len(img), len(img[0]);
r1 = [];
g1 = [];
b1 = [];
for i in range(height):
    for j in range(width): 
        pixel = img[i, j];
        r1.append(pixel[0]);
        g1.append(pixel[1]);
        b1.append(pixel[2]);    
r = exposure.equalize_hist(r1);        
g = exposure.equalize_hist(g1);
b = exposure.equalize_hist(b1);

And I get the error

AttributeError: 'list' object has no attribute 'shape'

So how should I do histogram equalization in an image with color, and if I wanna do it in an image in HSV, or CIELAB, is it the same way?! histogram equalization

1

1 Answers

2
votes

To equalize each channel separately:

from skimage import io, exposure


img = io.imread(img_path)

for channel in range(img.shape[2]):  # equalizing each channel
    img[:, :, channel] = exposure.equalize_hist(img[:, :, channel])

That's because img[:, :, channel] already gives you the 2d image array supported by equalize_hist, so that you don't need to create three lists (which may be considerably inefficient, by the way). The code supposes that you do have a image (3d array) with channels on the last dimension (which is the case if you load it with skimage.io.imread).

Also, it should work the same with RGB, HSV of Lab (skimage conversions will keep channels on the last dimension). For example img = color.rgb2hsv(img) or img = color.rgb2lab(img).

If you load a grey-scale image (already a 2d array), then your commented line should work (you could handle both cases with a simple if condition).

Just something else: you can drop the semicolons.