使用 PHP 从图像中获取视觉上最显眼的颜色

发布于 2024-09-30 05:13:57 字数 1070 浏览 3 评论 0原文

问题:是否可以在php中获取颜色亮度值?

我想做的是使用 PHP 从图像中获取最“视觉上突出”的颜色。我已经浏览了所有 Stack Overflow,但没有找到任何解决方案。

目前,我已经通过图像构建了颜色搜索,该搜索循环遍历图像的每个像素,存储颜色,计算色差和亮度值,然后将其存储在数据库中。

问题:结果在数学上是正确的,但在视觉上与之前匹配颜色的图像并不准确。我认为某种亮度比较可以解决这个问题。

我注意到,当我按升序(最低的在前)对结果进行排序时,结果实际上更准确(尽管在某些情况下不那么准确)。这意味着颜色出现值比订购 DESC(最高的在前)时低得多。

为了计算“亮度”,我使用了这个——我不相信它有那么强大:

function luminance($pixel){

   $pixel = sprintf('%06x',$pixel);
   $red = hexdec(substr($pixel,0,2))*0.30;
   $green = hexdec(substr($pixel,2,2))*0.59;
   $blue = hexdec(substr($pixel,4))*0.11;

   return $red+$green+$blue;
}

这是一个显示问题的场景: http://twitpic.com/33e23p http://twitpic.com/33e3x0

我也开始怀疑我的颜色距离函数是否找不到最相似的颜色:

function colorDistance(array $color1, array $color2) {
  return sqrt(pow($color1[0] - $color2[0], 2) +
    pow($color1[1] - $color2[1], 2) +
    pow($color1[2] - $color2[2], 2));
}

Question: Is it possible to get a colors luminance value in php?

What I'm trying to do is get the most "visually prominent" colors from an image with PHP. I've gone through all of Stack Overflow and I've not been able to find any solution.

Currently I've built a color search by images that loops through every pixel of an image, stores the colors, calculates color differences and brightness value then stores those in the database.

The problem: The results are mathematically correct, however they aren't visually accurate to the image the colors were previously matched from. I assumed that some sort of luminance comparison might solve this problem.

I've noticed that when I order the results by ascending (lowest first), that the results are actually more accurate (although not as accurate in some cases). This means that the color occurrence values are much lower than when ordering DESC (highest first).

To calculate "brightness", I'm using this—I don't believe its that powerful:

function luminance($pixel){

   $pixel = sprintf('%06x',$pixel);
   $red = hexdec(substr($pixel,0,2))*0.30;
   $green = hexdec(substr($pixel,2,2))*0.59;
   $blue = hexdec(substr($pixel,4))*0.11;

   return $red+$green+$blue;
}

Here is one scenario that shows the problem:
http://twitpic.com/33e23p
http://twitpic.com/33e3x0

I'm also starting to wonder if my color distance function is not finding the most similar color:

function colorDistance(array $color1, array $color2) {
  return sqrt(pow($color1[0] - $color2[0], 2) +
    pow($color1[1] - $color2[1], 2) +
    pow($color1[2] - $color2[2], 2));
}

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

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

发布评论

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

评论(1

梦过后 2024-10-07 05:13:57

由于您的色彩空间是标准 RGB,维基百科文章 亮度(相对) 表示 R、G 和 B 的乘数应该是

  • R: 0.21
  • G: 0.72
  • B: 0.07

而不是你有什么。所以

function luminance($pixel){

   $pixel = sprintf('%06x',$pixel);
   $red = hexdec(substr($pixel,0,2))*0.21;
   $green = hexdec(substr($pixel,2,2))*0.72;
   $blue = hexdec(substr($pixel,4))*0.07;

   return $red+$green+$blue;
}

Since your color space is standard RGB, the Wikipedia article Luminance (relative) says your multipliers on R, G, and B should be

  • R: 0.21
  • G: 0.72
  • B: 0.07

instead of what you have. So

function luminance($pixel){

   $pixel = sprintf('%06x',$pixel);
   $red = hexdec(substr($pixel,0,2))*0.21;
   $green = hexdec(substr($pixel,2,2))*0.72;
   $blue = hexdec(substr($pixel,4))*0.07;

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