将标量值指定给';RGB&x27;使用python对图像的几种主要颜色进行

将标量值指定给';RGB&x27;使用python对图像的几种主要颜色进行,python,arrays,image,colors,data-conversion,Python,Arrays,Image,Colors,Data Conversion,我有一个图像,它是由一些离散的颜色,如绿色,蓝色,红色,黄色,海军蓝。我想阅读此图像并制作一个具有以下两个属性的数组: 每个离散颜色对应一个数值 阵列各轴的大小与图像尺寸成比例(近似) 附言:我在阅读图像时遇到的所有问题都涉及rgb/像素。如果问题的目的相反,这只是一个有用的问题,在这种情况下,它可能与我所问的问题类似。不确定这是否接近您想要的,但如果我使用安装在大多数Linux发行版上并可用于OSX和Windows的ImageMagick,我可以在终端中运行此命令: convert image

我有一个图像,它是由一些离散的颜色,如绿色,蓝色,红色,黄色,海军蓝。我想阅读此图像并制作一个具有以下两个属性的数组:

  • 每个离散颜色对应一个数值
  • 阵列各轴的大小与图像尺寸成比例(近似)

  • 附言:我在阅读图像时遇到的所有问题都涉及rgb/像素。如果问题的目的相反,这只是一个有用的问题,在这种情况下,它可能与我所问的问题类似。

    不确定这是否接近您想要的,但如果我使用安装在大多数Linux发行版上并可用于OSX和Windows的ImageMagick,我可以在终端中运行此命令:

    convert image.png -alpha off -colors 6 -sample 50x20! result.png
    

    如果我要求输出文本,而不是图像,我会得到:

    convert image.png -alpha off -colors 6 -sample 50x20! txt:
    
    # ImageMagick pixel enumeration: 50,20,255,srgb
    0,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    1,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    2,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    3,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    4,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    5,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    6,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    7,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    8,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    9,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    10,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    11,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    12,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    13,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    14,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    15,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    16,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    17,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    18,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    19,0: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    20,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    21,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    22,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    23,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    24,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    25,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    26,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    27,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    28,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    29,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    30,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    31,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    32,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    33,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    34,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    35,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    36,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    37,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    38,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    39,0: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    40,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    41,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    42,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    43,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    44,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    45,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    46,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    47,0: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    48,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    49,0: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    0,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    1,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    2,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    3,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    4,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    5,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    6,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    7,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    8,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    9,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    10,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    11,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    12,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    13,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    14,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    15,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    16,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    17,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    18,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    19,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    20,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    21,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    22,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    23,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    24,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    25,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    26,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    27,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    28,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    29,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    30,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    31,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    32,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    33,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    34,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    35,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    36,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    37,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    38,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    39,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    40,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    41,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    42,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    43,1: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    44,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    45,1: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    46,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    47,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    48,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    49,1: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    0,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    1,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    2,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    3,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    4,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    5,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    6,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    7,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    8,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    9,2: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    10,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    11,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    12,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    13,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    14,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    15,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    16,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    17,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    18,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    19,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    20,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    21,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    22,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    23,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    24,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    25,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    26,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    27,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    28,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    29,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    30,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    31,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    32,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    33,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    34,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    35,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    36,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    37,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    38,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    39,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    40,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    41,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    42,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    43,2: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    44,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    45,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    46,2: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    47,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    48,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    49,2: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    0,3: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    1,3: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    ...
    ...
    46,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    47,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    48,18: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    49,18: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    0,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    1,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    2,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    3,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    4,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    5,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    6,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    7,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    8,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    9,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    10,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    11,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    12,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    13,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    14,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    15,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    16,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    17,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    18,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    19,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    20,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    21,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    22,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    23,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    24,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    25,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    26,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    27,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    28,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    29,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    30,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    31,19: (47517,15011,17755)  #B93A45  srgb(185,58,69)
    32,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    33,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    34,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    35,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    36,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    37,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    38,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    39,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    40,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    41,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    42,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    43,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    44,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    45,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    46,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    47,19: (17144,20406,18979)  #434F4A  srgb(67,79,74)
    48,19: (50918,44309,39507)  #C6AC9A  srgb(198,172,154)
    49,19: (52533,46293,16329)  #CCB440  srgb(204,180,64)
    
    也许这会让你开始

    -colors 6
    之后添加
    -median 7
    ,在一定程度上减少了小的“噪音”补丁

    convert image.png -alpha off -colors 6 -median 7 -sample 50x20! result.png
    

    请注意,ImageMagick也有Python绑定

    另一种选择可能是制作您自己的6种颜色的小色样,您希望识别如下:

    convert xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,160,120)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png
    
    convert image.png -colorspace hsl -separate hsl.png
    
    然后告诉ImageMagick将图像中的所有颜色映射到样例:

    convert image.png -quantize sRGB -alpha off -remap swatch.png  -sample 50x20! result.png
    

    如果您希望使用调色板颜色,请在末尾添加
    -详细信息:
    ,而不是输出图像名称,然后您将得到以下结果-查看最后6行-它们显示颜色索引:

    convert image.png -alpha off -colors 6 -sample 50x20! -type palette -verbose info:
    
    Image: image.png
      Format: PNG (Portable Network Graphics)
      Mime type: image/png
      Class: PseudoClass
      Geometry: 50x20+0+0
      Resolution: 59.05x59.05
      Print size: 0.84674x0.338696
      Units: PixelsPerCentimeter
      Type: Palette
      Base type: Palette
      Endianess: Undefined
      Colorspace: sRGB
      Depth: 8/16-bit
      Channel depth:
        red: 16-bit
        green: 16-bit
        blue: 16-bit
      Channel statistics:
        Pixels: 1000
        Red:
          min: 2 (0.00935378)
          max: 204 (0.801602)
          mean: 114.15 (0.447646)
          standard deviation: 85.674 (0.335977)
          kurtosis: -1.77949
          skewness: -0.174985
          entropy: 0.994611
        Green:
          min: 58 (0.229053)
          max: 180 (0.706386)
          mean: 127.02 (0.498117)
          standard deviation: 45.2218 (0.17734)
          kurtosis: -1.4666
          skewness: -0.2851
          entropy: 0.994611
        Blue:
          min: 64 (0.249165)
          max: 175 (0.687266)
          mean: 107.26 (0.420626)
          standard deviation: 43.1734 (0.169307)
          kurtosis: -1.48194
          skewness: 0.477601
          entropy: 0.994611
      Image statistics:
        Overall:
          min: 2 (0.00935378)
          max: 204 (0.801602)
          mean: 116.143 (0.455463)
          standard deviation: 61.2345 (0.240135)
          kurtosis: -1.14278
          skewness: -0.180352
          entropy: 0.994611
      Colors: 6
      Histogram:
           146: (  2,144,105) #029069 srgb(2,144,105)
           150: ( 10,125,175) #0A7DAF srgb(10,125,175)
           199: ( 67, 79, 74) #434F4A srgb(67,79,74)
           148: (185, 58, 69) #B93A45 srgb(185,58,69)
           200: (198,172,154) #C6AC9A srgb(198,172,154)
           157: (204,180, 64) #CCB440 srgb(204,180,64)
      Colormap entries: 6
      Colormap:
             0: ( 67, 79, 74) #434F4A srgb(67,79,74)      <--- index 0 in palette
             1: (185, 58, 69) #B93A45 srgb(185,58,69)     <--- index 1 in palette
             2: (  2,144,105) #029069 srgb(2,144,105)
             3: (204,180, 64) #CCB440 srgb(204,180,64)
             4: (198,172,154) #C6AC9A srgb(198,172,154)
             5: ( 10,125,175) #0A7DAF srgb(10,125,175)    <--- last palette entry
    
    convert image.png-alpha off-colors 6-sample 50x20-类型调色板-详细信息:
    图片:Image.png
    格式:PNG(便携式网络图形)
    Mime类型:图像/png
    类别:伪类别
    几何图形:50x20+0+0
    决议:59.05x59.05
    打印尺寸:0.84674x0.338696
    单位:像素分光光度计
    类型:调色板
    基本类型:调色板
    Endianess:未定义
    色彩空间:sRGB
    深度:8/16位
    航道深度:
    红色:16位
    绿色:16位
    蓝色:16位
    频道统计:
    像素:1000
    红色:
    最低:2(0.00935378)
    最高:204(0.801602)
    平均数:114.15(0.447646)
    标准偏差:85.674(0.335977)
    峰度:-1.77949
    偏度:-0.174985
    熵:0.994611
    绿色:
    最低:58(0.229053)
    最大值:180(0.706386)
    平均数:127.02(0.498117)
    标准偏差:45.2218(0.17734)
    峰度:-1.4666
    偏度:-0.2851
    熵:0.994611
    蓝色:
    最低:64(0.249165)
    最高:175(0.687266)
    平均数:107.26(0.420626)
    标准偏差:43.1734(0.169307)
    峰度:-1.48194
    偏度:0.477601
    熵:0.994611
    图像统计:
    总体而言:
    最低:2(0.00935378)
    最高:204(0.801602)
    平均数:116.143(0.455463)
    标准偏差:61.2345(0.240135)
    峰度:-1.14278
    偏度:-0.180352
    熵:0.994611
    颜色:6
    直方图:
    146:(2144105)029069SRGB(2144105)
    150:(10125175)#0A7DAF srgb(10125175)
    199:(67,79,74)354F4ASRGB(67,79,74)
    148:(185,58,69)#B93A45 srgb(185,58,69)
    200:(198172154)#C6AC9A srgb(198172154)
    157:(204180,64)#CCB440 srgb(204180,64)
    彩色地图条目:6
    彩色地图:
    
    0:(67,79,74)434F4ASRGB(67,79,74)我的另一个答案太长了,我还有一些想法想与大家分享。。。在另一个答案之前,先把这个答案看作是一个预处理步骤

    如果在HSL颜色空间中转换图像,如下所示:

    convert xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,160,120)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png
    
    convert image.png -colorspace hsl -separate hsl.png
    
    您将在文件
    hsl-0.png
    中获得如下色调:

    convert xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,160,120)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png
    
    convert image.png -colorspace hsl -separate hsl.png
    

    文件
    hsl-1.png
    中的饱和度如下:

    convert xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,160,120)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png
    
    convert image.png -colorspace hsl -separate hsl.png
    

    您可以看到,饱和图像在您的饱和红、绿、蓝和黄颜色处较亮,而在您不感兴趣的棕色处较暗。您还可以看到色调图像中的颜色差别相当大。这让我想到了使用饱和度通道作为遮罩和色调通道来分割颜色的想法

    convert image.png -colorspace hsl -channel s -threshold 80% -channel h -colors 12 -colorspace rgb result.png
    

    或者,从原始图像开始,您可以直接使用以下内容:

    convert image.png -fx "u.saturation>0.8?u:0" -median 3 result.png
    

    您可以将
    -中值3
    增加到5,以去除更多斑点

    因此,我想说,我目前在这方面的最佳尝试如下:

    # Make a swatch of the colours want in the output image
    convert xc:black xc:"rgb(255,230,11)" xc:"rgb(16,128,90)" xc:"rgb(200,0,40)" xc:"rgb(18,140,190)" xc:"rgb(14,17,116)"  +append swatch.png
    
    # Clean up image using Saturation to select coloured areas, median to reduce speckles, then remap to swatch created above and resample down to 50x20
    convert image.png -fx "u.saturation>0.8?u:0" -median 3 -remap swatch.png -sample 50x20! result.png
    

    现在打印调色板索引-这是您实际要求的-我知道如何在PHP中轻松实现这一点:

    #!/usr/local/bin/php
    <?php
    // Read in a palettised image
    $im = imagecreatefrompng("result.png");
    
    for ($r = 0; $r < imagesy($im); $r++) {
        printf("Row %d: ",$r);
        for ($c = 0; $c < imagesx($im); $c++) {
            $pixel = imagecolorat($im, $c, $r);
            $index = $pixel & 0xFF;
            printf("%d ",$index);
        }
        printf("\n");
    }
    ?>
    

    那么,每一行在其整个宽度上的值是相同的,还是会在整个宽度上发生变化?如果是,如何改变?顶部和底部的棕色层会发生什么变化?还有其他照片吗?它们有相同的五种颜色吗?@MarkSetchell:谢谢你的评论。以下是对您问题的一些澄清:
    1我想知道这些行是否包含与图像中显示的小波动相对应的值,这意味着一行的一些索引可能包含与单独颜色相对应的单独值。例如,让我们看一看绿色层:与该颜色对应的行将包含相同的值,但在某些索引中,海军蓝混合在其中<代码>2让我们假设棕色层是一种离散的棕色…因此,现在总共有6种颜色<代码>3是的,相同的颜色。我有两条注释:
    1)
    我没有看到包含对应于这六种不同颜色的数值(介于1-6之间)的数组<代码>2)
    颜色图案在很多地方与原始图像不太一致。我认为这是因为我们无法控制你所建议的算法(尽管不确定该算法是什么)。然而,我更愿意保持原始图像与获得的图像的一致性,比这里显示的更接近。换言之,两幅图像之间的颜色匹配非常复杂