1

到目前为止,我已经找到了有关如何获取图像中每个像素的所有 RGB 值的示例,但我想要一些可以分解图像并给我一个简化的调色板的东西。

有没有办法使用 imagetruecolortopalette 以某种方式吐出减少的调色板颜色,或者将图像分成 25 x 25 块然后获取该块的平均值?

也许我缺少另一种选择?我基本上只是希望能够在图像中找到最常见的颜色。

提前致谢。

4

1 回答 1

1

嗯,我已经为客户创造了这样的东西。截图如下

在此处输入图像描述

完整代码如下

$microTime = microtime(true);


function textColor($R1, $G1, $B1) {

    $a = (($R1 * 299) + ($G1 * 587 ) + ($B1 * 114 )) / 1000;
    if ($a < 128)
        return 'white';
    else
        return 'black';
}

function rgb2html($r, $g = -1, $b = -1) {
    $hex = "#";
    $hex.= str_pad(dechex($r), 2, "0", STR_PAD_LEFT);
    $hex.= str_pad(dechex($g), 2, "0", STR_PAD_LEFT);
    $hex.= str_pad(dechex($b), 2, "0", STR_PAD_LEFT);

    return $hex;

    if (is_array($r) && sizeof($r) == 3)
        list($r, $g, $b) = $r;

    $r = intval($r);
    $g = intval($g);
    $b = intval($b);

    $r = dechex($r < 0 ? 0 : ($r > 255 ? 255 : $r));
    $g = dechex($g < 0 ? 0 : ($g > 255 ? 255 : $g));
    $b = dechex($b < 0 ? 0 : ($b > 255 ? 255 : $b));

    $color = (strlen($r) < 2 ? '0' : '') . $r;
    $color .= (strlen($g) < 2 ? '0' : '') . $g;
    $color .= (strlen($b) < 2 ? '0' : '') . $b;
    return '#' . $color;
}

function colorPalette($imageFile, $colorJump, $granularity = 5) {

    $granularity = max(1, abs((int) $granularity));
    $colors = array();
    $ratio = array();
    $wastageCount = array();
    $occurrenceSCount = array();
    $occurrenceMCount = array();


    $size = @getimagesize($imageFile);



    if ($size === false) {
        return false;
    }
    $img = @imagecreatefromstring(file_get_contents($imageFile));

    if (!$img) {
        user_error("Unable to open image file");
        return false;
    }
    for ($y = 0; $y < $size[1]; $y += $granularity) {
        $lastColor = NULL;
        $lastX = -1;

        for ($x = 0; $x < $size[0]; $x += $granularity) {
            $thisColor = imagecolorat($img, $x, $y);
            $rgb = imagecolorsforindex($img, $thisColor);

            $red = round(round(($rgb['red'] / $colorJump)) * $colorJump);
            $green = round(round(($rgb['green'] / $colorJump)) * $colorJump);
            $blue = round(round(($rgb['blue'] / $colorJump)) * $colorJump);

            $thisRGB = $red . ',' . $green . ',' . $blue;


            if ($lastColor != $thisRGB) {

                if (array_key_exists($thisRGB, $wastageCount)) {
                    $wastageCount[$thisRGB]++;
                } else {
                    $wastageCount[$thisRGB] = 1;
                }

                if ($lastX + 1 == $x) {
                    if (array_key_exists($lastColor, $occurrenceSCount)) {
                        $occurrenceSCount[$lastColor]++;
                    } else {
                        $occurrenceSCount[$lastColor] = 1;
                    }
                }


                if ($lastX + 1 != $x) {
                    if (array_key_exists($lastColor, $occurrenceMCount)) {
                        $occurrenceMCount[$lastColor]++;
                    } else {
                        $occurrenceMCount[$lastColor] = 1;
                    }
                }

                $lastColor = $thisRGB;
                $lastX = $x;
            }


            if (array_key_exists($thisRGB, $colors)) {
                $colors[$thisRGB]++;
            } else {
                $colors[$thisRGB] = 1;
            }
        }
    }

    $totalPixels = array_sum($colors);
    foreach ($colors as $k => $v) {
        $ratio[$k] = round(($v / $totalPixels ) * 100, 2);
    }

    return array($ratio, $wastageCount, $colors, $occurrenceSCount, $occurrenceMCount);
}

用法

    $colorJump = 1;
    $pixelJump = 1;
    $paletteR = colorPalette($dbImgFile_dir, $colorJump, $pixelJump);
    $palette = $paletteR[0];
    $wastage = $paletteR[1];
    $colorsFound = $paletteR[2];
    $occSArray = $paletteR[3];
    $occMArray = $paletteR[4];
    $totalPixels = array_sum($colorsFound);

    $totalTime = abs(microtime(true) - $microTime);

循环更加复杂,因为我必须从数据库中获取托盘并将颜色与它们匹配,并且还使用了模板解析器,它是完整的自定义代码,不会帮助你。

从这里忽略Required Weight列,计算单次出现和多次出现,如果有单个像素,例如RED, RED, BLUE, RED, RED它将是1次出现和2次多次出现

于 2013-02-25T06:52:32.907 回答