jau*_*udo 2 alpha imagemagick image-processing
我有一个包含 alpha 混合文本的 jpeg。知道字体和大小后,我推导出一个代表文本的 png 文件
使用 ImageMagick,我可以获得原始图片的近似值吗?
实现此目的的一种方法是使用一种称为修复的技术。您可以在 (Python) Skimage 中找到它
http://scikit-image.org/docs/dev/api/skimage.restoration.html#inpaint-biharmonic
或者在 OpenCV 中
https://docs.opencv.org/3.0-beta/modules/photo/doc/inpainting.html https://docs.opencv.org/3.4.0/df/d3d/tutorial_py_inpainting.html
这是Python Skimage修复处理:
小猫图片:
水印图片:
Skimage 修复需要二值掩模图像。所以我可以通过以下方式将你的水印转换为这样的蒙版:
convert watermark.png -alpha extract -threshold 0 mask.png
Run Code Online (Sandbox Code Playgroud)
蒙版图像:
这是Python代码:
#!/opt/local/bin/python3.6
import numpy as np
import skimage.io
import skimage.restoration
import skimage.exposure
img = skimage.io.imread('/Users/fred/desktop/kitty.png')
msk = skimage.io.imread('/Users/fred/desktop/mask.png')
msk = skimage.exposure.rescale_intensity(msk, in_range='image', out_range=(0,1))
newimg = skimage.restoration.inpaint_biharmonic(img, msk, multichannel=True)
skimage.io.imsave('/Users/fred/desktop/kitty_inpaint_biharmonic.png', newimg)
Run Code Online (Sandbox Code Playgroud)
Imagemagick 没有官方版本。但 Imagemagick 论坛上的用户 snibgo 在http://im.snibgo.com/fillholespri.htm上实现了一个他称之为“洞填充”的自定义版本。他在https://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=28640#p127233上展示了一个示例。
此外,在同一页面上,他展示了一些聪明的 Imagemagick 代码,可以重复进行少量的大小调整。这实现了与修复有些相似的结果。但总的来说,它不会像修复那么好。尽管如此,它确实对你的形象起到了一定的作用。
小猫图片:
水印图片:
首先,我必须获取您的水印图像并从中提取二进制图像,其中文本为白色,背景为黑色。然后我用它使文本所在的小猫图像透明。然后我裁剪掉文本区域只是为了让后续处理更快。
convert kitty.png \
\( watermark.png -alpha extract -threshold 0 -negate \) \
-alpha off -compose copy_opacity -composite \
-crop 490x102+235+150 +repage tmp1.png
Run Code Online (Sandbox Code Playgroud)
然后我运行他相当长的连续调整图像大小的序列,然后合并所有图层并将大小调整回原始大小。
convert tmp1.png \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
\( +clone -resize 90.9091% \) \
-layers RemoveDups \
-filter Gaussian -resize 490x102! \
-background None \
-compose DstOver -layers merge \
-alpha opaque \
tmp2.png
Run Code Online (Sandbox Code Playgroud)
最后,我将这个结果合成回小猫图像上我裁剪它的位置。
convert kitty.png tmp2.png -geometry +235+150 -compose over -composite kitty2.png
Run Code Online (Sandbox Code Playgroud)
在全分辨率下,您仍然可以看出该图像中残留的非常微弱的文本。通过快速交替两张图像可以看出,Skimage 结果更好。
| 归档时间: |
|
| 查看次数: |
11524 次 |
| 最近记录: |