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)
结果图如下所示:
