从图像中提取主要/最常用的颜色

发布于 2024-08-13 02:52:41 字数 71 浏览 7 评论 0原文

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

I would like to extract the most used colors inside an image, or at least the primary tones
Could you recommend me how can I start with this task? or point me to a similar code? I have being looking for it but no success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

<逆流佳人身旁 2024-08-20 02:52:41

使用八叉树颜色量化算法可以获得非常好的结果。其他量化算法可以在维基百科上找到。

You can get very good results using an Octree Color Quantization algorithm. Other quantization algorithms can be found on Wikipedia.

安稳善良 2024-08-20 02:52:41

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

I agree with the comments - a programming solution would definitely need more information. But till then, assuming you'll obtain the RGB values of each pixel in your image, you should consider the HSV colorspace where the Hue can be said to represent the "tone" of each pixel. You can then use a histogram to identify the most used tones in your image.

半夏半凉 2024-08-20 02:52:41

好吧,我假设您可以访问每个像素的 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);
}

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

我希望这些技术对您有意义。

Well, I assume you can access to each pixel RGB color. There are two ways you can so depending on how you want it.

First you may simply create some of all pixel's R, G and B. Like this.

A pseudo code.


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

Then see which is more.

This give you only the picture is more red, green or blue.

Another way will give you static of combined color too (like orange) by simply create histogram of each RGB combination.

A pseudo code.


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

Then see which one has more count.
You may also reduce the color-resolution as a regular RGB coloring will give you up to 6.7 million colors.

This can be done easily by given the RGB to ranges of color. For example, let say, RGB is 8 step not 256.

A pseudo code.



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);
}

Then you can see which range have the most count.

I hope these technique makes sense to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文