直方图均衡化skimage

histogram equalization skimage

我试图从一些图像中获取一些静态信息,当我尝试执行直方图均衡化时,我感到困惑。

因为我试过这个:

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

然后我收到警告 warn("This might be a color image. The histogram will be "

然后我尝试像这样在每个通道中执行均衡:

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);

我收到错误

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

那么我应该如何在有颜色的图像中进行直方图均衡,如果我想在 HSV 或 CIELAB 中的图像中进行,是不是同样的方法?! histogram equalization

要分别对每个通道进行均衡:

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])

那是因为 img[:, :, channel] 已经为您提供了 equalize_hist 支持的二维图像数组,因此您不需要创建三个列表(顺便说一句,这可能效率很低)。该代码假设您确实有一个图像(3d 数组),在最后一个维度上带有通道(如果您使用 skimage.io.imread 加载它就是这种情况)。

此外,它应该与 Lab 的 RGB、HSV 一样工作(skimage 转换将使通道保持在最后一个维度上)。例如 img = color.rgb2hsv(img)img = color.rgb2lab(img).

如果您加载灰度图像(已经是二维数组),那么您的注释行应该有效(您可以使用简单的 if 条件处理这两种情况)。

还有一点:你可以去掉分号。