如何检测/计算大图片中是否存在小图片?

时间:2011-07-13 13:12:00

标签: php image imagemagick gd

我想用PHP或ImageMagick在PHP中完成的工作如下:

我有一张大图像(比如2000 x 2000像素)..我想检查一张较小的图像(比如50 x 50像素)是否出现在较大图片内的某个位置,以及该区域的百分比是多少匹配..因此,例如,较大图像中该位置较小图像的匹配率为95%。

这可能吗?怎么可以实现呢?

感谢!!!!

3 个答案:

答案 0 :(得分:4)

我快速浏览了PHP的 ImageMagick GD ,并且没有内置的方法。一种方法可能是使用ImageMagick将较大的图像划分为较小的图像(与较小的图像相同)并开始将它们与较小的图像进行比较。

然而,我认为这将是非常缓慢的。

如果在PHP代码中使用系统调用,则可以使用imagemagick执行此操作。 我不知道你是否想尝试这个,但这是如何做到的:

<?php
//set a bigger time out limit because comparison takes a while
set_time_limit ( 275 ) ;

//the bigger image
$bigimage = "big.bmp";
//the smaller image
$smallimage = "small.bmp";
//result image
$resimg = "/tmp/similarity";

//system call
$output = shell_exec("(compare -metric AE -subimage-search ".$bigimage." ".$smallimage." ".$resimg."  > /dev/null) 3>&1 1>&2 2>&3");

//result is something like "0 @ 251,263"
$res = explode("@",$output);
if($res[0]==0)
{
    echo "Perfect match<br/>";
    $res = explode(",",$res[1]);

    echo "width: ".$res[0];
    echo "<br/>";
    echo "height: ".$res[1];
} else {
    echo "Not match";
}

?>

我已经在Linux框中测试了上述代码,其中XAMPP for Linux 1.7.3a和ImageMagick 6.7.1-0 2011-07-10 Q16。

关于比较我使用公制AE(绝对误差)来计算有多少像素不同。 结果将打印到错误流(STERR)。 有关imagemagick的子图像搜索的更多信息,您可以找到here

祝你好运:)

答案 1 :(得分:1)

这是C#的一个很好的答案:How to find one image inside of another?

虽然我很确定它也可以用于PHP,因为它只是一个通用算法,所以使用PHP进行图像比较是非常糟糕的。

另请参阅维基百科Template match文章。

答案 2 :(得分:0)

行。 如果你有$ BigImage和$ SmallImage。

你可以根据$ big和$ small图片制作$ Final图像。然后您可以使用此功能进行检查:http://www.php.net/manual/en/function.getimagesize.php

如果您只有$ FinalImage,并且想在$ Final图像中跟踪$ smallimage base,那么PHP上的“NO WAY”。

尝试Java。

^ _ ^