查找坐标&;使用php的图像中实体对象的大小


Finding Coordinates & size of a solid object in an image using php

我想看看是否可以从图像中自动检测,如果像素的颜色与我们想要的颜色匹配(在这种情况下,你可以看到紫色),那么它应该返回紫色框开始的x、y坐标及其尺寸。这应该一直持续到完成,然后显示结果。

查看我要处理的图像:https://i.stack.imgur.com/o6xQk.png

我理解这个理论,我只是循环浏览所有像素,直到找到紫色,然后向上、向下、向左、向右找到边界框,然后继续,注意收集我已经匹配到框的像素列表。

有人能帮忙吗?

您可以使用这个:http://php.net/manual/en/imagick.getimagepixelcolor.php

$image = new Imagick('testimage.jpg');
$x = 1;
$y = 1;
$pixel = $image->getImagePixelColor($x, $y); 

所以你可以这样做循环:

//Pseudo-CODE
myX = -1;
myY = -1;
for(x = 0 -> width)
    for(y = 0 -> height)
    {
        if(getImagePixelColor(x,y) == color)
        {
            myX = x;
            myY = y;
        }
        else
        {
            if(myX != -1 && myY != -1 && getImagePixelColor(x,y) == white && getImagePixelColor(x - 1,y - 1) == color && getImagePixelColor(x - 1,y) == white && getImagePixelColor(x,y - 1) == white)
            {
                myWidth = x - myX;
                myHeight = y - myY;
                //You probably should stop the loop here ;)
            }
        }
    }