图像&视频编辑工具箱MMEditing使用示例:图像抠图(matting)

x33g5p2x  于2022-08-17 转载在 其他  
字(2.1k)|赞(0)|评价(0)|浏览(505)

    MMEditing的介绍及安装参考:https://blog.csdn.net/fengbingchun/article/details/126331541,这里给出图像抠图的测试代码,论文:《Indices Matter: Learning to Index for Deep Image Matting》:

    (1).下载模型(checkpoint):

  1. def download_checkpoint(path, name, url):
  2. if os.path.isfile(path+name) == False:
  3. print("checkpoint(model) file does not exist, now download ...")
  4. subprocess.run(["wget", "-P", path, url])
  5. path = "../../data/model/"
  6. checkpoint = "indexnet_mobv2_1x16_78k_comp1k_SAD-45.6_20200618_173817-26dd258d.pth"
  7. url = "https://download.openmmlab.com/mmediting/mattors/indexnet/indexnet_mobv2_1x16_78k_comp1k_SAD-45.6_20200618_173817-26dd258d.pth"
  8. download_checkpoint(path, checkpoint, url)

    (2).根据配置文件和checkpoint文件构建模型:

  1. config = "../../src/mmediting//configs/mattors/indexnet/indexnet_mobv2_1x16_78k_comp1k.py"
  2. model = init_model(config, path+checkpoint, device)

    (3).准备测试图像:

  1. image_path = "../../data/image/"
  2. image_name = "5.jpg"
  3. trimap_name = "5_trimap.png"

    每组需要2张,一张是待抠图的彩色图像;一张是三元图(trimap, 是对给定图像的一种粗略划分,即将给定图像划分为前景、背景和待求未知区域,每个像素取值为{0,128,255}其中之一,分别代表前景、未知与背景),如下图所示:源图来自于网络,三元图通过photoshop处理

    (4).进行推理抠图:

  1. result = matting_inference(model, image, trimap) * 255

    (5).显示执行结果及保存图像:

  1. print(f"result shape: {result.shape}; max value: {np.max(result)}") # result shape: (450, 617); max value: 255.0
  2. _, result = cv2.threshold(result, 254, 255, cv2.THRESH_BINARY)
  3. result = result.astype(np.uint8)
  4. cv2.imwrite("../../data/result_matting_indexnet.jpg", result)
  5. cv2.imshow("show_result", result)
  6. cv2.waitKey(0)
  7. mat1 = cv2.imread(image)
  8. mat3 = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
  9. mat3 = cv2.bitwise_and(mat1, mat3, result)
  10. # cv2.imshow("show_mat3", mat3)
  11. # cv2.waitKey(0)
  12. _, mat4 = cv2.threshold(result, 254, 255, cv2.THRESH_BINARY_INV)
  13. mat4 = cv2.cvtColor(mat4, cv2.COLOR_GRAY2BGR)
  14. mat4 = mat4.astype(np.uint8)
  15. mat2 = np.zeros(mat1.shape, dtype=np.uint8)
  16. mat2[:,:] = (0, 255, 0)
  17. mat2 = cv2.bitwise_and(mat2, mat4)
  18. mat2 = mat3 + mat2
  19. cv2.imwrite("../../data/result_matting_indexnet_bgr.jpg", mat2)
  20. cv2.imshow("show_mat2", mat2)
  21. cv2.waitKey(0)

    结果图如下所示:左图为推理结果

**      GitHub**: https://github.com/fengbingchun/PyTorch_Test

相关文章