使用 PHP 从图像中获取视觉上最显眼的颜色
问题:是否可以在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您的色彩空间是标准 RGB,维基百科文章 亮度(相对) 表示 R、G 和 B 的乘数应该是
而不是你有什么。所以
Since your color space is standard RGB, the Wikipedia article Luminance (relative) says your multipliers on R, G, and B should be
instead of what you have. So