Opencv2.2 Python 人脸检测

Python确实用起来要方便很多

我觉得最多的好处就是库的加入要方便很多,不需要想VS那样设置很多东西。

今天用Opencv2.2 写了一个人脸检测的程序:

import os
import sys
 
import cv
import cv2

from cv import *
import numpy as np
import numpy

def detect(image):
    image_size = cv.GetSize(image)
    
    # create grayscale version
    gray = cv.CreateImage((image.width, image.height), 8, 1)
    
    cv.CvtColor(image, gray, cv.CV_BGR2GRAY)
    
    # create storage
    storage = cv.CreateMemStorage(0)
    #cv.ClearMemStorage(storage)
    
    # equalize histogram
    cv.EqualizeHist(gray, gray)
    
    # detect objects
    cascade = cv.Load('haarcascade_frontalface_alt.xml')
    faces = cv.HaarDetectObjects(gray, cascade, storage, 1.2, 2, 0, (50, 50))
    
    if faces:
        for ((x, y, w, h), n) in faces:
            pt1 = (int(x), int(y))
            pt2 = (int((x + w)), int((y + h)))
            cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
            cv.SetImageROI(image, (pt1[0],pt1[1],pt2[0] - pt1[0],int((pt2[1] - pt1[1]))))
#            surf = cv2.SURF()
#            keys,desc = surf.detect(image,None,useProvidedKeypoints = False)
            

image = cv.LoadImage('woman2.bmp');
detect(image)
cv.SaveImage("face.bmp", image)

cv.ShowImage("Intilization Face", image)
cv.waitKey(0)

程序必须要用opencv2.2 Python2.7

这个大家要注意,因为我发现opencv2.1 2.2 2.4 中

很多函数都有变化。调用也都不太一样

你可能感兴趣的:(Python,OpenCv)