我有两个图像(小的和大的)。其中一个包含另一个。一张图片是一张照片,另一张是这张照片所在的相册页面的图片。我希望你明白我说的话。
那么,如何使用PHP获取大图上小图的坐标(x,y)呢?
发布于 2013-01-17 23:46:04
您需要注意的是,您很可能不能对每个像素执行简单的像素检查,因为过滤和压缩可能会稍微修改每个像素的值。
希望代码能让您走上正确的道路!
首先,让我们迭代我们的图片
$small = imagecreatefrompng("small.png");
$large = imagecreatefrompng("large.png");
$smallwidth = imagesx($small);
$smallheight = imagesy($small);
$largewidth = imagesx($large);
$largeheight = imagesy($large);
$foundX = -1;
$foundY = -1;
$keepThreshold = 20;
$potentialPositions = array();
for($x = 0; $x <= $largewidth - $smallwidth; ++$x)
{
for($y = 0; $y <= $largeheight - $smallheight; ++$y)
{
// Scan the whole picture
$error = GetImageErrorAt($large, $small, $x, $y);
if($error["avg"] < $keepThreshold)
{
array_push($potentialPositions, array("x" => $x, "y" => $y, "error" => $error));
}
}
}
imagedestroy($small);
imagedestroy($large);
echo "Found " . count($potentialPositions) . " potential positions\n";现在,这个错误是从哪里来的?
获得likeliness
我在这里做的是迭代小图片和大图片中的一个“窗口”,检查在red,green和blue通道上有多大差异:
function GetImageErrorAt($haystack, $needle, $startX, $startY)
{
$error = array("red" => 0, "green" => 0, "blue" => 0, "avg" => 0);
$needleWidth = imagesx($needle);
$needleHeight = imagesy($needle);
for($x = 0; $x < $needleWidth; ++$x)
{
for($y = 0; $y < $needleHeight; ++$y)
{
$nrgb = imagecolorat($needle, $x, $y);
$hrgb = imagecolorat($haystack, $x + $startX, $y + $startY);
$nr = $nrgb & 0xFF;
$hr = $hrgb & 0xFF;
$error["red"] += abs($hr - $nr);
$ng = ($nrgb >> 8) & 0xFF;
$hg = ($hrgb >> 8) & 0xFF;
$error["green"] += abs($hg - $ng);
$nb = ($nrgb >> 16) & 0xFF;
$hb = ($hrgb >> 16) & 0xFF;
$error["blue"] += abs($hb - $nb);
}
}
$error["avg"] = ($error["red"] + $error["green"] + $error["blue"]) / ($needleWidth * $needleHeight);
return $error;
}到目前为止,我们已经为大图片中可能包含小图片的每个“窗口”建立了一个潜在的误差值,如果它们看起来“足够好”,则将它们存储在一个数组中。
排序
function SortOnAvgError($a, $b)
{
if($a["error"]["avg"] == $b["error"]["avg"])
{
return 0;
}
return ($a["error"]["avg"] < $b["error"]["avg"]) ? -1 : 1;
}
if(count($potentialPositions) > 0)
{
usort($potentialPositions, "SortOnAvgError");
$mostLikely = $potentialPositions[0];
echo "Most likely at " . $mostLikely["x"] . "," . $mostLikely["y"];
}示例
给出以下两张图片:

和

您应该会得到以下结果:
Found 5 potential positions
Most likely at 288,235这与我们的鸭子的位置完全一致。其他4个位置是1个像素的上、下、左和右。
我将在我为你完成一些优化工作后编辑这篇文章,因为这段代码对于大图像来说太慢了(PHP的表现甚至比我预期的还要差)。
编辑
首先,在做任何“优化”代码之前,我们需要数字,所以我添加了
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();和
$time_end = microtime_float();
echo "in " . ($time_end - $time_start) . " seconds\n";最后,有一个具体的想法,即在算法过程中花费了多少时间。这样,我就可以知道我的更改是改进了代码还是使代码变得更糟。考虑到当前带有这些图片的代码需要大约45分钟才能执行,我们应该能够在这方面有很大的改进。
一种不成功的尝试是缓存来自$needle的RGB,以尝试加速GetImageErrorAt函数,但它使时间变得更糟。
考虑到我们的计算是在几何尺度上进行的,我们探索的像素越多,花费的时间就越长……因此,一个解决方案是跳过许多像素,尝试尽可能快地定位我们的图片,然后更准确地定位我们的位置。
我修改了错误函数,将如何递增x和y作为参数
function GetImageErrorAt($haystack, $needle, $startX, $startY, $increment)
{
$needleWidth = imagesx($needle);
$needleHeight = imagesy($needle);
$error = array("red" => 0, "green" => 0, "blue" => 0, "avg" => 0, "complete" => true);
for($x = 0; $x < $needleWidth; $x = $x + $increment)
{
for($y = 0; $y < $needleHeight; $y = $y + $increment)
{
$hrgb = imagecolorat($haystack, $x + $startX, $y + $startY);
$nrgb = imagecolorat($needle, $x, $y);
$nr = $nrgb & 0xFF;
$hr = $hrgb & 0xFF;
$ng = ($nrgb >> 8) & 0xFF;
$hg = ($hrgb >> 8) & 0xFF;
$nb = ($nrgb >> 16) & 0xFF;
$hb = ($hrgb >> 16) & 0xFF;
$error["red"] += abs($hr - $nr);
$error["green"] += abs($hg - $ng);
$error["blue"] += abs($hb - $nb);
}
}
$error["avg"] = ($error["red"] + $error["green"] + $error["blue"]) / ($needleWidth * $needleHeight);
return $error;
}例如,传递2将使函数的返回速度提高4倍,因为我们跳过了x和y的值。
我还为主循环添加了一个stepSize:
$stepSize = 10;
for($x = 0; $x <= $largewidth - $smallwidth; $x = $x + $stepSize)
{
for($y = 0; $y <= $largeheight - $smallheight; $y = $y + $stepSize)
{
// Scan the whole picture
$error = GetImageErrorAt($large, $small, $x, $y, 2);
if($error["complete"] == true && $error["avg"] < $keepThreshold)
{
array_push($potentialPositions, array("x" => $x, "y" => $y, "error" => $error));
}
}
}这样做,我能够以精确度为代价将执行时间从2657秒减少到7秒。我增加了keepThreshold以获得更多的“潜在结果”。
现在我没有检查每个像素,我的最佳答案是:
Found 8 potential positions
Most likely at 290,240如你所见,我们已经接近我们想要的位置了,但这并不完全正确。
我下一步要做的是在这个“非常接近”的位置周围定义一个矩形,以探索我们添加的stepSize中的每个像素。
我现在将脚本的下半部分更改为:
if(count($potentialPositions) > 0)
{
usort($potentialPositions, "SortOnAvgError");
$mostLikely = $potentialPositions[0];
echo "Most probably around " . $mostLikely["x"] . "," . $mostLikely["y"] . "\n";
$startX = $mostLikely["x"] - $stepSize + 1; // - $stepSize was already explored
$startY = $mostLikely["y"] - $stepSize + 1; // - $stepSize was already explored
$endX = $mostLikely["x"] + $stepSize - 1;
$endY = $mostLikely["y"] + $stepSize - 1;
$refinedPositions = array();
for($x = $startX; $x <= $endX; ++$x)
{
for($y = $startY; $y <= $endY; ++$y)
{
// Scan the whole picture
$error = GetImageErrorAt($large, $small, $x, $y, 1); // now check every pixel!
if($error["avg"] < $keepThreshold) // make the threshold smaller
{
array_push($refinedPositions, array("x" => $x, "y" => $y, "error" => $error));
}
}
}
echo "Found " . count($refinedPositions) . " refined positions\n";
if(count($refinedPositions))
{
usort($refinedPositions, "SortOnAvgError");
$mostLikely = $refinedPositions[0];
echo "Most likely at " . $mostLikely["x"] . "," . $mostLikely["y"] . "\n";
}
}它现在给出了如下输出:
Found 8 potential positions
Most probably around 290,240
Checking between X 281 and 299
Checking between Y 231 and 249
Found 23 refined positions
Most likely at 288,235
in 13.960182189941 seconds这确实是正确的答案,大约比最初的脚本快200倍。
编辑2
现在,我的测试案例有点太简单了.我把它改成了谷歌图片搜索:

寻找这张图片(位于718,432)

考虑到较大的图片大小,我们可以预期较长的处理时间,但算法确实在正确的位置找到了图片:
Found 123 potential positions
Most probably around 720,430
Found 17 refined positions
Most likely at 718,432
in 43.224536895752 seconds编辑3
我决定尝试我在评论中告诉你的选项,在执行查找之前缩小图片,我用它得到了很好的结果。
我在第一个循环之前添加了以下代码:
$smallresizedwidth = $smallwidth / 2;
$smallresizedheight = $smallheight / 2;
$largeresizedwidth = $largewidth / 2;
$largeresizedheight = $largeheight / 2;
$smallresized = imagecreatetruecolor($smallresizedwidth, $smallresizedheight);
$largeresized = imagecreatetruecolor($largeresizedwidth, $largeresizedheight);
imagecopyresized($smallresized, $small, 0, 0, 0, 0, $smallresizedwidth, $smallresizedheight, $smallwidth, $smallheight);
imagecopyresized($largeresized, $large, 0, 0, 0, 0, $largeresizedwidth, $largeresizedheight, $largewidth, $largeheight);然后,当添加到数组中时,我将x和y加倍,给出了以下内容:
array_push($potentialPositions, array("x" => $x * 2, "y" => $y * 2, "error" => $error));代码的其余部分保持不变,因为我们希望在实际大小的图片上进行精确定位。你所要做的就是在最后添加:
imagedestroy($smallresized);
imagedestroy($largeresized);使用这个版本的代码,以及google图像结果,我得到了:
Found 18 potential positions
Most around 720,440
Found 17 refined positions
Most likely at 718,432
in 11.499078989029 seconds性能提升了4倍!
希望这能有所帮助
发布于 2013-01-16 04:09:00
使用ImageMagick。
这个页面会给你答案:How can I detect / calculate if a small pictures is present inside a bigger picture?
https://stackoverflow.com/questions/14345256
复制相似问题