使用ImageMagick比较两个图像 [英] Compare two images with ImageMagick

查看:1569
本文介绍了使用ImageMagick比较两个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图拍摄两个图像,并使用ImageMagick进行比较。



我想知道图片是否接近相同,并返回一个数字存储在PHP的变量中,并显示在浏览器屏幕中。



我的主机帐户是与bluehost和我已通过调整图像大小和ImageMagick安装和运行测试。以下代码适用于此处: http://zing-cards.com/imagetest2.php p>

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

?>

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

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

我得到

 code> 20455.4(0.312129)

这是我期望的,因为图像是完全不同的...



我想你应该在命令行上尝试 compare >

例如,如果您的系统上没有安装比较,您将无法获得任何结果...



UPDATE :若要进一步调查持续缺少输出的情况,请执行以下操作:

 <?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 not execute $ compareCommand!
$ output = shell_exec($ compareCommand -metric RMSE $ photoPath1 $ photoPath2 NULL:2>& 1);
print $ output;
?>

UPDATE 2



在注释中回答OP问题:



在convert man页面中没有指定如何以及如何只打印RMSE错误百分比...



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


I am trying to take two images and compare them using ImageMagick.

I want to find out if the images are close to the same and return a number to be stored in a variable in php and displayed in my browser screen.

My hosting account is with bluehost and I have tested by resizing an image and ImageMagick is installed and running. The following code works here: 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);

?>

I understand the the following command line code (according to this post) is supposed to get the results in a number format:

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

Here is my code so far:

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

How do I make this value show in my browser?

Other than the above code I have tried:

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

From what I understand this should display a low number (since the images are equal) and if I use completely different images they should display a high number but I don't get any results.

解决方案

Here if I do

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

I get

20455.4 (0.312129)

Which is what I expect, because the images are completely different...

I suppose you should try compare on the command-line, beforehand...

For example, if you don't have compare installed on your system, you don't get any results...

UPDATE: To further investigate in case of persistent lack of output, please do:

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

UPDATE 2

Answering OP question in comment:

In the convert man page it's not specified if and how it's possible to print only the RMSE error percentage...

You could add (for example) a | sed -e 's/.*(\(.*\))/\1/g' to the command, to just keep RMSE error percentage...

这篇关于使用ImageMagick比较两个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆