ios--OpenCV--人脸识别核心代码

来源:互联网 发布:ubuntu 17.04 wine qq 编辑:程序博客网 时间:2024/05/17 22:40

内容来自苹果官方示例:

    // Load cascade classifier from the XML file    NSString* cascadePath = [[NSBundle mainBundle]                     pathForResource:@"haarcascade_frontalface_alt"                              ofType:@"xml"];    faceDetector.load([cascadePath UTF8String]);        //Load image with face    UIImage* image = [UIImage imageNamed:@"测试图片.jpg"];    cv::Mat faceImage;    UIImageToMat(image, faceImage);        // Convert to grayscale    cv::Mat gray;    cvtColor(faceImage, gray, CV_BGR2GRAY);        // Detect faces    std::vector<cv::Rect> faces;    faceDetector.detectMultiScale(gray, faces, 1.1,                                 2, 0|CV_HAAR_SCALE_IMAGE, cv::Size(30, 30));        // Draw all detected faces    for(unsigned int i = 0; i < faces.size(); i++)    {        const cv::Rect& face = faces[i];        // Get top-left and bottom-right corner points        cv::Point tl(face.x, face.y);        cv::Point br = tl + cv::Point(face.width, face.height);                // Draw rectangle around the face        cv::Scalar magenta = cv::Scalar(255, 0, 255);        cv::rectangle(faceImage, tl, br, magenta, 4, 8, 0);    }        // Show resulting image    imageView.image = MatToUIImage(faceImage);


0 0