Skip to main content
 首页 » 编程设计

python之无法均衡图像

2024年10月01日7xing901022

我正在使用 Python 中的库 skimage 来增加图像的对比度。 我的图像在名为 X_train 的列表中,其图像形状为:(32x32x3)。

首先我将它转换为 [0, 1],然后从 RGB 转换为 HSV,然后我使用库中的方法:

X_train = X_train/256 
X_train_hsv = matplotlib.colors.rgb_to_hsv(X_train) 
X_train_eq = skimage.exposure.equalize_adapthist(X_train_hsv, kernel_size=None, 
                                                 clip_limit=0.01, nbins=256, ) 

问题是我得到了这个错误:

/home/carnd/anaconda3/envs/carnd-term1/lib/python3.5/site-packages/skimage/exposure/_adapthist.py in interpolate(image, xslice, yslice, mapLU, mapRU, mapLB, mapRB, lut) 
 
        333                  int(xslice[0]):int(xslice[-1] + 1)] 
        334     im_slice = lut[view] 
    --> 335     new = ((y_inv_coef * (x_inv_coef * mapLU[im_slice] 
        336                           + x_coef * mapRU[im_slice]) 
        337             + y_coef * (x_inv_coef * mapLB[im_slice] 
 
ValueError: operands could not be broadcast together with shapes (2175,2) (2175,2,32,3) 

有人知道我的错误是什么吗?

请您参考如下方法:

根据 scikit-image documentation ,您不需要将图像重新缩放为 0..1 并将它们从 RGB 转换为 HSV:

Notes

  • For color images, the following steps are performed:

    • The image is converted to HSV color space
    • The CLAHE algorithm is run on the V (Value) channel
    • The image is converted back to RGB space and returned
  • For RGBA images, the original alpha channel is removed.

因此,当您使用关键字参数 kernel_size、clip_limit 和 nbins 的默认值时,您可以简单地编写:

X_train_eq = skimage.exposure.equalize_adapthist(X_train)