1

PHP gd でコントラスト ストレッチ機能を実装しようとしています。画像の 5 パーセントを最小値として、95 パーセントを最大値として設定したいと考えています。PHP gdを使用してヒストグラムとそれらの値を取得する方法を知っている人はいますか?

ありがとうございました。

4

2 に答える 2

4

残念ながら、ピクセルを 1 つずつカウントする必要があります。

$gd = // Create image of same size, copy original image into $gd
// Make grayscale
imageFilter($gd, IMG_FILTER_GRAYSCALE);

$pre = array_fill(0, 256, 0);

for ($y = 0; $y < ImageSY($gd); $y++)
{
    for ($x = 0; $x < ImageSX($gd); $x++)
    {
        $luma = (imageColorAt($x, $y) & 0xFF); // Grayscale, so R=G=B=luma
        $pre[$luma]++;
    }
}

// Then you need to build the cumulative histogram:

$max = $pre[0];
$hist[0] = $pre[0];
for ($i = 1; $i < 256; $i++)
{
    $hist[$i] = $hist[$i-1]+$pre[$i];
    if ($max < $pre[$i])
            $max = $pre[$i];
}
// Now scale to 100%
for ($i = 0; $i < 256; $i++)
{
    $hist[$i] = ($hist[$i]*100.0)/((float)$hist[255]);
    if ($hist[$i] >= 5)
        if ((0 == $i) || ($hist[$i-1] < 5))
            print "Fifth percentile ends at index $i (not included)\n";
    if ($hist[$i] >= 95)
        if ($hist[$i-1] < 95)
            print "Ninety-fifth percentile begins at index $i (included)\n";
}

// Create graphics, just to check.
// Frequency is red, cumulative histogram is green

$ck = ImageCreateTrueColor(255, 100);
$w  = ImageColorAllocate($ck, 255, 255, 255);
$r  = ImageColorAllocate($ck, 255,   0,   0);
$g  = ImageColorAllocate($ck,   0, 255,   0);
ImageFilledRectangle($ck, 0, 0, 255, 100, $w);
for ($i = 0; $i < 256; $i++)
{
    ImageLine($ck, $i, 100-$hist[$i], $i, 100, $g);
    ImageLine($ck, $i, 100.0-100.0*((float)$pre[$i]/$max), $i, 100, $r);
}
ImagePNG($ck, 'histograms.png');
于 2012-10-04T08:35:36.680 に答える
0

すべての値の並べ替えられたリストを取得します (5 パーセンタイルで昇順、95 パーセンタイルで降順)。次に、最初からそのインデックスまでのサブリストの長さが完全なリストの長さの 5% 以上になるまで、すべての値をループします。現在のインデックスの値は、探しているパーセンタイルです。ヒストグラムすら関係ありません。

于 2012-10-04T08:34:49.503 に答える