【问题标题】:sort images based on their color根据颜色对图像进行排序
【发布时间】:2017-10-10 12:24:05
【问题描述】:

我有一些石头的图像。石头的颜色是奶油色的,我想根据颜色的差异对图像进行分类。我想给最轻的石头等级 0 和最黑的石头等级 10,并在这两者之间进行分类。石头非常相似,但它们的颜色差异是肉眼可以察觉的。
我知道如果石头都是蓝色的,例如我可以根据 RGB 颜色的 R 部分对它们进行分类。但是奶油色呢?

【问题讨论】:

  • 你能上传一些示例图片吗?您是否成功地从背景中分割出石头?否则,由于背景噪声,设计一种基于颜色进行排序的算法将无法正常工作。
  • @ZdaR 我从石头上传图片

标签: opencv image-processing


【解决方案1】:

您无需费心安装编译器和 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.jpgLab 亮度为 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

【讨论】:

  • 您知道-format 选项中使用的语法的文档在哪里。喜欢这个:%[fx:int(100*mean.b)。我在野外也见过这样的东西:%[fx:floor(quantumrange*r)]。这似乎没有被覆盖here
  • @BryanK 我想你的意思是这个imagemagick.org/script/fx.php
【解决方案2】:

我想根据图像的颜色差异对图像进行分类。我想给最轻的石头等级 0 和最暗的石头等级 10,并在这两者之间对其他人进行分类。石头非常相似,但它们的颜色差异是肉眼可以察觉的。

如果您想将它们从浅到深排序,并考虑到色调,那么 RGB 色彩空间不太适合。你应该看看Lab colour space 或者Hue Saturation Lightness (HSL) 色彩空间。

通过将图像转换为这些颜色空间之一,然后取平均值,您可以将其分类为亮度,以及一个或两个颜色/色调轴。 (这是假设图像内容大多是同质的。)

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 2014-11-07
    • 2014-05-15
    • 2013-12-11
    • 2017-04-05
    • 2012-01-05
    相关资源
    最近更新 更多