opencv-python(三)

马赛克

     face = img[162:428,297:527]  # 人脸坐标区域
    face = face[::10,::10]  # 每10个中取出一个像素,马赛克
    face = np.repeat(face, 10, axis=0) # 行方向重复10次
    face = np.repeat(face, 10, axis=1) # 列方向重复10次
    img[162:428,297:527] = face[:266,:230] # 填充,保持尺寸一致
    cv2.imshow('bao', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

人脸识别

人脸特征参数下载地址

opencv/data/haarcascades at 4.x · opencv/opencv · GitHub

    # 人脸特征详细说明,1万多行,计算机根据这些特征,进行人脸检测
    # 符合其中一部分说明,算作人脸
    # 级联分类器、检测器
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(img) # 坐标x,y,w,h
    for x,y,w,h in faces:
        cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h), color=[0,0,255],thickness=2) # 矩形
    cv2.imshow('face', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

多人脸

 调整参数

    gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(gray,
                                           scaleFactor=1.2, # 缩放
                                           minNeighbors=3) # 坐标x,y,w,h
    for x,y,w,h in faces:
        cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h), color=[0,0,255],thickness=2) # 矩形
    cv2.imshow('face', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(gray,
                                           scaleFactor=1.2, # 缩放
                                           minNeighbors=3, minSize=(40,40)) # 设置检测框大小,过滤太小的框
    for x,y,w,h in faces:
        # cv2.rectangle(img,pt1=(x,y),pt2=(x+w,y+h), color=[0,0,255],thickness=2) # 矩形
        cv2.circle(img, center=(x+w//2, y+h//2),
                   radius=w//2,
                   color=[0, 255, 0]) # 圆形
    cv2.imshow('face', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

人脸贴纸

    gray = cv2.cvtColor(img, code=cv2.COLOR_BGR2GRAY)
    face_detector = cv2.CascadeClassifier('./haarcascade_frontalface_alt.xml')
    faces = face_detector.detectMultiScale(gray) # 设置检测框大小,过滤太小的框
    star = cv2.imread('./star.png')
    for x,y,w,h in faces:
        star_s = cv2.resize(star, (w//4, h//4))
        w1 = w//4
        h1 = h//4
        for i in range(h1):
            for j in range(w1):  # 遍历图片数据
                if not (star_s[i,j] > 200).all():  # 红色
                    img[i+y,j+x+3*w//8] = star_s[i,j]

    cv2.imshow('face', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值