PHPマニュアル/PEARマニュアル | ユーザフォーラムで議論/質問 | マニュアル検索 | ハイライト | ハイライトオフ | ポータル | php spot

Imagick::getImageHistogram - 画像のヒストグラムを取得する | JavaScript入門&応用&リファレンスなら「JavaScriptist」

  

Imagick::getImageHistogram

(PECL imagick 2.0.0)

Imagick::getImageHistogram画像のヒストグラムを取得する

説明

array Imagick::getImageHistogram ( void )

画像のヒストグラムを ImagickPixel オブジェクトの配列で返します。

返り値

画像のヒストグラムを ImagickPixel オブジェクトの配列で返します。

エラー / 例外

エラー時に ImagickException をスローします。

例1 Generates Imagick::getImageHistogram()

<?php
function getColorStatistics($histogramElements$colorChannel) {
    
$colorStatistics = [];

    foreach (
$histogramElements as $histogramElement) {
        
$color $histogramElement->getColorValue($colorChannel);
        
$color intval($color 255);
        
$count $histogramElement->getColorCount();

        if (
array_key_exists($color$colorStatistics)) {
            
$colorStatistics[$color] += $count;
        }
        else {
            
$colorStatistics[$color] = $count;
        }
    }

    
ksort($colorStatistics);
    
    return 
$colorStatistics;
}
    


function 
getImageHistogram($imagePath) {

    
$backgroundColor 'black';

    
$draw = new \ImagickDraw();
    
$draw->setStrokeWidth(0); //make the lines be as thin as possible

    
$imagick = new \Imagick();
    
$imagick->newImage(500500$backgroundColor);
    
$imagick->setImageFormat("png");
    
$imagick->drawImage($draw);

    
$histogramWidth 256;
    
$histogramHeight 100// the height for each RGB segment

    
$imagick = new \Imagick(realpath($imagePath));
    
//Resize the image to be small, otherwise PHP tends to run out of memory
    //This might lead to bad results for images that are pathologically 'pixelly'
    
$imagick->adaptiveResizeImage(200200true);
    
$histogramElements $imagick->getImageHistogram();

    
$histogram = new \Imagick();
    
$histogram->newpseudoimage($histogramWidth$histogramHeight 3'xc:black');
    
$histogram->setImageFormat('png');

    
$getMax = function ($carry$item)  {
        if (
$item $carry) {
            return 
$item;
        }
        return 
$carry;
    };

    
$colorValues = [
        
'red' => getColorStatistics($histogramElements, \Imagick::COLOR_RED),
        
'lime' => getColorStatistics($histogramElements, \Imagick::COLOR_GREEN),
        
'blue' => getColorStatistics($histogramElements, \Imagick::COLOR_BLUE),
    ];

    
$max array_reduce($colorValues['red'] , $getMax0);
    
$max array_reduce($colorValues['lime'] , $getMax$max);
    
$max array_reduce($colorValues['blue'] , $getMax$max);

    
$scale =  $histogramHeight $max;

    
$count 0;
    foreach (
$colorValues as $color => $values) {
        
$draw->setstrokecolor($color);

        
$offset = ($count 1) * $histogramHeight;

        foreach (
$values as $index => $value) {
            
$draw->line($index$offset$index$offset - ($value $scale));
        }
        
$count++;
    }

    
$histogram->drawImage($draw);
    
    
header"Content-Type: image/png" );
    echo 
$histogram;
}

?>


忘却曲線を使ってこの知識を確実に記憶に残す

フォーラムで「Imagick::getImageHistogram - 画像のヒストグラムを取得する」について話す
各種マニュアル: PHPマニュアル | PEARマニュアル | Smarty(英語)マニュアル | PHP-GTKマニュアル | Imagick::getImageHistogram - 画像のヒストグラムを取得する」をGoogle検索
copyright © 1997-2024 PHP ドキュメント作成グループ(ライセンス). provided by php spot. マニュアル: