您无需费心安装编译器和 OpenCV 以及编写/编译 Python/C++ 代码来获得图像的平均亮度。您可以使用 ImageMagick,它安装在大多数 Linux 发行版上,也适用于 macOS 和 Windows。
基本上,您可以在 HSL 色彩空间或 Lab 色彩空间中查看“亮度”。
我们先来看看HSL。
# Resize stone down to one average pixel, convert to HSL colourspace and print
convert stone1.jpg -resize 1x1 -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (4228.92,19250.9,52587.2) #104BCD hsl(23.2305,29.375%,80.2429%)
所以 stone1.jpg 在 HSL 中的亮度为 80.24%。让我们试试stone2.jpg:
convert stone2.jpg -resize 1x1 -colorspace HSL txt:
# ImageMagick pixel enumeration: 1,1,65535,hsl
0,0: (7387.85,27252.5,57243) #1D6ADF hsl(40.5833,41.5846%,87.3472%)
所以,stone2.jpg 更轻,为 87.35%
让我们简化输出以仅显示亮度:
convert stone1.jpg -colorspace HSL -format "%[fx:int(100*mean.b)]" info:
80
和stone2.jpg:
convert stone2.jpg -colorspace HSL -format "%[fx:int(100*mean.b)]" info:
87
现在让我们看看 Lab 色彩空间。
convert stone1.jpg -resize 1x1 -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (53895.2,1140.43,2057.36) #D20408 cielab(82.2388%,1.74018%,3.13933%)
所以,stone1.jpg 的 Lab 亮度为 82.24%,我们来看看 stone2.jpg:
convert stone2.jpg -resize 1x1 -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (59395,-21.0391,2545.27) #E7000A cielab(90.6309%,-0.0321036%,3.88383%)
所以,stone2.jpg 的亮度为 90.6%。
如果我们想要更简单的形式怎么办?
convert stone1.jpg -colorspace Lab -format "%[fx:int(100*mean.r)]" info:
82
百分比是多少?它们是白色的百分比,因此纯白色为 100%,纯黑色为 0%。快速测试...
convert xc:black -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (0,-0.5,-0.5) #000000000000 cielab(0%,-0.000762951%,-0.000762951%)
convert xc:white -colorspace Lab txt:
# ImageMagick pixel enumeration: 1,1,65535,cielab
0,0: (65535,0.125,-1.69336) #FFFF00000000 cielab(100%,0.000190738%,-0.0025839%)
如果您有一个完整的目录,里面装满了石头样本,并且想要每个样本的 HSL 亮度值,该怎么办?
convert stone* -colorspace HSL -format "%f:%[fx:int(100*mean.b)]\n" info:
stone1.jpg:80
stone2.jpg:87
stone3.jpg:75
stone4.jpg:92