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 = "../../data/image/"
image_name = "5.jpg"
trimap_name = "5_trimap.png"
每组需要2张,一张是待抠图的彩色图像;一张是三元图(trimap, 是对给定图像的一种粗略划分,即将给定图像划分为前景、背景和待求未知区域,每个像素取值为{0,128,255}其中之一,分别代表前景、未知与背景),如下图所示:源图来自于网络,三元图通过photoshop处理
(4).进行推理抠图:
result = matting_inference(model, image, trimap) * 255
(5).显示执行结果及保存图像:
print(f"result shape: {result.shape}; max value: {np.max(result)}") # result shape: (450, 617); max value: 255.0
_, result = cv2.threshold(result, 254, 255, cv2.THRESH_BINARY)
result = result.astype(np.uint8)
cv2.imwrite("../../data/result_matting_indexnet.jpg", result)
cv2.imshow("show_result", result)
cv2.waitKey(0)
mat1 = cv2.imread(image)
mat3 = cv2.cvtColor(result, cv2.COLOR_GRAY2BGR)
mat3 = cv2.bitwise_and(mat1, mat3, result)
# cv2.imshow("show_mat3", mat3)
# cv2.waitKey(0)
_, mat4 = cv2.threshold(result, 254, 255, cv2.THRESH_BINARY_INV)
mat4 = cv2.cvtColor(mat4, cv2.COLOR_GRAY2BGR)
mat4 = mat4.astype(np.uint8)
mat2 = np.zeros(mat1.shape, dtype=np.uint8)
mat2[:,:] = (0, 255, 0)
mat2 = cv2.bitwise_and(mat2, mat4)
mat2 = mat3 + mat2
cv2.imwrite("../../data/result_matting_indexnet_bgr.jpg", mat2)
cv2.imshow("show_mat2", mat2)
cv2.waitKey(0)
结果图如下所示:左图为推理结果
** GitHub**: https://github.com/fengbingchun/PyTorch_Test
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/fengbingchun/article/details/126332219
内容来源于网络,如有侵权,请联系作者删除!