使用ImageMagick比较两张图像


Compare two images with ImageMagick

我正在尝试拍摄两张图像,并使用ImageMagick进行比较。

我想知道这些图像是否接近相同,并返回一个数字,将其存储在php中的变量中,并显示在我的浏览器屏幕上。

我的主机帐户是bluehost,我已经通过调整图像大小进行了测试,ImageMagick已经安装并运行。以下代码在此处工作:http://zing-cards.com/imagetest2.php

<?php
$photo="/home/tcsdesig/public_html/zing/flower.jpg";
$cmd = "/usr/bin/convert $photo -thumbnail 100x100 JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>

我理解以下命令行代码(根据这篇文章)应该以数字格式获得结果:

compare -metric RMSE first.png second.png NULL:

这是我到目前为止的代码:

<?php
$photoPath1="/home/*username*/public_html/flower.jpg";
$photoPath2="/home/*username*/public_html/flower1.jpg";
$cmd = "/usr/bin/compare -metric RMSE $photoPath1 $photoPath2 NULL:";
exec($cmd);
?>

如何在浏览器中显示此值?

除了上面的代码我已经尝试:

$result = exec($cmd);
echo $result;

据我所知,这应该显示一个较低的数字(因为图像是相等的),如果我使用完全不同的图像,它们应该显示较高的数字,但我没有得到任何结果。

如果我进行

<?php
    $photoPath1="/home/*username*/public_html/flower.jpg";
    $photoPath2="/home/*username*/public_html/flower1.jpg";
    $result = exec("/usr/bin/compare -metric RMSE /usr/share/pixmaps/faces/dice.jpg /usr/share/pixmaps/faces/fish.jpg NULL:");
    echo $result;
?>

我得到

20455.4 (0.312129)

这就是我所期望的,因为图像完全不同。。。

我想您应该事先在命令行上尝试compare。。。

例如,如果您的系统上没有安装compare,则不会得到任何结果。。。

更新:要在持续缺乏输出的情况下进行进一步调查,请执行:

<?php
    $photoPath1 = "/home/*username*/public_html/flower.jpg";
    $photoPath2 = "/home/*username*/public_html/flower1.jpg";
    $compareCommand = "/usr/bin/compare";
    if (!is_executable($compareCommand)) die("Sorry, can't execute $compareCommand!");
    $output = shell_exec("$compareCommand -metric RMSE $photoPath1 $photoPath2 NULL: 2>&1");
    print $output;
?>

更新2

在评论中回答OP问题:

在转换手册页中,没有指定是否以及如何可以只打印RMSE错误百分比。。。

您可以在命令中添加(例如)| sed -e 's/.*('(.*'))/'1/g',以保持RMSE错误百分比。。。