先读取图片,之后进行灰度处理,
除4得到小号五角星,将五角星白色部分隔离,
白色条件大于200(这里)
import cv2
if __name__ == '__main__':
img=cv2.imread('./1927su.jpg')
gray=cv2.cvtColor(img,code=cv2.COLOR_BGR2GRAY)
face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
faces=face_detector.detectMultiScale(gray,scaleFactor=1.05,minNeighbors=4)
star= cv2.imread('./star.jpg')
for x,y,w,h in faces:
star_s=cv2.resize(star,(w//4,h//4)) #小号五角星
h1=h//4
w1=w//4
d=3*w//8
for i in range(h1):
for j in range(w1):
if not (star_s[i,j]>200).all():#白色
img[i+y,j+x+d]=star_s[i,j]
cv2.imshow('face',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
多人的人脸图片贴图也不是问题
import cv2
if __name__ == '__main__':
img=cv2.imread('./123.jpg')
gray=cv2.cvtColor(img,code=cv2.COLOR_BGR2GRAY)
face_detector=cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
faces=face_detector.detectMultiScale(gray,scaleFactor=1.05,minNeighbors=4)
star= cv2.imread('./star.jpg')
for x,y,w,h in faces:
star_s=cv2.resize(star,(w//4,h//4)) #小号五角星
h1=h//4
w1=w//4
d=3*w//8
for i in range(h1):
for j in range(w1):
if not (star_s[i,j]>200).all():#白色
img[i+y,j+x+d]=star_s[i,j]
cv2.imshow('face',img)
cv2.waitKey(0)
cv2.destroyAllWindows()