前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OpenCV 人脸检测(一)

OpenCV 人脸检测(一)

作者头像
用户6021899
发布2019-09-25 10:56:14
1.7K0
发布2019-09-25 10:56:14
举报

OpenCV的Haar级联分类器可以通过对比分析相邻图像区域的特征来判断给定图像或子图像与已知对象是否匹配,从而给图像进行分类。提取图像的细节对产生稳定可靠的分类结果很有用。这些提取的结果被称为特征。特征的数量通常应该比像素数少得多。两个图像的相似程度可以通过它们的特征向量的距离来计算。

OpenCV的Haar级联分类器具有尺度不变型(通过循环缩放图像再进行特征比对来实现),即它在尺度缩放上具有鲁棒性。但是,它不具备旋转不变形。例如,Haar级联分离器认为倒置的人脸图像和正立的人脸图像不一样,且认为侧面的人脸图像和正面的人脸图像也不一样。

在OpenCV的源代码的副本中会有一个文件夹 \sources\data\haarcascades。该文件夹包含了所有OpenCV的人脸检测的XML文件,这些文件可用于检测静止图像、视频和摄像头所得到的图像中的人脸。

假设我们已将上述文件夹都拷贝到了项目文件夹中。下面的例子我们来检测静止图像中人脸,视频帧流中人脸检测的方法也大致一样。

代码语言:javascript
复制
import cv2
import numpy as np
from matplotlib import pyplot as plt


def detect(filename):
    img = cv2.imread(filename)
    gray =cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    #检测正脸
    front_face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_frontalface_default.xml')
    faces0 = front_face_cascade.detectMultiScale(gray, 1.022, 5)
    print("共检测到%d张人的正脸" %len(faces0))
    
    #检测侧脸
    profile_face_cascade = cv2.CascadeClassifier('./cascades/haarcascade_profileface.xml')
    faces1 = profile_face_cascade.detectMultiScale(gray, 1.2, 6)
    print("共检测到%d张人的侧脸" %len(faces1))
   
    for (x, y, w, h) in faces0:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 1) #画红色矩形框标记正脸
    for (x, y, w, h) in faces1:
        cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 1) #画绿色矩形框标记侧脸
    return img

img = detect("physicists.jpg")
plt.subplot(1,1,1)
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.title("OpenCV 人脸检测",fontSize =16, color="b")
plt.show()

检测的结果如下(图中不看镜头的那位大牛是发表“泡利不相容”原理的泡利,被检测出了侧脸):

附上detectMultiScale函数的参数说明:

代码语言:javascript
复制
'''
detectMultiScale(...) method of cv2.CascadeClassifier instance
    detectMultiScale(image[, scaleFactor[, minNeighbors[, flags[, minSize[, maxSize]]]]]) -> objects
    .   @brief Detects objects of different sizes in the input image. The detected objects are returned as a list
    .   of rectangles.
    .  
    .   @param image Matrix of the type CV_8U containing an image where objects are detected.
    .   @param objects Vector of rectangles where each rectangle contains the detected object, the
    .   rectangles may be partially outside the original image.
    .   @param scaleFactor Parameter specifying how much the image size is reduced at each image scale.
    .   @param minNeighbors Parameter specifying how many neighbors each candidate rectangle should have
    .   to retain it.
    .   @param flags Parameter with the same meaning for an old cascade as in the function
    .   cvHaarDetectObjects. It is not used for a new cascade.
    .   @param minSize Minimum possible object size. Objects smaller than that are ignored.
    .   @param maxSize Maximum possible object size. Objects larger than that are ignored. If `maxSize == minSize` model is evaluated on single scale.
    .  
    .   The function is parallelized with the TBB library.
    .  
    .   @note
    .   -   (Python) A face detection example using cascade classifiers can be found at
    .   opencv_source_code/samples/python/facedetect.py
'''
  • scaleFactor是每次迭代的缩放比例,越小(比1大)越可能检测到更多的人脸,但更可能重复。
  • minNeighbors 是每个人脸矩形保留尽量数目的最小值,整数。越小越可能检测到更多的人脸。
  • minSize 和maxSize 可以加入尺寸过滤。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-22,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Python可视化编程机器学习OpenCV 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
人脸识别
腾讯云神图·人脸识别(Face Recognition)基于腾讯优图强大的面部分析技术,提供包括人脸检测与分析、比对、搜索、验证、五官定位、活体检测等多种功能,为开发者和企业提供高性能高可用的人脸识别服务。 可应用于在线娱乐、在线身份认证等多种应用场景,充分满足各行业客户的人脸属性识别及用户身份确认等需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档