【问题标题】:Mathematically calculate "Vibrancy" of a color数学计算颜色的“活力”
【发布时间】:2020-05-20 06:02:03
【问题描述】:

我正在编写一个程序来分析图片并返回最突出的颜色。获得最常出现的颜色很简单,但我发现这种颜色通常是深黑色/灰色/棕色或白色,而不是您将与图像关联的“颜色”。所以我想获得前 5 种颜色,并根据一些指标进行比较,以确定哪种颜色最“充满活力/色彩缤纷”并返回该颜色。

在这种情况下,饱和度不起作用,因为饱和的黑色将排在较浅的粉红色之上,而亮度/亮度不起作用,因为白色将排在较深的红色附近。我想知道我可以用什么指标来判断这一点。我承认这是一个迟钝的问题,但我知道其他做类似事情的程序,所以我认为必须有某种方法来计算“活力/色彩”。 大多数时间不需要完美

对于我在 javascript 中工作的价值,但实际代码不是问题,我只需要我可以使用的等式,然后我就可以实现它

【问题讨论】:

    标签: math colors


    【解决方案1】:

    没有通用的方法来定义颜色的“活力”。因此,您可以尝试组合多个指标,例如“饱和度”、“亮度”和“亮度”。整体指标越低越好。以下是伪代码示例。

    // Compare metrics to "ideal"
    var deltaSat = Saturation(thisColor) - idealSat;
    var deltaBright = Brightness(thisColor) - idealBrightness;
    var deltaLum = Luminance(thisColor) - idealLum;
    // Calculate overall distance from ideal; the lower
    // the better.
    var dist = sqrt((deltaSat*deltaSat) +
       (deltaBright*deltaBright) +
       (deltaLum*deltaLum))
    

    (如果您的问题仅仅是在计算给定颜色的指标时遇到问题,请参阅我在color topics for programmers 上的页面。)

    如果您的“活力”标准足够复杂,您应该考虑使用分类算法等机器学习技术。在一般的机器学习中:

    • 您训练模型以识别不同的类别(例如本例中的“鲜艳”和“非鲜艳”颜色)。
    • 您测试模型以检查其性能。
    • 一旦模型运行良好,您就可以部署模型并使用它来预测颜色是“鲜艳”还是“不鲜艳”。

    但是,机器学习相当复杂,因此您应该尝试此答案前面给出的更简单的方法。

    【讨论】:

      【解决方案2】:

      在尝试了几种不同的公式后,我在以下方面取得了最大的成功

      let colorfulness = ((max+ min) * (max-min))/max
      

      其中 max 和 min 分别是最高和最低 RGB 值。 This page 对公式本身有更详细的解释。

      这将返回一个介于 0 和 255 之间的值,其中 0 是最不色彩的,而 255 是最多的。通过在一堆不同的颜色上运行它,我发现对于我的应用程序,任何高于 50 的值都足够色彩鲜艳,我认为你可以调整它。

      我的最终代码如下

      function getColorFromImage(image) {
          //gets the three most commonly occuring, distinct colors in an image as RGB values, in order of their frequency
          let palette = getPaletteFromImage(image, 3)
      
          for (let color of palette){
              var colorfulness = 0
      
              //(0,0,0) will return NAN if used in the formula, if (0,0,0) leave colorfulness as its default 0
              if (color != [0,0,0]){
                  //get min & max values
                  let min = Math.min(color)
                  let max = Math.max(color)
      
                  //calculate colorfulness of color
                  colorfulness = ((max+ min) * (max-min))/max
              }
      
              //compare color's colorfulness against a threshold to determine if color is "colorful" enough
              //ive found 50 is a good threshold but adjust as needed
              if (colorfulness >= 50.0){
                  return color
              }
          }
      
          //if none of the colors are deemed to be sufficiently colorful, just return the most common
          return palette[0]
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-07
        • 2018-04-24
        • 2011-07-06
        • 1970-01-01
        • 2013-07-03
        • 2011-08-09
        相关资源
        最近更新 更多