import random
import math
import numpy as np
import cv2
# 标记方式有Trimap和Strokes两种的静态图像抠图算法
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
cv2.namedWindow('gaus2', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('gaus2',trimap)
cv2.waitKey(0)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/trimap.jpg", trimap)
return trimap
def composite4(fg, bg, a, w, h):
fg = np.array(fg, np.float32)
bg_h, bg_w = bg.shape[:2]
x = 0
if bg_w > w:
x = np.random.randint(0, bg_w - w)
y = 0
if bg_h > h:
y = np.random.randint(0, bg_h - h)
bg = np.array(bg[y:y + h, x:x + w], np.float32)
alpha = np.zeros((h, w, 1), np.float32)
alpha[:, :, 0] = a / 255.
im = alpha * fg + (1 - alpha) * bg
im = im.astype(np.uint8)
return im, a, fg, bg
def process(fg_file, alpha_file, bg_file):
# 备注
# https://www.aiuai.cn/aifarm1569.html?replyTo=1690
# 输入的图像格式不对。。。前两张一样的图片大小,第三张合成。背景合成--只能加背景图片三合一的改进。
im = cv2.imread(fg_file)
a = cv2.imread(alpha_file, 0) #变成原始的灰度图
# cv2.namedWindow('alphaa', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
# cv2.imshow('alphaa', a)
# cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alphaa.jpg", a)
h, w = im.shape[:2]
print("im_shape==", im.shape)
# print("a=",a)
# print("h=",h)
# print("w=",w)
bg = cv2.imread(bg_file)
bh, bw = bg.shape[:2]
print("bg_shape==", bg.shape)
# print("bh=",bh)
# print("bw=",bw)
wratio = w / bw
hratio = h / bh
print("wratio=",wratio)
print("hratio=",hratio)
ratio = wratio if wratio > hratio else hratio
if ratio > 1:
bg = cv2.resize(src=bg, dsize=(math.ceil(bw * ratio), math.ceil(bh * ratio)), interpolation=cv2.INTER_CUBIC)
return composite4(im, bg, a, w, h)
if __name__ == "__main__":
#
fg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/37.tiff'
alpha_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/35.tiff'
bg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/3.jpg'
# fg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/001.jpg'
# alpha_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/002.jpg'
# bg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/3.jpg'
# fg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/006.jpg'
# alpha_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/005.jpg'
# bg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/2.jpg'
img, alpha, fg, bg = process(fg_file, alpha_file, bg_file)
cv2.namedWindow('alpha0', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha0',img)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha0.jpg", img)
cv2.namedWindow('alpha1', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha1',alpha)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha1.jpg", alpha)
cv2.namedWindow('alpha2', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha2', fg)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha2.jpg", fg)
cv2.namedWindow('alpha3', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha3', bg)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha3.jpg", bg)
# print("alpha=",alpha)
# print("fg=",fg)
# print("bg=",bg)
# print(bg)
# print("fg==",len(fg))
# print("bg==", len(bg))
# print("fg_size==",fg.size)
# print("bg_size==", bg.size)
print("fg_shape==",fg.shape)
print("bg_shape==", bg.shape)
# 阿尔法通道是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度信息,定义透明、不透明和半透明区域,
# 其中黑表示透明,白表示不透明,灰表示半透明。https://zhidao.baidu.com/question/1817884346481655668.html
# trimap
trimap = gen_trimap(alpha)
#
# alpha = xxxx
# assert (np.unique(alpha_np) == [0, 255]).any()
# alpha = alpha * 255
# gen_trimap(alpha)
#
# 翻转
if np.random.random_sample() > 0.5:
img = np.fliplr(img)
trimap = np.fliplr(trimap)
alpha = np.fliplr(alpha)
精简版实现:
import random
import math
import numpy as np
import cv2
# 标记方式有Trimap和Strokes两种的静态图像抠图算法
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
cv2.namedWindow('gaus2', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('gaus2',trimap)
cv2.waitKey(0)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/trimap.jpg", trimap)
return trimap
def process(fg_file, alpha_file, bg_file):
im = cv2.imread(fg_file)
a = cv2.imread(alpha_file, 0) #变成原始的灰度图
h, w = im.shape[:2]
print("im_shape==", im.shape)
bg = cv2.imread(bg_file)
bh, bw = bg.shape[:2]
print("bg_shape==", bg.shape)
wratio = w / bw
hratio = h / bh
print("wratio=",wratio)
print("hratio=",hratio)
ratio = wratio if wratio > hratio else hratio
if ratio > 1:
bg = cv2.resize(src=bg, dsize=(math.ceil(bw * ratio), math.ceil(bh * ratio)), interpolation=cv2.INTER_CUBIC)
return composite4(im, bg, a, w, h)
if __name__ == "__main__":
fg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/37.tiff'
alpha_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/35.tiff'
bg_file = 'D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/3.jpg'
img, alpha, fg, bg = process(fg_file, alpha_file, bg_file)
cv2.namedWindow('alpha0', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha0',img)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha0.jpg", img)
cv2.namedWindow('alpha1', cv2.WINDOW_NORMAL) # 设置为WINDOW_NORMAL可以任意缩放
cv2.imshow('alpha1',alpha)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha1.jpg", alpha)
cv2.namedWindow('alpha2', cv2.WINDOW_NORMAL)
cv2.imshow('alpha2', fg)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha2.jpg", fg)
cv2.namedWindow('alpha3', cv2.WINDOW_NORMAL)
cv2.imshow('alpha3', bg)
cv2.imwrite("D:/Pictures/nbnb11/gaussian/gaussian_11tower/data01/alpha3.jpg", bg)
print("fg_shape==",fg.shape)
print("bg_shape==", bg.shape)
效果如图