图像&视频编辑工具箱MMEditing使用示例:图像抠图(matting)
Posted fengbingchun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像&视频编辑工具箱MMEditing使用示例:图像抠图(matting)相关的知识,希望对你有一定的参考价值。
MMEditing的介绍及安装参考:https://blog.csdn.net/fengbingchun/article/details/126331541,这里给出图像抠图的测试代码,论文:《Indices Matter: Learning to Index for Deep Image Matting》:
(1).下载模型(checkpoint):
def download_checkpoint(path, name, url):
if os.path.isfile(path+name) == False:
print("checkpoint(model) file does not exist, now download ...")
subprocess.run(["wget", "-P", path, url])
path = "../../data/model/"
checkpoint = "indexnet_mobv2_1x16_78k_comp1k_SAD-45.6_20200618_173817-26dd258d.pth"
url = "https://download.openmmlab.com/mmediting/mattors/indexnet/indexnet_mobv2_1x16_78k_comp1k_SAD-45.6_20200618_173817-26dd258d.pth"
download_checkpoint(path, checkpoint, url)
(2).根据配置文件和checkpoint文件构建模型:
config = "../../src/mmediting//configs/mattors/indexnet/indexnet_mobv2_1x16_78k_comp1k.py"
model = init_model(config, path+checkpoint, device)
(3).准备测试图像:
image_path = "../../src/mmediting/tests/data/"
image_name = "merged/GT05.jpg"
trimap_name = "trimap/GT05.png"
每组需要2张,一张是待抠图的彩色图像;一张是三元图(trimap),如下图所示:源图来自于MMEditing
(4).进行推理抠图:
result = matting_inference(model, image, trimap) * 255
(5).显示执行结果及保存图像:
print(f"result shape: result.shape; max value: np.max(result)") # result shape: (552, 800); max value: 255.0
cv2.imwrite("../../data/result_matting_indexnet.jpg", result)
cv2.imshow("show", result)
cv2.waitKey(0)
结果图如下所示:
GitHub: https://github.com/fengbingchun/PyTorch_Test
以上是关于图像&视频编辑工具箱MMEditing使用示例:图像抠图(matting)的主要内容,如果未能解决你的问题,请参考以下文章
图像&视频编辑工具箱MMEditing使用示例:图像抠图(matting)
图像&视频编辑工具箱MMEditing使用示例:图像超分辨率(super-resolution)