简单人像背景虚化处理思路如下:
对图像内容分割,提取人像,背景
背景模糊处理
人像与模糊处理后的背景融合
本实例使用DeepLabV3图像分割深度学习模型实现。代码如下:
import numpy as np
import tensorflow as tf
import cv2
from deeplabmodel import *
def create_pascal_label_colormap():
colormap = np.zeros((256, 3), dtype=int)
ind = np.arange(256, dtype=int)
for shift in reversed(range(8)):
for channel in range(3):
colormap[:, channel] |= ((ind >> channel) & 1) << shift
ind >>= 3
return colormap
def label_to_color_image(label):
if label.ndim != 2:
raise ValueError('Expect 2-D input label')
colormap = create_pascal_label_colormap()
if np.max(label) >= len(colormap):
raise ValueError('label value too large.')
return colormap[label