【问题标题】:Calculate image variance in Imagick在 Imagick 中计算图像方差
【发布时间】:2021-03-15 11:34:54
【问题描述】:

如何在 Imagick 中计算图像的变化?我已经将图像转换为灰度,但是通过原生函数我只能得到平均值和标准偏差。

【问题讨论】:

  • variation 是什么意思?
  • 这是一个标准差和均值的指标
  • 做了一些研究,我发现了这个video。因此,创建第三张差异图像应该没有什么大问题,因为您可以读出像素数据。
  • 你能提供任何你迄今为止尝试过的例子吗?
  • 请编辑您的问题以包含更多信息,例如您当前使用的代码以及缺少的说明

标签: php imagick


【解决方案1】:

我的方法是生成两个噪声图像并计算两个 RGB 像素颜色值之间的欧几里得差异。

使用真实图像(例如网络摄像头)肯定会更好看。

$getRandomNoiseImage = function ($x, $y, $steps): Imagick {
    $draw = new ImagickDraw();
    $draw->setResolution($x, $y);
    $draw->setStrokeWidth(1);
    $pixel = new ImagickPixel();

    for ($i = 0; $i < $x; $i += $steps) {
        for ($j = 0; $j < $y; $j += $steps) {
            $color = join(',', [rand(0, 255), rand(0, 255), rand(0, 255)]);
            $pixel->setColor("rgb($color)");
            $draw->setFillColor($pixel);
            $draw->setStrokeColor($pixel);
            $draw->rectangle($i, $j, $i + $steps, $j + $steps);
        }
    }
    $im = new Imagick();
    $im->newImage($x, $y, new ImagickPixel('black'));
    $im->drawImage($draw);

    return $im;
};

$width  = 64;
$height = 64;
$im1    = $getRandomNoiseImage($width, $height, 16);
$im2    = $getRandomNoiseImage($width, $height, 16);

$draw = new ImagickDraw();
$draw->setResolution($width, $height);
$draw->setStrokeWidth(1);
$pixel = new ImagickPixel();

for ($i = 0; $i < $width; $i++) {
    for ($j = 0; $j < $height; $j++) {
        $c1    = $im1->getImagePixelColor($i, $j)->getColor(0);
        $c2    = $im2->getImagePixelColor($i, $j)->getColor(0);
        $color = [
          sqrt(abs($c2['r']**2 - $c1['r']**2)),
          sqrt(abs($c2['g']**2 - $c1['g']**2)),
          sqrt(abs($c2['b']**2 - $c1['b']**2)),
        ];
        $pixel->setColor(sprintf('rgb(%d,%d,%d)', ...$color));
        $draw->setFillColor($pixel);
        $draw->setStrokeColor($pixel);
        $draw->rectangle($i, $j, $i + 1, $j + 1);
    }
}

$im3 = new Imagick();
$im3->newImage($width, $height, new ImagickPixel('black'));
$im3->drawImage($draw);

$im1->writeImage('image1.png');
$im2->writeImage('image2.png');
$im3->writeImage('image3.png');

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 1970-01-01
    • 2012-03-11
    • 2015-05-09
    • 2017-05-10
    • 2018-06-27
    • 2020-03-31
    • 2021-07-16
    • 1970-01-01
    相关资源
    最近更新 更多