显示图像的颜色直方图

发布于 2024-12-17 05:07:48 字数 382 浏览 2 评论 0原文

我正在 PHP 中寻找一个函数,它将 直方图 从图像提取到 PNG 文件。此 PNG 文件将位于与实际图像不同的文件夹中,并且该函数必须处理大图像(超过 3 MB)。我确实找到了一个 函数几乎与我的请求类似,但该函数无法处理大图像,而且它没有显示了直方图,也没有显示其网站上显示的图像(它只显示了一个带有边框的空白窗口)。

我希望你们能帮助我解决这个问题。

提前致谢。

I'm searching for a function in PHP which extract the histogram from an image to an PNG file. This PNG file will be located in a different folder than the actual image and the function must handle large images (over 3 MB). I did find a function almost similar to my request but the function can not handle large images and it didn't showed the histogram nor the image as showed on their website (it showed only a blank window with a border).

I hope that you guys can help me with this.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风透绣罗衣 2024-12-24 05:07:48

我们一直在我们的项目中使用这个:
http://www.histogramgenerator.com/

我们没有遇到大图像问题。它不是免费的,但我们确实觉得它是
值得我们付的钱。该课程还提供许多其他有趣的功能。

问候

We've been using this one for our projects:
http://www.histogramgenerator.com/

We did not experience issues with large images. It's not free, but we definetly feel it's
worth the money we paid for. The class also offers many additional interesting features.

Regards

我不吻晚风 2024-12-24 05:07:48

它是一个像 Photoshop 那样绘制简单直方图的脚本(只是相似,因为我怀疑它使用 sigmoid 函数或类似函数缩放两个轴)。

我编写了一个 scale() 函数,您可以在其中使用最后一个布尔参数来绘制线性直方图,或使用平方根刻度来提高低值。

<?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();
?>

请注意,我不会检查filename是否存在,也不检查getimagesize()imagecreatefrompng()是否失败。

It is a script to draw a simple histogram like Photoshop does (only similar, because I suspect it scale both axes with a sigmoid function, or something like that).

I wrote a scale() function where you can use a last bool argument to do a linear histogram, or use a square root scale to boost low values.

<?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();
?>

Note I'm not checking if filename exist, neither if getimagesize() or imagecreatefrompng() failed.

野却迷人 2024-12-24 05:07:48

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

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

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

在创建Image之后,恢复内存限制

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

参考:http://www.php.net/manual/en/function.imagecreatefrompng.php#73546

I tested this with a 2MB (5800 x 5800) PNG Image. Basicaly the "imagecreatefrompng()" method is consuming lot of memory.

So before making the call, I increased the memory al the way up to 512M and set the execution time to 5 mins

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

After the Image is created, restore the memory limit

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

Reference: http://www.php.net/manual/en/function.imagecreatefrompng.php#73546

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文