python - opencv haar文件返回太多面部特征

标签 python opencv opencv3.0 haar-classifier

我正在尝试检测Elon Musks的面部特征。现在它已经可以,但是我检测到太多的眼睛, Nose 和嘴巴特征。我不确定如何解决此问题,以便只找到一套。

我正在使用opencv的github提供的haar文件,对于 Nose 和嘴巴,我在网上某个地方找到了一些haar文件。

haarcascade_frontalface_default.xml
haarcascade_eye.xml

Haar文件
class Filterize(object):
    def __init__(self, filterpath):
        self.filterpath = filterpath
        self.haarpath = os.path.join(os.getcwd(), 'haar')
        self.face_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'face.xml'))
        self.eye_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'eye.xml'))
        self.nose_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'nose.xml'))
        self.mouth_cascade = cv2.CascadeClassifier(os.path.join(self.haarpath, 'mouth.xml'))

    def get_filter_facial_features(self):
        filter = cv2.imread(self.filterpath)
        grayscale_filter = cv2.cvtColor(filter, cv2.COLOR_BGR2GRAY)
        face = self.face_cascade.detectMultiScale(grayscale_filter, 1.3, 5)
        for x, y, w, h in face:
            cv2.rectangle(filter, (x, y), (x + w, y + h), (255, 0, 0), 2)
            roi_gray = grayscale_filter[y:y + h, x:x + w]
            roi_color = filter[y:y + h, x:x + w]
            eyes = self.eye_cascade.detectMultiScale(roi_gray, 1.3, 5)
            nose = self.nose_cascade.detectMultiScale(roi_gray, 1.3, 5)
            mouth = self.mouth_cascade.detectMultiScale(roi_gray, 1.3, 5)
            for eye_x, eye_y, eye_w, eye_h in eyes:
                cv2.rectangle(roi_color, (eye_x, eye_y), (eye_x + eye_w, eye_y + eye_h), (0, 255, 0), 2)
            for nose_x, nose_y, nose_w, nose_h in nose:
                cv2.rectangle(roi_color, (nose_x, nose_y), (nose_x + nose_w, nose_y + nose_h), (0, 255, 0), 2)
            for mouth_x, mouth_y, mouth_w, mouth_h in mouth:
                cv2.rectangle(roi_color, (mouth_x, mouth_y), (mouth_x + mouth_w, mouth_y + mouth_h), (0, 255, 0), 2)
            cv2.imwrite('face.png', filter)

创建照片:
a = Filterize(filterpath)
a.get_filter_facial_features()

enter image description here

最佳答案

在此行中:

face = self.face_cascade.detectMultiScale(grayscale_filter, 1.3, 5)

您传递以下可用参数(取自the docs):

Parameters:

  • cascade – Haar classifier cascade (OpenCV 1.x API only). It can be loaded from XML or YAML file using Load(). When the cascade is not
    needed anymore, release it using
    cvReleaseHaarClassifierCascade(&cascade).
  • image – Matrix of the type CV_8U containing an image where objects are detected. objects – Vector of rectangles where each rectangle contains the detected object.
  • scaleFactor – Parameter specifying how much the image size is reduced at each image scale.
  • minNeighbors – Parameter specifying how many neighbors each candidate rectangle should have to retain it.

  • flags – Parameter with the same meaning for an old cascade as in the function cvHaarDetectObjects. It is not used for a new cascade.

  • minSize – Minimum possible object size. Objects smaller than that are ignored.

  • maxSize – Maximum possible object size. Objects larger than that are ignored.


该功能的作用是检测具有定义边界的所有功能。我建议您需要使用这些值,直到矩形的数量达到可接受的数量为止。

实际上,maxSize似乎是一个不错的开始,因为每次检测都有较小的矩形

关于python - opencv haar文件返回太多面部特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47468222/

相关文章:

python - 使用动态版本的 Python 执行嵌入的 Python 代码时出现致命的 Python 错误

image-processing - 如何处理图像中的浮点坐标值

c++ - 如何找出两个图像之间的对象差异?

OpenCv 无法在我的许多图像上找到棋盘图案,而 Matlab 则不能

java - Opencv中的嘴巴检测在android中检测多个区域

python-2.7 - 使用 Open CV Python 在 Microsoft Kinect 的深度图像中分割区域

python - 使用正则表达式搜索重复结构

具有增强赋值的 Python 运算符优先级

python - 在带有嵌套字典的 Python 中,我应该检查类型吗?我还可以使用什么其他方法?

python - 在特定版本的 OpenCV 中使用 python