如何从一张图像中自动检测特定特征并将其映射到另一张蒙版图像?那么如何只平滑图像的角落呢?
How to automatically detect a specific feature from one image and map it to another mask image? Then how to smoothen only the corners of the image?
使用 dlib
库,我能够从一张图像中屏蔽嘴部特征(屏蔽)。
蒙面
同样,我有另一张没有遮罩(colorlip)的嘴巴裁剪图像。
彩唇
我已经缩放并替换了图像(已替换)并使用 np.where
,如下面的代码所示。
已替换
#Get the values of the lip and the target mask
lip = pred_toblackscreen[bbox_lip[0]:bbox_lip[1], bbox_lip[2]:bbox_lip[3],:]
target = roi[bbox_mask[0]:bbox_mask[1], bbox_mask[2]:bbox_mask[3],:]
cv2.namedWindow('masked', cv2.WINDOW_NORMAL)
cv2.imshow('masked', target)
#Resize the lip to be the same scale/shape as the mask
lip_h, lip_w, _ = lip.shape
target_h, target_w, _ = target.shape
fy = target_h / lip_h
fx = target_w / lip_w
scaled_lip = cv2.resize(lip,(0,0),fx=fx,fy=fy)
cv2.namedWindow('colorlip', cv2.WINDOW_NORMAL)
cv2.imshow('colorlip', scaled_lip)
update = np.where(target==[0,0,0],scaled_lip,target)
cv2.namedWindow('replaced', cv2.WINDOW_NORMAL)
cv2.imshow('replaced', update)
但是 'colorlip' 中的特征形状(嘴唇)与 'masked' 图像不匹配。因此,存在错位并且蒙版的边缘看起来很清晰,就好像图像已被覆盖一样。如何解决这个问题呢?以及如何让最终替换后的图片看起来更加细腻和正常?
**更新 #2:OpenCV 图像修复以平滑锯齿状边界。
OpenCV python inpainting 应该有助于粗略的边界。使用嘴部界标模型,可以找到来自 DL 模型的嘴部分割掩码或任何使用边界位置的东西。从新图像中嘴部轮廓周围的小选定宽度绘制边框,并将其用作修复的蒙版。我提供的面具需要倒置才能工作。
在输入蒙版中,其中一个较宽,一个有阴影,最后一个较窄。生成的六个输出图像的半径值为 5
和 20
,用于所有三个蒙版。
代码
import numpy as np
# import cv2 as cv2
# import cv2
import cv2.cv2 as cv2
img = cv2.imread('images/lip_img.png')
#mask = cv2.imread('images/lip_img_border_mask.png',0)
mask = cv2.imread('images/lip_img_border_mask2.png',0)
#mask = cv2.imread('images/lip_img_border_mask3.png',0)
mask = np.invert(mask)
# Choose appropriate method and radius.
radius = 20
dst = cv2.inpaint(img, mask, radius, cv2.INPAINT_TELEA)
# dst = cv2.inpaint(img, mask, radius, cv2.INPAINT_NS)
cv2.imwrite('images/inpainted_lip.jpg', dst)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入图像和蒙版
输出图像
**更新 #1:添加了基于深度图像协调的混合方法。
在尖角处尝试 OpenCV seamless cloning for subtle replacement and getting rid of sharp edges. Also deep learning based image inpainting 或将其与无缝克隆结合使用可能会提供更好的结果。
Deep learning based Image Harmonization 可以是另一种将两个图像混合在一起的方法,使裁剪部分与背景图像的风格相匹配。即使在这种情况下,像素强度也会发生变化以匹配背景,但混合会更加平滑。链接添加到 post.
的底部
例子
本代码示例基于learnopencv无缝克隆示例,
# import cv2
from cv2 import cv2
import numpy as np
src = cv2.imread("images/src_img.jpg")
dst = cv2.imread("images/dest_img.jpg")
src_mask = cv2.imread("images/src_img_rough_mask.jpg")
src_mask = np.invert(src_mask)
cv2.namedWindow('src_mask', cv2.WINDOW_NORMAL)
cv2.imshow('src_mask', src_mask)
cv2.waitKey(0)
# Where to place image.
center = (500,500)
# Clone seamlessly.
output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
# Write result
cv2.imwrite("images/opencv-seamless-cloning-example.jpg", output)
cv2.namedWindow('output', cv2.WINDOW_NORMAL)
cv2.imshow('output', output)
cv2.waitKey(0)
源图片
粗糙蒙版图像
目标图片
最终图像
参考
- https://docs.opencv.org/4.5.4/df/da0/group__photo__clone.html
- https://learnopencv.com/seamless-cloning-using-opencv-python-cpp/
- https://learnopencv.com/face-swap-using-opencv-c-python/
- https://github.com/JiahuiYu/generative_inpainting
- https://docs.opencv.org/4.x/df/d3d/tutorial_py_inpainting.html
深度图像协调
- https://github.com/bcmi/Image-Harmonization-Dataset-iHarmony4
- https://github.com/wasidennis/DeepHarmonization
- https://github.com/saic-vul/image_harmonization
- https://github.com/wuhuikai/GP-GAN
- https://github.com/junleen/RainNet
- https://github.com/bcmi/BargainNet-Image-Harmonization
- https://github.com/vinthony/s2am
使用 dlib
库,我能够从一张图像中屏蔽嘴部特征(屏蔽)。
蒙面
同样,我有另一张没有遮罩(colorlip)的嘴巴裁剪图像。
彩唇
我已经缩放并替换了图像(已替换)并使用 np.where
,如下面的代码所示。
已替换
#Get the values of the lip and the target mask
lip = pred_toblackscreen[bbox_lip[0]:bbox_lip[1], bbox_lip[2]:bbox_lip[3],:]
target = roi[bbox_mask[0]:bbox_mask[1], bbox_mask[2]:bbox_mask[3],:]
cv2.namedWindow('masked', cv2.WINDOW_NORMAL)
cv2.imshow('masked', target)
#Resize the lip to be the same scale/shape as the mask
lip_h, lip_w, _ = lip.shape
target_h, target_w, _ = target.shape
fy = target_h / lip_h
fx = target_w / lip_w
scaled_lip = cv2.resize(lip,(0,0),fx=fx,fy=fy)
cv2.namedWindow('colorlip', cv2.WINDOW_NORMAL)
cv2.imshow('colorlip', scaled_lip)
update = np.where(target==[0,0,0],scaled_lip,target)
cv2.namedWindow('replaced', cv2.WINDOW_NORMAL)
cv2.imshow('replaced', update)
但是 'colorlip' 中的特征形状(嘴唇)与 'masked' 图像不匹配。因此,存在错位并且蒙版的边缘看起来很清晰,就好像图像已被覆盖一样。如何解决这个问题呢?以及如何让最终替换后的图片看起来更加细腻和正常?
**更新 #2:OpenCV 图像修复以平滑锯齿状边界。
OpenCV python inpainting 应该有助于粗略的边界。使用嘴部界标模型,可以找到来自 DL 模型的嘴部分割掩码或任何使用边界位置的东西。从新图像中嘴部轮廓周围的小选定宽度绘制边框,并将其用作修复的蒙版。我提供的面具需要倒置才能工作。
在输入蒙版中,其中一个较宽,一个有阴影,最后一个较窄。生成的六个输出图像的半径值为 5
和 20
,用于所有三个蒙版。
代码
import numpy as np
# import cv2 as cv2
# import cv2
import cv2.cv2 as cv2
img = cv2.imread('images/lip_img.png')
#mask = cv2.imread('images/lip_img_border_mask.png',0)
mask = cv2.imread('images/lip_img_border_mask2.png',0)
#mask = cv2.imread('images/lip_img_border_mask3.png',0)
mask = np.invert(mask)
# Choose appropriate method and radius.
radius = 20
dst = cv2.inpaint(img, mask, radius, cv2.INPAINT_TELEA)
# dst = cv2.inpaint(img, mask, radius, cv2.INPAINT_NS)
cv2.imwrite('images/inpainted_lip.jpg', dst)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入图像和蒙版
输出图像
**更新 #1:添加了基于深度图像协调的混合方法。
在尖角处尝试 OpenCV seamless cloning for subtle replacement and getting rid of sharp edges. Also deep learning based image inpainting 或将其与无缝克隆结合使用可能会提供更好的结果。
Deep learning based Image Harmonization 可以是另一种将两个图像混合在一起的方法,使裁剪部分与背景图像的风格相匹配。即使在这种情况下,像素强度也会发生变化以匹配背景,但混合会更加平滑。链接添加到 post.
的底部例子
本代码示例基于learnopencv无缝克隆示例,
# import cv2
from cv2 import cv2
import numpy as np
src = cv2.imread("images/src_img.jpg")
dst = cv2.imread("images/dest_img.jpg")
src_mask = cv2.imread("images/src_img_rough_mask.jpg")
src_mask = np.invert(src_mask)
cv2.namedWindow('src_mask', cv2.WINDOW_NORMAL)
cv2.imshow('src_mask', src_mask)
cv2.waitKey(0)
# Where to place image.
center = (500,500)
# Clone seamlessly.
output = cv2.seamlessClone(src, dst, src_mask, center, cv2.NORMAL_CLONE)
# Write result
cv2.imwrite("images/opencv-seamless-cloning-example.jpg", output)
cv2.namedWindow('output', cv2.WINDOW_NORMAL)
cv2.imshow('output', output)
cv2.waitKey(0)
源图片
粗糙蒙版图像
目标图片
最终图像
参考
- https://docs.opencv.org/4.5.4/df/da0/group__photo__clone.html
- https://learnopencv.com/seamless-cloning-using-opencv-python-cpp/
- https://learnopencv.com/face-swap-using-opencv-c-python/
- https://github.com/JiahuiYu/generative_inpainting
- https://docs.opencv.org/4.x/df/d3d/tutorial_py_inpainting.html
深度图像协调
- https://github.com/bcmi/Image-Harmonization-Dataset-iHarmony4
- https://github.com/wasidennis/DeepHarmonization
- https://github.com/saic-vul/image_harmonization
- https://github.com/wuhuikai/GP-GAN
- https://github.com/junleen/RainNet
- https://github.com/bcmi/BargainNet-Image-Harmonization
- https://github.com/vinthony/s2am