使用 Imagick 勾勒透明图像时如何用边框颜色填充封闭区域 PHP

How to fill the closed regions by a border color when outlining a transparent image using Imagick PHP

我想在具有 20 像素边框的透明背景中勾勒出一个对象的轮廓。但是,我想用边框颜色填充封闭区域。

$image = new Imagick('./img/hinata.png');
$mask = clone $image;
$mask->separateImageChannel(Imagick::CHANNEL_ALPHA);
$mask->negateImage(true);
$mask->edgeImage(20);
$mask->opaquePaintImage("white","blue",65000,false);
//// TODO: I don't know how to fill the holes
$mask->transparentPaintImage("black",0.0,0,false);
$image->compositeImage($mask,Imagick::COMPOSITE_DEFAULT,0,0);

我参考了这个问题:

这是图片:

这就是我想要实现的:

这不是我想要实现的:

这就是我在 ImageMagick 命令行中的做法。

Make the background under the transparency blue.

Extract the alpha channel.

Dilate the alpha channel.

Use connected components to fill in any "holes" smaller than some threshold in area.

Replace the old alpha channel with the new one


输入:from here

convert cartoon_girl.png -background blue -alpha background \
\( -clone 0 -alpha extract \
-morphology dilate diamond:12 \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=500 \
-connected-components 8 \) \
-alpha off -compose copy_opacity -composite \
result.png


不幸的是,据我所知,Imagick 不支持连通分量。所以唯一的其他方法是在每个 "hole" 内的某个点使用洪水填充。这意味着您必须在每个孔内选择 x,y 坐标,以用于在进行扩张后进行洪水填充。参见 https://www.php.net/manual/en/imagick.floodfillpaintimage.php

convert cartoon_girl.png -background blue -alpha background \
\( -clone 0 -alpha extract \
-morphology dilate diamond:12 \
-fuzz 80% -fill white \
-draw "color 100,310 floodfill" \
-draw "color 200,235 floodfill" -alpha off  \) \
-alpha off -compose copy_opacity -composite \
result2.png