【问题标题】:How to detect masked region on an image in PHP?如何在 PHP 中检测图像上的蒙版区域?
【发布时间】:2021-09-28 23:16:36
【问题描述】:

我有一些图像,每个图像都有一个相同大小的蒙版(透明)矩形区域。如何检测这些区域的坐标?我搜索了图书馆,但没有一个人这样做。我想放我的二维码而不是蒙面部分。

【问题讨论】:

    标签: php image png mask masking


    【解决方案1】:

    我会尝试(未经测试)读取图像并使用透明颜色:

    $src        = imagecreatefrompng("your-image.png");
    $trans      = imageColorAllocateAlpha($src, 0, 0, 0, 127);
    $transArray = imagecolorsforindex($src, $trans);
    

    我会像这样读取图像尺寸并检查每个像素:

    $width  = imagesx($src);
    $height = imagesy($src);
    for ($x = 0; $x < $width; $x++) {
       for ($y = 0; $y < $height; $y++) {
           $color  = imagecolorat($src, $x, $y);
           $colors = imagecolorsforindex($src, $color);
           if ($colors["alpha"] == $transArray["alpha"]) {
               // you have detected the first pixel of transparency
               // here you have to remember smallest $x and smallest $y
               // as well as biggest $x and biggest $y 
               // that should be your rectangle
           }
       }
     }
    

    【讨论】:

    • 成功了!非常感谢你,马可。现在,我必须弄清楚如何将此解决方案应用于旋转矩形:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 1970-01-01
    相关资源
    最近更新 更多