【问题标题】:Image color detection using php使用php进行图像颜色检测
【发布时间】:2011-11-04 20:51:51
【问题描述】:

我使用以下代码来检测图像颜色:

function colorPalette($imageFile, $numColors, $granularity = 5)
{
$granularity = max(1, abs((int)$granularity));
$colors = array();
$size = @getimagesize($imageFile);
if($size === false)
{
   user_error("Unable to get image size data");
   return false;
}
$img = @imagecreatefromjpeg($imageFile);
if(!$img)
{
  user_error("Unable to open image file");
  return false;
}
for($x = 0; $x < $size[0]; $x += $granularity)
{
  for($y = 0; $y < $size[1]; $y += $granularity)
  {
     $thisColor = imagecolorat($img, $x, $y);
     $rgb = imagecolorsforindex($img, $thisColor);
     $red = round(round(($rgb['red'] / 0x33)) * 0x33);
     $green = round(round(($rgb['green'] / 0x33)) * 0x33);
     $blue = round(round(($rgb['blue'] / 0x33)) * 0x33);
     $thisRGB = sprintf('%02X%02X%02X', $red, $green, $blue);
     if(array_key_exists($thisRGB, $colors))
     {
        $colors[$thisRGB]++;
     }
     else
     {
        $colors[$thisRGB] = 1;
     }
  }
}
arsort($colors);
return array_slice(array_keys($colors), 0, $numColors);
}

这里已经介绍过这段代码
Detect "overall average" color of the picture
但是,此代码不适用于分辨率大于 2500 像素的图像!
什么问题?
非常感谢

【问题讨论】:

    标签: php colors


    【解决方案1】:

    假设图像足够大并且包含所有可能的颜色,您可能会创建一个包含多达 1670 万个元素的数组。即使是 2500x2500 的图像也可以有 625 万种可能的独特颜色。

    一个更简单的解决方法是将图像大小调整为单个 1x1 像素,然后读出该像素的颜色..

    【讨论】:

      【解决方案2】:

      你可以分割图像然后检查它,我认为它会工作。

      【讨论】:

        猜你喜欢
        • 2011-01-17
        • 2011-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        • 1970-01-01
        • 2021-11-24
        相关资源
        最近更新 更多