LoginSignup
11
11

More than 5 years have passed since last update.

macでOpenCVで顔検出

Posted at

初めて投稿。
色々調べて、MacでOpenCVで顔検出するサンプル
OpenCVがHomebrewなどでインストールされている前提

test_face_detect.cpp
#include <opencv2/opencv.hpp>

int main (int argc, char **argv){
    cv::Mat frame, img, gray;
    cv::VideoCapture cap(0);
    const std::string cascade_name = "/usr/local/Cellar/opencv/2.4.4a/share/OpenCV/lbpcascades/lbpcascade_frontalface.xml";
    cv::CascadeClassifier cascade;

    cascade.load(cascade_name);
    while (cv::waitKey(1) != 'q') {
        cap >> frame;
        cv::cvtColor(frame, gray, CV_BGRA2GRAY);
        cv::equalizeHist (gray, gray);

        std::vector<cv::Rect> faces;
        cascade.detectMultiScale(gray, faces);

        for(std::vector<cv::Rect>::iterator it=faces.begin(); it!=faces.end(); it++){
            cv::rectangle(frame, *it, CV_RGB(255,0,0), 3);
        }

        cv::imshow("Face Detected", frame);
    }
    return 0;
}

コンパイルは下記みたいな感じ
$ clang++ test_face_detect.cpp -o FaceDetected `pkg-config --cflags --libs opencv`

で、実行
$ ./FaceDetected

参考資料

11
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
11