如何检测/计算大图片中是否存在小图片? [英] How can I detect / calculate if a small pictures is present inside a bigger picture?

查看:184
本文介绍了如何检测/计算大图片中是否存在小图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

What I am trying to accomplish in PHP with GD or ImageMagick is the following:

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

I have one large image (say 2000 x 2000 pixels).. I would like to check if a second, smaller image (say 50 x 50 pixels) appears somewhere inside the larger picture, and to what percentage the area is a match.. So, for example, there's a 95% match of the smaller image at that place in the larger image..

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

Is this possible? How could this be achieved?

谢谢!!!!

推荐答案

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

I had a quick look in PHP's ImageMagick and GD and neither has a built in way of doing that. An approach could be to use ImageMagick to divide the larger image to smaller ones(same size as the smaller one) and start comparing them to the smaller one.

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

However this will be very slow I suppose.

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

You can do that with imagemagick if you use a system call in your PHP code. I don't know if you want to try this out but here is how it can be done:

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

I have tested the above code in a linux box with XAMPP for Linux 1.7.3a and ImageMagick 6.7.1-0 2011-07-10 Q16.

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

About the comparison I use the metric AE(Absolute Error) which counts how many pixels differ. The result is printed to the error stream(STERR). More about imagemagick's subimage search you can find here.

祝你好运:)

这篇关于如何检测/计算大图片中是否存在小图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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