繁体   English   中英

skimage 在每个通道的 LAB 色彩空间中使用什么范围?

[英]What range does skimage use in LAB color space for each channel?

使用skimage.color.rgb2lab将图像从 RGB 转换为 LAB 时,我真的找不到文档,说明 skimage 中三个通道中的每一个都可以具有哪个值范围。 我尝试使用下面的代码建议L通道为[0.0, 9341.57] [-6952.27, 7924.33]A通道为[-8700.71, 7621.43]B通道为[-8700.71, 7621.43]
但在我看来,这看起来既不像描述中的典型 LBA 值( [0, 100][-170, 100][-100, 150] ),数字的比率也不相似,而且这些通常看起来很“奇怪”的范围似乎根本没有标准化。

那么,如果通过skimage转换,LBA 图像的每个通道可以具有哪些值?

(和/或我在下面的错误在哪里尝试确定它们?)

# WARN: can take a while to calculate
import numpy as np
from skimage.color import rgb2lab
import multiprocessing as mp

colors = [[r,g,b] for r in range(256) for g in range(256) for b in range(256)]

imgs = np.zeros((16777216,1,1,3))
for i, color in enumerate(colors):
    imgs[i,:,:] = color


labs = np.zeros((16777216,1,1,3))
pool = mp.Pool(mp.cpu_count()-1)
try:
    labs = pool.map(rgb2lab, imgs)
except KeyboardInterrupt:
    # without catching this here we will never be able to manually stop running in a sane way
    pool.terminate()
pool.close()
pool.join()

print(np.min(labs[:,:,:,0]), np.max(labs[:,:,:,0]), np.min(labs[:,:,:,1]), np.max(labs[:,:,:,1]), np.min(labs[:,:,:,2]), np.max(labs[:,:,:,2]))

# 0.0 9341.570221995466 -6952.27373084052 7924.333617630548 -8700.709143439595 7621.42813486568

好吧,您的第一个错误是使用纯 Python 循环而不是 NumPy 表达式,但那是题外话。 =) 请参阅下文,了解您要实现的目标的更有效版本。

第二个错误更微妙,可能是 scikit-image 新手最常见的错误。 从 0.16 版本开始,scikit-image 对不同的数据类型使用隐式数据范围,在本页中有详细说明。 np.zeros默认为浮点数据类型,因此您上面的输入数组的浮点数范围为 0-255,这比预期的 0-1 范围大得多,因此您最终在实验室空间中获得了同样更大的范围.

您可以通过将imgs声明更改为imgs = np.zeros((16777216, 1, 1, 3), dtype=np.uint8)来使用numpy.uint8值而不是 float 来查看 Lab 值的范围。 但是,下面是“NumPy 方式”的完整代码:

In [2]: import numpy as np
In [3]: colors = np.mgrid[0:256, 0:256, 0:256].astype(np.uint8)
In [4]: colors.shape
Out[4]: (3, 256, 256, 256)

In [6]: all_rgb = np.transpose(colors)
In [7]: from skimage import color
In [8]: all_lab = color.rgb2lab(all_rgb)
In [9]: np.max(all_lab, axis=(0, 1, 2))
Out[9]: array([100.        ,  98.23305386,  94.47812228])

In [10]: np.min(all_lab, axis=(0, 1, 2))
Out[10]: array([   0.        ,  -86.18302974, -107.85730021])

为了说明数据范围问题,您可以看到在 0-1 中使用浮点输入会得到相同的结果:

In [12]: all_lab2 = color.rgb2lab(all_rgb / 255)
In [13]: np.max(all_lab2, axis=(0, 1, 2))
Out[13]: array([100.        ,  98.23305386,  94.47812228])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM