显示图像的颜色直方图

4
我正在寻找一个在PHP中提取图像直方图并将其转换为PNG文件的函数。这个PNG文件需要位于与实际图像不同的文件夹中,而且该函数必须处理大型图像(超过3 MB)。我发现了一个几乎符合我的要求的函数,但是该函数无法处理大型图像,并且它没有像网站上展示的那样展示出直方图或者图像(它只显示了一个带边框的空白窗口)。
我希望你们能帮助我解决这个问题。
谢谢。
3个回答

0
我们一直在使用这个工具来处理项目中的图片: http://www.histogramgenerator.com/ 我们没有遇到过大图片的问题。虽然这个工具不是免费的,但我们绝对觉得它物有所值。此外,该类还提供了许多附加的有趣功能。
谢谢!

谢谢你的回答 :) 29.99欧元或29.99美元(他们添加了两种货币,但没有链接,这非常令人困惑)对于这种功能来说并不是最便宜的,但我相信你 :) 我会尽快查看它。 - Airikr

0

这是一个编写简单直方图的脚本,就像Photoshop一样(只是类似,因为我怀疑它使用了sigmoid函数或类似的方法来缩放两个轴)。

我编写了一个scale()函数,您可以使用最后一个bool参数制作线性直方图,或者使用平方根比例来提高低值。

<?php
    //Just in case GD needs more memory
    ini_set('memory_limit', '64M');

    $filename='image1.png';
    //Attempt to open 
    [$width, $height, $type]=getimagesize($filename);
    if($type==IMAGETYPE_PNG){
        $img=imagecreatefrompng($filename);
    }

    //Histogram initialization
    $hist = array(
      'red'=>array_fill(0,256,0),
      'green'=>array_fill(0,256,0),
      'blue'=>array_fill(0,256,0)
    );

    //Counting colors
    for($x=0;$x<$width;++$x){
        for($y=0;$y<$height;++$y){          
            $bytes=imagecolorat($img,$x,$y);
            $colors=imagecolorsforindex($img,$bytes);
            ++$hist['red'][$colors['red']];
            ++$hist['green'][$colors['green']];
            ++$hist['blue'][$colors['blue']];
        }
    }

    //Drawing histogram as a 256x128px image            
    $width=256;
    $height=128;
    $newimg=imagecreatetruecolor($width,$height);    
    //Max frequency for normalization
    $maxr=max($hist['red']);
    $maxg=max($hist['green']);                
    $maxb=max($hist['blue']);             
    $max=max($maxr,$maxg,$maxb);

    function scale($value,$max,$height,$scale=FALSE){
        $result=$value/$max; //normalization: value between 0 and 1
        $result=$scale?$result**0.5:$result; //sqrt scale       
        $result=$height-round($result*$height); //scaling to image height
        return $result;
    }

    $top=220; //255 seems too bright to me
    for($x=0;$x<$width;++$x){
        for($y=0;$y<$height;++$y){          
            $r=($y>scale($hist['red'][$x],$maxr,$height,TRUE))?$top:0;
            $g=($y>scale($hist['green'][$x],$maxg,$height,TRUE))?$top:0;
            $b=($y>scale($hist['blue'][$x],$maxb,$height,TRUE))?$top:0;
            $colors=imagecolorallocate($newimg,$r,$g,$b);
            imagesetpixel($newimg,$x,$y,$colors);
        }
    }

    //Saving the histogram as you need
    imagepng($newimg,'.subfolder/histogram.png');

    //Use the next lines, and remove the previous one, to show the histogram image instead
    //header('Content-Type: image/png');
    //imagepng($newimg);
    exit();
?>

请注意,我没有检查文件名是否存在,也没有检查getimagesize()imagecreatefrompng()是否失败。

-1

我用一张2MB(5800 x 5800)的PNG图像进行了测试。基本上,“imagecreatefrompng()”方法消耗了大量内存。

因此,在调用之前,我将内存增加到512M,并将执行时间设置为5分钟。

ini_set('memory_limit', '512M');
set_time_limit(5*60);

在图像创建完成后,恢复内存限制

$im = ImageCreateFromPng($source_file); 
ini_restore('memory_limit');

参考资料:http://www.php.net/manual/zh/function.imagecreatefrompng.php#73546


谢谢,但我的 webhost 上的 memory_limit 不能超过64M上传 :/ 而且这没有帮助我创建图像的直方图。 - Airikr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接