快速获取图像的亮度/光度

发布于 2024-12-25 05:24:14 字数 159 浏览 6 评论 0原文

我正在 php 中工作,并逐像素地检查每个图像以获得每个图像的平均亮度,这将是 CPU 密集型的...

我已经浏览了 GD 和 imagemagick 文档,但还没有'没有找到一种返回图像平均亮度的方法...这可以在这些库中快速完成,还是在另一个可以通过 php 轻松访问的包中快速完成?

I'm working in php, and going through each image pixel-by-pixel to get an average brightness for each image is going to be way to cpu intensive...

I've looked through both GD and imagemagick docs, but haven't found a way to return the average brightness of an image... Can this be done quickly either in these libraries, or in another package easily accessible by php?

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

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

发布评论

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

评论(5

汐鸠 2025-01-01 05:24:14

这里是一个有趣的帖子,使用ImageMagick 用于计算图像的平均灰度级。这篇文章还讨论了 Mark Ransom 使用 ImageMagick 将尺寸缩小到 1x1 的技术。

Here is an interesting post using ImageMagick for computing the average graylevel of an image. This post also discusses Mark Ransom's technique of size reduction to 1x1 using ImageMagick.

白首有我共你 2025-01-01 05:24:14

在 Imagemagick 命令行中,您可以转换为 HSI 或 LAB,并从 I 或 L 通道的平均值中获取亮度(强度或光度)。这些方法中的任何一个都应该有效。请注意,-scale 1x1 对整个图像/通道进行简单平均,并将该值保存在 1 像素结果中。 -规模非常快。它不像-resize那样使用特定的过滤功能。或者,您可以只计算图像的平均值,而不写入 1 个像素。

convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:

convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:

convert image -colorspace HSI -channel b -separate +channel -format "%[fx:100*u.mean]\n" info:

convert image -colorspace LAB -channel r -separate +channel -format "%[fx:100*u.mean]\n" info:

convert image -colorspace HSI -channel b -separate +channel -format "%[mean]\n" info:

convert image -colorspace LAB -channel r -separate +channel -format "%[mean]\n" info:

The result will be between 0 and 100% with 0 being black and 100 white for all but the last two, where fx range is between 0 and 1. Thus the 100 factor to get percent. For the last two commands, the values will be between 0-255 for Q8 install and 0-65535 for Q16 install.

请注意,通道按顺序标记为 r、g、b。但对于现代版本的 Imagemagick,您可以使用 0,1,2。

或者,您可以获得通道的像素颜色,该颜色将是一些灰度值:

convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:

convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:

Sorry I do not know Imagick, but see

https://www.php.net/manual/en/imagick.scaleimage.php

https://www.php.net/manual/en/imagick.getimagepixelcolor.php

https://www.php.net/manual/en/imagick.transformimagecolorspace.php

https://www.php.net/manual/en/imagick.getimagechannelstatistics.php< /a>

或可能

https://www.php.net/manual/en/imagick.getimageproperty.php

也许 Imagick 专家会很友善地将这些命令之一从命令行转换为 Imagick 代码。

In Imagemagick command line, you can convert to HSI or LAB and get the brightness (Intensity or Luminosity) from the average of the I or L channel. Any of these methods should work. Note that -scale 1x1 does a simple average of the whole image/channel and saves that value in 1 pixel result. -scale is very fast. It is not like -resize, which uses a specific filter function. Alternately, you can just compute the mean of the image without writing to 1 pixel.

convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:

convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[fx:100*u]\n" info:

convert image -colorspace HSI -channel b -separate +channel -format "%[fx:100*u.mean]\n" info:

convert image -colorspace LAB -channel r -separate +channel -format "%[fx:100*u.mean]\n" info:

convert image -colorspace HSI -channel b -separate +channel -format "%[mean]\n" info:

convert image -colorspace LAB -channel r -separate +channel -format "%[mean]\n" info:

The result will be between 0 and 100% with 0 being black and 100 white for all but the last two, where fx range is between 0 and 1. Thus the 100 factor to get percent. For the last two commands, the values will be between 0-255 for Q8 install and 0-65535 for Q16 install.

Note that channels are labeled in order as if they were r,g,b. But for modern versions of Imagemagick, you can use 0,1,2.

Alternately, you can get the pixel color for the channel which will be some gray value:

convert image -colorspace HSI -channel b -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:

convert image -colorspace LAB -channel r -separate +channel -scale 1x1 -format "%[pixel:u.p{0,0}]\n" info:

Sorry I do not know Imagick, but see

https://www.php.net/manual/en/imagick.scaleimage.php

https://www.php.net/manual/en/imagick.getimagepixelcolor.php

https://www.php.net/manual/en/imagick.transformimagecolorspace.php

https://www.php.net/manual/en/imagick.getimagechannelstatistics.php

or possibly

https://www.php.net/manual/en/imagick.getimageproperty.php

Perhaps an Imagick expert would be kind enough to convert one of these commands from command line to Imagick code.

廻憶裏菂餘溫 2025-01-01 05:24:14

样本?只需选择 10% 的随机像素而不是 100%...错误率会明显上升,但 10% 的像素对我来说似乎很好,在大多数情况下它应该会产生很好的结果!

Sample? Just pick 10% of random pixels instead of 100%... Error rate will rise obviously but 10% of the pixels seems fine to me, in most cases it should yield great results!

负佳期 2025-01-01 05:24:14

如果多次使用这些值,请缓存这些值,因为这不是一个快速的解决方案。我首先尝试使用 imagick 将图像大小调整为 1x1 像素,但结果并不好。我在没有 imagick 调整大小的情况下得到了最好的结果,但对于大图像来说它非常慢。该示例的大小调整为 1000x1000 像素。请记住,此示例不涵盖带有 Alpha 通道的图像。

function getImageBrightness( $path )
{
    $width  = 1000;
    $height = 1000;

    try
    {
        $imagick = new imagick( $path );
        $imagick->resizeImage( $width, $height );

        $_brightness = 0;

        for( $i=0; $i < $width; $i++ )
            for( $j=0; $j < $height; $j++ )
                if( $pixel = $imagick->getImagePixelColor($i, $j) )
                    if( $colors = $pixel->getColor() AND isset($colors['r']) )
                    {
                        $brightness = ($colors['r'] + $colors['g'] + $colors['b']) / (3* 255);

                        $_brightness = $brightness + $_brightness;
                    }

        $_brightness = $_brightness / ( $height * $width );

        return $_brightness; // from 0 (black) to 1 (white)

    } catch( ImagickException $e )
    {}

    return 0.5;  // default
}

Cache the values if using them more then once, because this is not a fast solution. I tried first to resize the image to 1x1 pixel with imagick, but the results were not good. Best results I got without imagick resize, but its very slow with big images. The example resizes to 1000x1000 pixels. Keep in mind this example does not cover images with alpha channel.

function getImageBrightness( $path )
{
    $width  = 1000;
    $height = 1000;

    try
    {
        $imagick = new imagick( $path );
        $imagick->resizeImage( $width, $height );

        $_brightness = 0;

        for( $i=0; $i < $width; $i++ )
            for( $j=0; $j < $height; $j++ )
                if( $pixel = $imagick->getImagePixelColor($i, $j) )
                    if( $colors = $pixel->getColor() AND isset($colors['r']) )
                    {
                        $brightness = ($colors['r'] + $colors['g'] + $colors['b']) / (3* 255);

                        $_brightness = $brightness + $_brightness;
                    }

        $_brightness = $_brightness / ( $height * $width );

        return $_brightness; // from 0 (black) to 1 (white)

    } catch( ImagickException $e )
    {}

    return 0.5;  // default
}
绮烟 2025-01-01 05:24:14

Imagick 具有直方图功能,可按计数返回颜色。您可以更快地平均相同的信息,而无需逐像素处理。

性能将取决于图像/直方图中的颜色数量(例如,单色图像将非常快)。

<?php

$image = new Imagick('image.jpg');
$pixels = $image->getImageHistogram();  // Generate histogram of colors

$sumbright = 0;  // sum of brightness values
$cntbright = 0;  // count of pixels

foreach($pixels as $p){
  $color = $p->getColor();  // Get rbg pixel color
  $cnt = $p->getColorCount();  // Get number of pixels with above color

  $sumbright += (($color['r'] + $color['g'] + $color['b']) / (3*255)) * $cnt;  // Calculate 0.0 - 1.0 value of brightness (0=rgb[0] 1=rgb[255])

  $cntbright += $cnt;
}

$avgbright = $sumbright / $cntbright;  // Average 0-1 brightness of all pixels in the histogram

?>

Imagick has a histogram feature to return colors by count. You can more quickly average the same information without processing pixel-by-pixel.

Performance will be dependent on the number of colors in the image/histogram (e.g. a single color image will be extremely fast).

<?php

$image = new Imagick('image.jpg');
$pixels = $image->getImageHistogram();  // Generate histogram of colors

$sumbright = 0;  // sum of brightness values
$cntbright = 0;  // count of pixels

foreach($pixels as $p){
  $color = $p->getColor();  // Get rbg pixel color
  $cnt = $p->getColorCount();  // Get number of pixels with above color

  $sumbright += (($color['r'] + $color['g'] + $color['b']) / (3*255)) * $cnt;  // Calculate 0.0 - 1.0 value of brightness (0=rgb[0] 1=rgb[255])

  $cntbright += $cnt;
}

$avgbright = $sumbright / $cntbright;  // Average 0-1 brightness of all pixels in the histogram

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