参考1参考2
一 生成三分图trimap
import random
import numpy as np
import cv2
def gen_trimap(alpha):
k_size = random.choice(range(1, 5))
iterations = np.random.randint(1, 20)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (k_size, k_size))
dilated = cv2.dilate(alpha, kernel, iterations)
eroded = cv2.erode(alpha, kernel, iterations)
trimap = np.zeros(alpha.shape)
trimap.fill(128)
trimap[eroded >= 255] = 255
trimap[dilated <= 0] = 0
return trimap
if __name__ == "__main__":
alpha = xxxx
assert (np.unique(alpha_np) == [0, 255]).any()
alpha = alpha * 255
gen_trimap(alpha)