图像中颜色的分类器

Posted

技术标签:

【中文标题】图像中颜色的分类器【英文标题】:Classifier for colors in image 【发布时间】:2018-08-08 01:00:31 【问题描述】:

我有这张图片。

我想对所有像素进行分类。该图像中像素的颜色如下:

    蓝色:水 红色:屋顶 白:路 黄色:浅 浅绿色:草 深绿色:树 黑色:背景

如何对所有像素进行分类。例如,如果 2'column 和 1'row 的像素是蓝色的,则将 (1,2) 分类为 water 类,依此类推。

【问题讨论】:

不确定您是否知道您的图像有 8,002 种颜色?期望最终得到一个仅包含强度 1、2、3、4 的非常暗的灰度图像?黄色像素呢? 我编辑问题,图像是 RGB 颜色 图像中是否只有 7 个不同的 RGB 值?还是您想将不同的值“分组”,这意味着您可能希望将不同的蓝色阴影(具有不同的 RGB 值)组合在一起? 我想对不同的值进行分组,我有不同深浅的蓝色、红色、白色和其他。 我认为您需要将 RGB 转换为 HSL(或类似的,实际上您只需要 H)。所以在你有了色调之后,你可以定义间隔 【参考方案1】:

我可以向您展示如何做到这一点以及您将获得什么结果,并希望您可以将其适应 Python - 如果您愿意的话。我选择的武器是 ImageMagick,它安装在大多数 Linux 发行版上,并且在 macOS 和 Windows 上免费提供。


第 1 步

首先,我使用 “color dropper” 工具(在 Mac 上称为 “Digital Color Meter”)并查看 7 个区域中的每一个您已识别的颜色并获取它们的 RGB 坐标,并通过在终端中键入以下内容来制作这些值的简单“查找表”

# First make a LUT of our 7 colours
convert xc:"rgb(59,79,162)"   \
        xc:"rgb(230,30,35)"   \
        xc:"rgb(240,240,240)" \
        xc:"rgb(230,230,97)"  \
        xc:"rgb(103,180,65)"  \
        xc:"rgb(47,140,87)"   \
        xc:black +append LUT.png

那个文件 (LUT.png) 看起来像这样 - 只是更小 - 我在下面放大了它,所以你可以看到它。实际上,它只有 7x1 像素:


第 2 步

现在我将查找表应用到图像上,而不进行抖动,以便图像中的每种颜色都被强制为表中最接近的颜色:

convert image.png +dither -map LUT.png result.png

图像现在真正只有我们选择的 7 种颜色,而不是原始图像中的 8,002 种颜色!


第 3 步

现在,我们检查图像并对其进行实际分类,将所有蓝色像素替换为 1,将所有红色像素替换为 2,将所有白色像素替换为 3,依此类推:

convert result.png            \
        -fill "gray(1)" -opaque "rgb(59,79,162)"   \
        -fill "gray(2)" -opaque "rgb(230,30,35)"   \
        -fill "gray(3)" -opaque "rgb(240,240,240)" \
        -fill "gray(4)" -opaque "rgb(230,230,97)"  \
        -fill "gray(5)" -opaque "rgb(103,180,65)"  \
        -fill "gray(6)" -opaque "rgb(47,140,87)"   \
        -fill "gray(7)" -opaque black final.png

这给了我们这个非常暗的图像,因为在 0-255 的范围内所有像素都是 7 或以下:

所以我们使用标准化将灰色扩展到整个范围:

convert final.png -normalize final.png


我猜接下来要确定每种封面类型的百分比。我们先来看看水。如果我们把所有是水的东西都变成白色像素,把所有不是水的东西变成黑色像素,我们可以简单地通过查看图像均值来计算水覆盖的百分比:

convert final.png -fill white -opaque "rgb(1,1,1)" -fill black +opaque white -format "%[fx:int(mean*100)]" info:
12

所以有 12% 的水覆盖率。要获取实际图像,请将上面的计算替换为输出文件名:

convert final.png -fill white -opaque "rgb(1,1,1)" -fill black +opaque white water.png

接下来让我们尝试树木...同样的过程,将所有树木变为白色,将所有非树木变为黑色:

convert final.png -fill white -opaque "rgb(6,6,6)" -fill black +opaque white -format "%[fx:int(mean*100)]" info:
26

因此,26% 的树木覆盖率。让我们看看它们:

convert final.png -fill white -opaque "rgb(6,6,6)" -fill black +opaque white trees.png


Python

那么,在 Python 中会发生什么?

第 1 步意味着您为 7 种颜色中的每一种分配一个列表或数组(或任何 Python 调用它们)。列表中的每个条目都有一个红色、绿色和蓝色组件。

第 2 步。您遍历图像中的每个像素(按行和列),获取其红色、绿色和蓝色分量。然后,您计算从当前像素到 7 种 LUT 颜色中的每一种颜色的颜色距离,并取结果最小的颜色,即最近的距离。你用

计算距离
(thispixel.Red-LUT[n].Red)^2 + (thispixel.Green-LUT[n].Green)^2 + (thispixel.Blue-LUT[n].Blue)^2 

n 将遍历 LUT 的 7 个索引。

第 3 步。将输出像素设置为 LUT 中最接近的颜色的索引。

【讨论】:

【参考方案2】:

马克有一个很好的答案。这是从他的 6 色图像开始的稍微不同的方法。使用 Imagemagick 的连接组件标记来使用 id 号对每个隔离区域进行编码。 (使用-connected-components,您甚至可以将小的孤立区域合并到它们周围的颜色中。但我不会在这里这样做)。

convert V5s4L.png \
-define connected-components:verbose=true \
-connected-components 4 \
result1.png

Objects (id: bounding-box centroid area mean-color):
  1: 121x114+0+0 61.3,58.8 6759 srgb(0,0,0)
  0: 62x37+0+0 27.0,17.4 1670 srgb(59,79,162)
  71: 82x40+4+36 52.0,49.1 893 srgb(47,140,87)
  30: 67x79+47+28 79.4,65.7 800 srgb(47,140,87)
  123: 50x61+68+53 96.4,92.1 567 srgb(47,140,87)
  122: 47x61+60+53 81.8,84.8 519 srgb(47,140,87)
  8: 25x37+96+0 110.6,12.5 222 srgb(47,140,87)
  3: 19x23+49+0 59.2,6.8 213 srgb(103,180,65)
  119: 13x21+41+52 47.0,62.4 206 srgb(230,30,35)
  126: 8x34+113+63 116.9,80.6 181 srgb(47,140,87)
  130: 17x26+33+84 42.1,93.0 178 srgb(47,140,87)
  129: 29x33+64+81 79.3,97.8 170 srgb(0,0,0)
  125: 10x16+0+58 4.7,65.8 150 srgb(230,30,35)
  141: 17x12+99+91 106.7,96.7 143 srgb(230,30,35)
  124: 11x19+20+57 23.8,67.4 141 srgb(230,30,35)
  131: 16x13+23+85 29.2,92.3 119 srgb(230,30,35)
  145: 18x10+16+101 23.3,105.6 108 srgb(230,30,35)
  132: 9x12+0+88 4.4,94.3 70 srgb(230,30,35)
  87: 14x13+107+38 113.2,43.1 61 srgb(240,240,240)
  6: 6x17+68+0 70.0,6.3 48 srgb(103,180,65)
  35: 22x4+60+31 70.5,33.2 44 srgb(230,230,97)
  28: 5x11+116+19 117.8,23.6 40 srgb(47,140,87)
  118: 3x14+13+52 14.0,58.5 38 srgb(47,140,87)
  48: 16x5+29+33 37.7,35.8 36 srgb(230,230,97)
  128: 4x10+117+76 118.9,80.6 28 srgb(0,0,0)
  144: 7x6+0+100 2.5,102.4 28 srgb(47,140,87)
  149: 6x5+47+109 49.7,111.5 24 srgb(47,140,87)
  156: 11x3+89+111 93.7,112.4 24 srgb(230,30,35)
  64: 17x1+60+35 68.0,35.0 17 srgb(103,180,65)
  13: 1x15+65+6 65.0,13.0 15 srgb(47,140,87)
  44: 15x1+62+32 69.0,32.0 15 srgb(103,180,65)
  38: 6x5+34+32 37.3,34.1 15 srgb(0,0,0)
  16: 2x12+67+7 67.1,12.8 13 srgb(47,140,87)
  154: 4x4+117+110 119.0,112.0 10 srgb(47,140,87)
  66: 4x3+19+36 20.7,36.8 9 srgb(230,230,97)
  146: 3x4+0+108 0.8,109.9 9 srgb(230,30,35)
  152: 4x4+83+110 84.9,111.9 9 srgb(230,230,97)
  134: 5x4+98+88 100.2,89.7 9 srgb(230,230,97)
  4: 2x7+67+0 67.9,3.1 8 srgb(47,140,87)
  110: 4x2+0+48 1.7,48.6 7 srgb(47,140,87)
  29: 3x3+62+21 63.2,22.3 6 srgb(47,140,87)
  33: 2x4+0+30 0.6,31.6 5 srgb(47,140,87)
  7: 1x5+74+0 74.0,2.0 5 srgb(47,140,87)
  52: 4x1+17+34 18.5,34.0 4 srgb(0,0,0)
  82: 4x1+33+38 34.5,38.0 4 srgb(103,180,65)
  83: 4x1+37+38 38.5,38.0 4 srgb(47,140,87)
  34: 4x1+77+31 78.5,31.0 4 srgb(103,180,65)
  2: 2x3+48+0 48.2,1.2 4 srgb(47,140,87)
  127: 4x1+0+66 1.5,66.0 4 srgb(0,0,0)
  55: 3x2+29+34 30.2,34.2 4 srgb(103,180,65)
  5: 1x4+69+0 69.0,1.5 4 srgb(0,0,0)
  21: 1x3+55+11 55.0,12.0 3 srgb(47,140,87)
  10: 1x3+51+4 51.0,5.0 3 srgb(47,140,87)
  22: 3x1+56+14 57.0,14.0 3 srgb(47,140,87)
  56: 2x2+44+34 44.7,34.3 3 srgb(47,140,87)
  43: 2x1+60+32 60.5,32.0 2 srgb(47,140,87)
  147: 1x2+87+108 87.0,108.5 2 srgb(103,180,65)
  97: 2x1+107+42 107.5,42.0 2 srgb(47,140,87)
  133: 2x1+100+88 100.5,88.0 2 srgb(103,180,65)
  143: 2x1+98+92 98.5,92.0 2 srgb(103,180,65)
  100: 2x1+116+42 116.5,42.0 2 srgb(103,180,65)
  32: 1x2+0+30 0.0,30.5 2 srgb(103,180,65)
  135: 1x2+103+88 103.0,88.5 2 srgb(103,180,65)
  148: 1x2+88+108 88.0,108.5 2 srgb(47,140,87)
  121: 1x2+120+52 120.0,52.5 2 srgb(240,240,240)
  120: 1x2+119+52 119.0,52.5 2 srgb(47,140,87)
  59: 1x2+18+35 18.0,35.5 2 srgb(47,140,87)
  89: 2x1+20+39 20.5,39.0 2 srgb(103,180,65)
  105: 1x1+107+45 107.0,45.0 1 srgb(47,140,87)
  106: 1x1+108+46 108.0,46.0 1 srgb(59,79,162)
  107: 1x1+114+46 114.0,46.0 1 srgb(103,180,65)
  108: 1x1+112+47 112.0,47.0 1 srgb(47,140,87)
  109: 1x1+115+47 115.0,47.0 1 srgb(103,180,65)
  111: 1x1+113+48 113.0,48.0 1 srgb(103,180,65)
  112: 1x1+116+48 116.0,48.0 1 srgb(103,180,65)
  113: 1x1+114+49 114.0,49.0 1 srgb(103,180,65)
  114: 1x1+117+49 117.0,49.0 1 srgb(47,140,87)
  115: 1x1+115+50 115.0,50.0 1 srgb(103,180,65)
  116: 1x1+116+51 116.0,51.0 1 srgb(47,140,87)
  117: 1x1+120+51 120.0,51.0 1 srgb(103,180,65)
  136: 1x1+99+89 99.0,89.0 1 srgb(103,180,65)
  137: 1x1+98+90 98.0,90.0 1 srgb(103,180,65)
  138: 1x1+102+90 102.0,90.0 1 srgb(103,180,65)
  139: 1x1+97+91 97.0,91.0 1 srgb(103,180,65)
  140: 1x1+100+91 100.0,91.0 1 srgb(103,180,65)
  142: 1x1+97+92 97.0,92.0 1 srgb(47,140,87)
  150: 1x1+86+109 86.0,109.0 1 srgb(47,140,87)
  151: 1x1+85+110 85.0,110.0 1 srgb(103,180,65)
  153: 1x1+87+110 87.0,110.0 1 srgb(47,140,87)
  155: 1x1+84+111 84.0,111.0 1 srgb(103,180,65)
  157: 1x1+83+112 83.0,112.0 1 srgb(103,180,65)
  158: 1x1+82+113 82.0,113.0 1 srgb(47,140,87)
  159: 1x1+86+113 86.0,113.0 1 srgb(103,180,65)
  9: 1x1+50+3 50.0,3.0 1 srgb(47,140,87)
  11: 1x1+66+5 66.0,5.0 1 srgb(47,140,87)
  12: 1x1+73+5 73.0,5.0 1 srgb(47,140,87)
  14: 1x1+72+6 72.0,6.0 1 srgb(47,140,87)
  15: 1x1+52+7 52.0,7.0 1 srgb(47,140,87)
  17: 1x1+71+7 71.0,7.0 1 srgb(47,140,87)
  18: 1x1+70+8 70.0,8.0 1 srgb(47,140,87)
  19: 1x1+53+9 53.0,9.0 1 srgb(47,140,87)
  20: 1x1+54+10 54.0,10.0 1 srgb(47,140,87)
  23: 1x1+59+15 59.0,15.0 1 srgb(47,140,87)
  24: 1x1+60+16 60.0,16.0 1 srgb(47,140,87)
  25: 1x1+69+16 69.0,16.0 1 srgb(47,140,87)
  26: 1x1+61+17 61.0,17.0 1 srgb(47,140,87)
  27: 1x1+62+18 62.0,18.0 1 srgb(47,140,87)
  31: 1x1+0+29 0.0,29.0 1 srgb(47,140,87)
  36: 1x1+82+31 82.0,31.0 1 srgb(59,79,162)
  37: 1x1+28+32 28.0,32.0 1 srgb(47,140,87)
  39: 1x1+40+32 40.0,32.0 1 srgb(47,140,87)
  40: 1x1+41+32 41.0,32.0 1 srgb(103,180,65)
  41: 1x1+42+32 42.0,32.0 1 srgb(47,140,87)
  42: 1x1+43+32 43.0,32.0 1 srgb(0,0,0)
  45: 1x1+82+32 82.0,32.0 1 srgb(103,180,65)
  46: 1x1+28+33 28.0,33.0 1 srgb(0,0,0)
  47: 1x1+29+33 29.0,33.0 1 srgb(47,140,87)
  49: 1x1+41+33 41.0,33.0 1 srgb(240,240,240)
  50: 1x1+43+33 43.0,33.0 1 srgb(47,140,87)
  51: 1x1+81+33 81.0,33.0 1 srgb(103,180,65)
  53: 1x1+21+34 21.0,34.0 1 srgb(47,140,87)
  54: 1x1+28+34 28.0,34.0 1 srgb(47,140,87)
  57: 1x1+77+34 77.0,34.0 1 srgb(103,180,65)
  58: 1x1+79+34 79.0,34.0 1 srgb(47,140,87)
  60: 1x1+19+35 19.0,35.0 1 srgb(59,79,162)
  61: 1x1+20+35 20.0,35.0 1 srgb(47,140,87)
  62: 1x1+28+35 28.0,35.0 1 srgb(103,180,65)
  63: 1x1+39+35 39.0,35.0 1 srgb(103,180,65)
  65: 1x1+16+36 16.0,36.0 1 srgb(47,140,87)
  67: 1x1+28+36 28.0,36.0 1 srgb(47,140,87)
  68: 1x1+33+36 33.0,36.0 1 srgb(103,180,65)
  69: 1x1+38+36 38.0,36.0 1 srgb(103,180,65)
  70: 1x1+45+36 45.0,36.0 1 srgb(103,180,65)
  72: 1x1+19+37 19.0,37.0 1 srgb(47,140,87)
  73: 1x1+29+37 29.0,37.0 1 srgb(47,140,87)
  74: 1x1+43+37 43.0,37.0 1 srgb(47,140,87)
  75: 1x1+115+37 115.0,37.0 1 srgb(47,140,87)
  76: 1x1+116+37 116.0,37.0 1 srgb(230,230,97)
  77: 1x1+0+38 0.0,38.0 1 srgb(47,140,87)
  78: 1x1+22+38 22.0,38.0 1 srgb(103,180,65)
  79: 1x1+30+38 30.0,38.0 1 srgb(47,140,87)
  80: 1x1+31+38 31.0,38.0 1 srgb(103,180,65)
  81: 1x1+32+38 32.0,38.0 1 srgb(47,140,87)
  84: 1x1+41+38 41.0,38.0 1 srgb(103,180,65)
  85: 1x1+42+38 42.0,38.0 1 srgb(47,140,87)
  86: 1x1+114+38 114.0,38.0 1 srgb(47,140,87)
  88: 1x1+117+38 117.0,38.0 1 srgb(230,230,97)
  90: 1x1+113+39 113.0,39.0 1 srgb(47,140,87)
  91: 1x1+118+39 118.0,39.0 1 srgb(230,230,97)
  92: 1x1+119+39 119.0,39.0 1 srgb(47,140,87)
  93: 1x1+112+40 112.0,40.0 1 srgb(47,140,87)
  94: 1x1+119+40 119.0,40.0 1 srgb(230,230,97)
  95: 1x1+110+41 110.0,41.0 1 srgb(103,180,65)
  96: 1x1+111+41 111.0,41.0 1 srgb(230,230,97)
  98: 1x1+109+42 109.0,42.0 1 srgb(103,180,65)
  99: 1x1+115+42 115.0,42.0 1 srgb(230,230,97)
  101: 1x1+120+42 120.0,42.0 1 srgb(103,180,65)
  102: 1x1+114+43 114.0,43.0 1 srgb(47,140,87)
  103: 1x1+118+43 118.0,43.0 1 srgb(47,140,87)
  104: 1x1+120+44 120.0,44.0 1 srgb(47,140,87)

在这种情况下,图像会变暗,因为可能的 65535 中只有大约 160 个 id。因此,为了更好地查看,我们可以将图像拉伸到全动态范围。

convert result1.png -auto-level result2.png

或者我们可以在输出中保留原始颜色:

convert V5s4L.png \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 \
result3.png

Objects (id: bounding-box centroid area mean-color):
  1: 121x114+0+0 61.3,58.8 6759 srgb(0,0,0)
  0: 62x37+0+0 27.0,17.4 1670 srgb(59,79,162)
  71: 82x40+4+36 52.0,49.1 893 srgb(47,140,87)
  30: 67x79+47+28 79.4,65.7 800 srgb(47,140,87)
  123: 50x61+68+53 96.4,92.1 567 srgb(47,140,87)
  122: 47x61+60+53 81.8,84.8 519 srgb(47,140,87)
  8: 25x37+96+0 110.6,12.5 222 srgb(47,140,87)
  3: 19x23+49+0 59.2,6.8 213 srgb(103,180,65)
  119: 13x21+41+52 47.0,62.4 206 srgb(230,30,35)
  126: 8x34+113+63 116.9,80.6 181 srgb(47,140,87)
  130: 17x26+33+84 42.1,93.0 178 srgb(47,140,87)
  129: 29x33+64+81 79.3,97.8 170 srgb(0,0,0)
  125: 10x16+0+58 4.7,65.8 150 srgb(230,30,35)
  141: 17x12+99+91 106.7,96.7 143 srgb(230,30,35)
  124: 11x19+20+57 23.8,67.4 141 srgb(230,30,35)
  131: 16x13+23+85 29.2,92.3 119 srgb(230,30,35)
  145: 18x10+16+101 23.3,105.6 108 srgb(230,30,35)
  132: 9x12+0+88 4.4,94.3 70 srgb(230,30,35)
  87: 14x13+107+38 113.2,43.1 61 srgb(240,240,240)
  6: 6x17+68+0 70.0,6.3 48 srgb(103,180,65)
  35: 22x4+60+31 70.5,33.2 44 srgb(230,230,97)
  28: 5x11+116+19 117.8,23.6 40 srgb(47,140,87)
  118: 3x14+13+52 14.0,58.5 38 srgb(47,140,87)
  48: 16x5+29+33 37.7,35.8 36 srgb(230,230,97)
  128: 4x10+117+76 118.9,80.6 28 srgb(0,0,0)
  144: 7x6+0+100 2.5,102.4 28 srgb(47,140,87)
  149: 6x5+47+109 49.7,111.5 24 srgb(47,140,87)
  156: 11x3+89+111 93.7,112.4 24 srgb(230,30,35)
  64: 17x1+60+35 68.0,35.0 17 srgb(103,180,65)
  13: 1x15+65+6 65.0,13.0 15 srgb(47,140,87)
  44: 15x1+62+32 69.0,32.0 15 srgb(103,180,65)
  38: 6x5+34+32 37.3,34.1 15 srgb(0,0,0)
  16: 2x12+67+7 67.1,12.8 13 srgb(47,140,87)
  154: 4x4+117+110 119.0,112.0 10 srgb(47,140,87)
  66: 4x3+19+36 20.7,36.8 9 srgb(230,230,97)
  146: 3x4+0+108 0.8,109.9 9 srgb(230,30,35)
  152: 4x4+83+110 84.9,111.9 9 srgb(230,230,97)
  134: 5x4+98+88 100.2,89.7 9 srgb(230,230,97)
  4: 2x7+67+0 67.9,3.1 8 srgb(47,140,87)
  110: 4x2+0+48 1.7,48.6 7 srgb(47,140,87)
  29: 3x3+62+21 63.2,22.3 6 srgb(47,140,87)
  33: 2x4+0+30 0.6,31.6 5 srgb(47,140,87)
  7: 1x5+74+0 74.0,2.0 5 srgb(47,140,87)
  52: 4x1+17+34 18.5,34.0 4 srgb(0,0,0)
  82: 4x1+33+38 34.5,38.0 4 srgb(103,180,65)
  83: 4x1+37+38 38.5,38.0 4 srgb(47,140,87)
  34: 4x1+77+31 78.5,31.0 4 srgb(103,180,65)
  2: 2x3+48+0 48.2,1.2 4 srgb(47,140,87)
  127: 4x1+0+66 1.5,66.0 4 srgb(0,0,0)
  55: 3x2+29+34 30.2,34.2 4 srgb(103,180,65)
  5: 1x4+69+0 69.0,1.5 4 srgb(0,0,0)
  21: 1x3+55+11 55.0,12.0 3 srgb(47,140,87)
  10: 1x3+51+4 51.0,5.0 3 srgb(47,140,87)
  22: 3x1+56+14 57.0,14.0 3 srgb(47,140,87)
  56: 2x2+44+34 44.7,34.3 3 srgb(47,140,87)
  43: 2x1+60+32 60.5,32.0 2 srgb(47,140,87)
  147: 1x2+87+108 87.0,108.5 2 srgb(103,180,65)
  97: 2x1+107+42 107.5,42.0 2 srgb(47,140,87)
  133: 2x1+100+88 100.5,88.0 2 srgb(103,180,65)
  143: 2x1+98+92 98.5,92.0 2 srgb(103,180,65)
  100: 2x1+116+42 116.5,42.0 2 srgb(103,180,65)
  32: 1x2+0+30 0.0,30.5 2 srgb(103,180,65)
  135: 1x2+103+88 103.0,88.5 2 srgb(103,180,65)
  148: 1x2+88+108 88.0,108.5 2 srgb(47,140,87)
  121: 1x2+120+52 120.0,52.5 2 srgb(240,240,240)
  120: 1x2+119+52 119.0,52.5 2 srgb(47,140,87)
  59: 1x2+18+35 18.0,35.5 2 srgb(47,140,87)
  89: 2x1+20+39 20.5,39.0 2 srgb(103,180,65)
  105: 1x1+107+45 107.0,45.0 1 srgb(47,140,87)
  106: 1x1+108+46 108.0,46.0 1 srgb(59,79,162)
  107: 1x1+114+46 114.0,46.0 1 srgb(103,180,65)
  108: 1x1+112+47 112.0,47.0 1 srgb(47,140,87)
  109: 1x1+115+47 115.0,47.0 1 srgb(103,180,65)
  111: 1x1+113+48 113.0,48.0 1 srgb(103,180,65)
  112: 1x1+116+48 116.0,48.0 1 srgb(103,180,65)
  113: 1x1+114+49 114.0,49.0 1 srgb(103,180,65)
  114: 1x1+117+49 117.0,49.0 1 srgb(47,140,87)
  115: 1x1+115+50 115.0,50.0 1 srgb(103,180,65)
  116: 1x1+116+51 116.0,51.0 1 srgb(47,140,87)
  117: 1x1+120+51 120.0,51.0 1 srgb(103,180,65)
  136: 1x1+99+89 99.0,89.0 1 srgb(103,180,65)
  137: 1x1+98+90 98.0,90.0 1 srgb(103,180,65)
  138: 1x1+102+90 102.0,90.0 1 srgb(103,180,65)
  139: 1x1+97+91 97.0,91.0 1 srgb(103,180,65)
  140: 1x1+100+91 100.0,91.0 1 srgb(103,180,65)
  142: 1x1+97+92 97.0,92.0 1 srgb(47,140,87)
  150: 1x1+86+109 86.0,109.0 1 srgb(47,140,87)
  151: 1x1+85+110 85.0,110.0 1 srgb(103,180,65)
  153: 1x1+87+110 87.0,110.0 1 srgb(47,140,87)
  155: 1x1+84+111 84.0,111.0 1 srgb(103,180,65)
  157: 1x1+83+112 83.0,112.0 1 srgb(103,180,65)
  158: 1x1+82+113 82.0,113.0 1 srgb(47,140,87)
  159: 1x1+86+113 86.0,113.0 1 srgb(103,180,65)
  9: 1x1+50+3 50.0,3.0 1 srgb(47,140,87)
  11: 1x1+66+5 66.0,5.0 1 srgb(47,140,87)
  12: 1x1+73+5 73.0,5.0 1 srgb(47,140,87)
  14: 1x1+72+6 72.0,6.0 1 srgb(47,140,87)
  15: 1x1+52+7 52.0,7.0 1 srgb(47,140,87)
  17: 1x1+71+7 71.0,7.0 1 srgb(47,140,87)
  18: 1x1+70+8 70.0,8.0 1 srgb(47,140,87)
  19: 1x1+53+9 53.0,9.0 1 srgb(47,140,87)
  20: 1x1+54+10 54.0,10.0 1 srgb(47,140,87)
  23: 1x1+59+15 59.0,15.0 1 srgb(47,140,87)
  24: 1x1+60+16 60.0,16.0 1 srgb(47,140,87)
  25: 1x1+69+16 69.0,16.0 1 srgb(47,140,87)
  26: 1x1+61+17 61.0,17.0 1 srgb(47,140,87)
  27: 1x1+62+18 62.0,18.0 1 srgb(47,140,87)
  31: 1x1+0+29 0.0,29.0 1 srgb(47,140,87)
  36: 1x1+82+31 82.0,31.0 1 srgb(59,79,162)
  37: 1x1+28+32 28.0,32.0 1 srgb(47,140,87)
  39: 1x1+40+32 40.0,32.0 1 srgb(47,140,87)
  40: 1x1+41+32 41.0,32.0 1 srgb(103,180,65)
  41: 1x1+42+32 42.0,32.0 1 srgb(47,140,87)
  42: 1x1+43+32 43.0,32.0 1 srgb(0,0,0)
  45: 1x1+82+32 82.0,32.0 1 srgb(103,180,65)
  46: 1x1+28+33 28.0,33.0 1 srgb(0,0,0)
  47: 1x1+29+33 29.0,33.0 1 srgb(47,140,87)
  49: 1x1+41+33 41.0,33.0 1 srgb(240,240,240)
  50: 1x1+43+33 43.0,33.0 1 srgb(47,140,87)
  51: 1x1+81+33 81.0,33.0 1 srgb(103,180,65)
  53: 1x1+21+34 21.0,34.0 1 srgb(47,140,87)
  54: 1x1+28+34 28.0,34.0 1 srgb(47,140,87)
  57: 1x1+77+34 77.0,34.0 1 srgb(103,180,65)
  58: 1x1+79+34 79.0,34.0 1 srgb(47,140,87)
  60: 1x1+19+35 19.0,35.0 1 srgb(59,79,162)
  61: 1x1+20+35 20.0,35.0 1 srgb(47,140,87)
  62: 1x1+28+35 28.0,35.0 1 srgb(103,180,65)
  63: 1x1+39+35 39.0,35.0 1 srgb(103,180,65)
  65: 1x1+16+36 16.0,36.0 1 srgb(47,140,87)
  67: 1x1+28+36 28.0,36.0 1 srgb(47,140,87)
  68: 1x1+33+36 33.0,36.0 1 srgb(103,180,65)
  69: 1x1+38+36 38.0,36.0 1 srgb(103,180,65)
  70: 1x1+45+36 45.0,36.0 1 srgb(103,180,65)
  72: 1x1+19+37 19.0,37.0 1 srgb(47,140,87)
  73: 1x1+29+37 29.0,37.0 1 srgb(47,140,87)
  74: 1x1+43+37 43.0,37.0 1 srgb(47,140,87)
  75: 1x1+115+37 115.0,37.0 1 srgb(47,140,87)
  76: 1x1+116+37 116.0,37.0 1 srgb(230,230,97)
  77: 1x1+0+38 0.0,38.0 1 srgb(47,140,87)
  78: 1x1+22+38 22.0,38.0 1 srgb(103,180,65)
  79: 1x1+30+38 30.0,38.0 1 srgb(47,140,87)
  80: 1x1+31+38 31.0,38.0 1 srgb(103,180,65)
  81: 1x1+32+38 32.0,38.0 1 srgb(47,140,87)
  84: 1x1+41+38 41.0,38.0 1 srgb(103,180,65)
  85: 1x1+42+38 42.0,38.0 1 srgb(47,140,87)
  86: 1x1+114+38 114.0,38.0 1 srgb(47,140,87)
  88: 1x1+117+38 117.0,38.0 1 srgb(230,230,97)
  90: 1x1+113+39 113.0,39.0 1 srgb(47,140,87)
  91: 1x1+118+39 118.0,39.0 1 srgb(230,230,97)
  92: 1x1+119+39 119.0,39.0 1 srgb(47,140,87)
  93: 1x1+112+40 112.0,40.0 1 srgb(47,140,87)
  94: 1x1+119+40 119.0,40.0 1 srgb(230,230,97)
  95: 1x1+110+41 110.0,41.0 1 srgb(103,180,65)
  96: 1x1+111+41 111.0,41.0 1 srgb(230,230,97)
  98: 1x1+109+42 109.0,42.0 1 srgb(103,180,65)
  99: 1x1+115+42 115.0,42.0 1 srgb(230,230,97)
  101: 1x1+120+42 120.0,42.0 1 srgb(103,180,65)
  102: 1x1+114+43 114.0,43.0 1 srgb(47,140,87)
  103: 1x1+118+43 118.0,43.0 1 srgb(47,140,87)
  104: 1x1+120+44 120.0,44.0 1 srgb(47,140,87)

人们可以像 Mark 所做的那样将每种颜色提取为单独的图像。

或者,我们可以用我的 bash Unix shell 脚本替换整个过程,该脚本调用 ImageMagick 对原始图像进行 kmeans 分类,并将生成的颜色分成单独的层或图像。见http://www.fmwconcepts.com/imagemagick/index.php

One 将 6 种颜色的列表作为种子(或者,仅提供所需颜色的数量)提供给脚本。所以我会使用马克的颜色。 Kmeans 迭代地将各种颜色收敛到它们的聚类平均值。见https://en.wikipedia.org/wiki/K-means_clustering

kmeans -s "rgb(59,79,162) rgb(230,30,35) rgb(240,240,240) rgb(230,230,97) rgb(103,180,65) rgb(47,140,87)" -t layered 4htJX.png result_%d.png

【讨论】:

以上是关于图像中颜色的分类器的主要内容,如果未能解决你的问题,请参考以下文章

决策树分类器 sklearn 中节点的不同颜色表示啥?

LabVIEW色彩分类识别

机器学习-分类器-级联分类器训练(Train CascadeClassifier )

使用机器学习创建植物健康分类器

显示来自分类器的查询图像

训练级联分类器,同时包括图像注释作为特征