Php 检查像素模式
如何在PHP中检查像素模式 我的意思是,我想用像素A有xxx值,下面的像素B有另一个值yyy作为条件 这是我写的:Php 检查像素模式,php,Php,如何在PHP中检查像素模式 我的意思是,我想用像素A有xxx值,下面的像素B有另一个值yyy作为条件 这是我写的: $img = imagecreatefrompng("myimage.png"); $w = imagesx($img); $h = imagesy($img); for($y=0;$y<$h;$y++) { for($x=0;$x<$w;$x++) { $rgb = imagecolorat($img, $x, $y);
$img = imagecreatefrompng("myimage.png");
$w = imagesx($img);
$h = imagesy($img);
for($y=0;$y<$h;$y++) {
for($x=0;$x<$w;$x++) {
$rgb = imagecolorat($img, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
echo "#".$r.$g.$b.",";
$pixel = $r.$g.$b;
if ($pixel == "481023" and $pixel+1???
}
echo "<br />\r\n";
}
我还想问一下,我是否可以通过每个周期将$x值增加2来加速整个过程。这是因为我有一个2像素的图案,也许我可以使用类似于:
for($x=0;$x<$w;$x+2) {
//...
if ($pixel == "xxx") {//check the following pixel}
else if ($pixel == "yyy") {//check the previous pixel}
}
您可能需要定义如下函数:
function getpixelat($img,$x,$y) {
$rgb = imagecolorat($img,$x,$y);
$r = dechex(($rgb >> 16) & 0xFF);
$g = dechex(($rgb >> 8) & 0xFF);
$b = dechex($rgb & 0xFF);
return $r.$g.$b;
}
请注意dechex-如果希望它看起来像HTML颜色代码,则需要它。否则,白色将是255255255而不是ffffff,并且您也会得到模糊的颜色-202020是深灰色20,20,20还是带有轻微蓝色202,0,20的红色
一旦你有了这个,这应该是一件简单的事情:
for( $y=0; $y<$h; $y++) {
for( $x=0; $x<$w; $x++) {
$pixel = getpixelat($img,$x,$y);
if( $pixel == "481023" && getpixelat($img,$x+1,$y) == "998877") {
// pattern! Do something here.
$x++; // increment X so we don't bother checking the next pixel again.
}
}
}
你试过每两个像素吗?你的代码不起作用吗?我不知道如何设置第一个条件来检查两个连续的像素。你想实现什么?您是否正在检查两个图像是否相同/相似?您是否正在检查图像中是否存在图案或特定序列?