How to detect position of shape in image PHP [clos

2019-03-06 03:16发布

问题:

I have image with white background and in that image i have some kind of shape - part of cloth. I need to find most left, up, right and bottom pixel. What is the most efficient way to do that?

回答1:

You can use the trimImage function to trim the image, and then look at the geometry and page info to find how the bounding box of the image that was left after trimming.

$base = new Imagick(realpath('./trim.png'));

$base->trimImage(0);

$geometry = $base->getImageGeometry();
$pageInfo = $base->getImagePage();

printf (
    "Width %d Height %d\n",
    $geometry['width'],
    $geometry['height']
);

printf(
    "OffsetX: %d OffsetY %d\n",
    $pageInfo['x'],
    $pageInfo['y']
);