1.用opencv读取人脸图片和贴纸图片
import cv2 import numpy as np import matplotlib.pyplot as plt img = r"C:/Users/Curry/Desktop/winggs.png" img = cv2.imread(img) flag = r"C:/Users/Curry/Desktop/flag.png" flag = cv2.imread(flag) # cv2.imshow("flag",flag) # print(img.shape) # print(flag.shape)
2.与人脸检测一样用特征匹配人脸,获取人脸坐标
'''CascadeClassifier,是Opencv中做人脸检测的时候的一个级联分类器。并且既可以使用Haar,也可以使用LBP特征,检测出来的是X,Y,W,H''' renlian_path = r"C:/Users/Curry/Desktop/cascades/haarcascade_frontalface_default.xml" face_det = cv2.CascadeClassifier(renlian_path) faces = face_det.detectMultiScale(img)
3.在循环中遍历人脸x,y,w,h,将贴纸大小resize成人脸大小,在放缩,并且移动至额头中间
# img = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) for x,y,w,h in faces: # cv2.rectangle(img,pt1 = (x,y), pt2 = (x+w,y+h), color = [0,0,255],thickness=2) img[y:y+h//4,x+(3*w)//8-5:x+(w*3//8)-5 +w//4] = cv2.resize(flag,(w//4,h//4))
4.效果展示
cv2.imshow('winngs',img) cv2.waitKey(0) cv2.destroyAllWindows()
OpenCv人脸贴画
最新推荐文章于 2023-02-09 21:55:40 发布