opencv图像融合(给人脸添加一个眼镜)

时间:2022-02-11 01:55:40

基于dlib68点人脸检测的小功能实现

图像旋转找的现成的方法,稍稍麻烦点的地方就是mask处理,虽然目的达到了,但是效果一般

 import numpy as np
import cv2 as cv
import dlib
import math # 做一个戴眼镜的滤镜效果 detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('dlib/shape_predictor_68_face_landmarks.dat') # 图像旋转,保持原来大小
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w // 2, h // 2) # grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv.getRotationMatrix2D((cX, cY), -angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1]) # compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin)) # adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY # perform the actual rotation and return the image
return cv.warpAffine(image, M, (nW, nH)) def detect_face(camera_idx):
# camera_idx: 电脑自带摄像头或者usb摄像头
cv.namedWindow('detect')
cap = cv.VideoCapture(camera_idx) while cap.isOpened():
cv.namedWindow('detect', cv.WINDOW_AUTOSIZE)
ok, frame = cap.read()
# 为摄像头的时候,翻转画面
if camera_idx == 0 or camera_idx == 1:
frame = cv.flip(frame, 1, dst=None)
if not ok:
break
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
rects = detector(gray, 0)
for i in range(len(rects)):
landmarks = np.matrix([[p.x, p.y] for p in predictor(frame, rects[i]).parts()])
# 脸轮廓:1~17
# 眉毛:18~22, 23~27
# 鼻梁:28~31
# 鼻子:31~36
# 眼睛:37~42, 43~48
# 嘴唇:49~68
# 左眼角和右眼角的位置
pos_left = (landmarks[0][0, 0], landmarks[36][0, 1])
pos_right = (landmarks[16][0, 0], landmarks[45][0, 1])
face_center = (landmarks[27][0, 0], landmarks[27][0, 1])
src = cv.imread('images/glasses.jpg')
# 433x187眼镜图片原始大小,按人脸比例缩放一下
length = pos_right[0] - pos_left[0]
width = int(187/(433/length))
src = cv.resize(src, (length, width), interpolation=cv.INTER_CUBIC) # 角度旋转,通过计算两个眼角和水平方向的夹角来旋转眼镜
sx = landmarks[36][0, 0] - landmarks[45][0, 0]
sy = landmarks[36][0, 1] - landmarks[45][0, 1]
# 夹角正切值
r = sy/sx
# 求正切角,弧度转为度
degree = math.degrees(math.atan(r))
# 调用旋转方法
src = rotate_bound(src, degree) # mask处理,去掉旋转后的无关区域,初始化一个全0mask,用或运算处理mask
src_mask = np.zeros(src.shape, src.dtype)
src_mask = cv.bitwise_or(src, src_mask)
# 泊松融合
output = cv.seamlessClone(src, frame, src_mask, face_center, cv.MIXED_CLONE)
cv.imshow('detect', output)
c = cv.waitKey(10)
if c & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows() if __name__ == '__main__':
video = 'video/face.mp4'
detect_face(video)

眼镜图片

opencv图像融合(给人脸添加一个眼镜)

效果

opencv图像融合(给人脸添加一个眼镜)

泊松融合三种参数效果在这里一样

除了眼镜图片较浅其他的还算可以吧

还可以扩展面部其他装饰

参考:

泊松融合:https://www.smwenku.com/a/5b7aec012b7177392c97200d

图像旋转:https://blog.csdn.net/hui3909/article/details/78854387