代码之家  ›  专栏  ›  技术社区  ›  biquillo

从图像中提取颜色

  •  5
  • biquillo  · 技术社区  · 16 年前

    我想提取图像中最常用的颜色,或者至少提取主色调 你能推荐我如何开始这个任务吗?或者给我指一个类似的代码?我一直在寻找,但没有成功。

    3 回复  |  直到 16 年前
        1
  •  4
  •   Mark Ransom    16 年前

    使用 Octree Color Quantization algorithm . 其他量化算法可以在 Wikipedia .

        2
  •  3
  •   Jacob    16 年前

    我同意这些评论——编程解决方案肯定需要更多信息。但在此之前,假设您将获得图像中每个像素的RGB值,则应考虑 HSV colorspace 其中色调可以说代表每个像素的“色调”。然后您可以使用 histogram 以识别图像中最常用的色调。

        3
  •  2
  •   NawaMan    16 年前

    好吧,我假设你可以访问每个像素的RGB颜色。有两种方法可以实现,这取决于您想要的方式。

    首先,您可以简单地创建一些像素的R、G和B。就像这样。

    伪代码。

    
    int Red   = 0;
    int Green = 0;
    int Blue  = 0;
    foreach (Pixels as aPixel) {
        Red   += aPixel.getRed();
        Green += aPixel.getGreen();
        Blue  += aPixel.getBlue();
    }

    然后看看哪个更重要。

    这只会让你的照片更红、更绿或更蓝。

    另一种方法也可以通过简单地创建每个RGB组合的柱状图来为组合颜色(如橙色)提供静态效果。

    伪代码。

    
    Map ColorCounts = new();
    foreach (Pixels as aPixel) {
        const aRGB   = aPixel.getRGB();
        var   aCount = ColorCounts.get(aRGB);
        aCount++;
        ColorCounts.put(aRGB, aCount);
    }

    然后看看哪一个更重要。 您还可以降低颜色分辨率,因为常规的RGB着色最多可以提供670万种颜色。

    这可以很容易地通过给RGB颜色范围来完成。例如,假设rgb是8步而不是256步。

    伪代码。

    
    
    function Reduce(Color) {
        return (Color/32)*32; // 32 is 256/8 as for 8 ranges.
    }
    function ReduceRGB(RGB) {
        return new RGB(Reduce(RGB.getRed()),Reduce(RGB.getGreen() Reduce(RGB.getBlue()));
    }
    
    Map ColorCounts = new();
    foreach (Pixels as aPixel) {
        const aRGB   = ReduceRGB(aPixel.getRGB());
        var   aCount = ColorCounts.get(aRGB);
        aCount++;
        ColorCounts.put(aRGB, aCount);
    }

    然后您可以看到哪个范围的计数最多。

    我希望这些技巧对你有意义。