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

3
我想在透明背景上用20px的边框勾勒出一个对象。但是,我想用边框颜色填充封闭区域。
$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);

我参考了这个问题: 如何使用Imagick PHP描边透明图像

这是图片: 输入图像描述

这是我想要实现的效果: 输入图像描述

这不是我想要实现的效果: 输入图像描述


请发布原始输入,而不是用棋盘替代透明度的输入。提取alpha通道,然后使用形态学膨胀来扩展它。然后使用连通组件来填充孔洞。 - fmw42
1个回答

2
这是我在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


输出:输入:从这里开始

enter image description 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


enter image description here

很遗憾,据我所知,Imagick不支持连通组件。因此,唯一的另一种方法是在每个“孔”内部的某个点使用泛洪填充。这意味着您必须选择每个孔内部的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


enter image description here


非常有帮助!FloodFill是我一直在寻找的方法。我在PHP中找到了它,该方法是Imagick::floodFillPaintImage()。如果我能指定孔洞中心的坐标,我想我就能填充它们了。谢谢! - tomorin
是的,Imagick :: floodFillPaintImage()的链接已经在我的答案中了。 - fmw42

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接