因为最近在做深度学习抠图,正好要用到蒙版进行抠图,所以我将抠图代码进行了封装注释,可以直接使用。可能走了弯路,若有高见请一定提出!
主要代码
import cv2
from PIL import Image
import numpy as np
class UnsupportedFormat(Exception):
def __init__(self, input_type):
self.t = input_type
def __str__(self):
return "不支持'{}'模式的转换,请使用为图片地址(path)、PIL.Image(pil)或OpenCV(cv2)模式".format(self.t)
class MatteMatting():
def __init__(self, original_graph, mask_graph, input_type='path'):
"""
将输入的图片经过蒙版转化为透明图构造函数
:param original_graph:输入的图片地址、PIL格式、CV2格式
:param mask_graph:蒙版的图片地址、PIL格式、CV2格式
:param input_type:输入的类型,有path:图片地址、pil:pil类型、cv2类型
"""
if input_type == 'path':
self.img1 = cv2.imread(original_graph)
self.img2 = cv2.im