11
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OpenCV3で画像キャプチャして顔検出してモザイク処理してから表示

Last updated at Posted at 2015-11-14

http://qiita.com/yohhoy/items/313da1bbddb782fd3d7f で自分の顔がいきなり映ったときの精神的ダメージの軽減をもとめて。

facemoz.cpp
# include <vector>
# include <string>
# include <iostream>
# include <opencv2/opencv.hpp>


cv::CascadeClassifier classifier;
const char * const file = "./haarcascade.xml";
cv::Size min_face;

//! initialize
void init(int w, int h)
{
    std::cout << "frame=" << w << 'x' << h << std::endl;

    if (!classifier.load(file)) {
        std::cout << "fail to load: " << file << std::endl;
        std::exit(1);
    }
    const double scale = 0.10;
    min_face = cv::Size(w * scale, h * scale);
    std::cout << "min_face=" << min_face << std::endl;
}

//! process image frame
cv::Mat process(cv::Mat& frame)
{
    std::vector<cv::Rect> faces;
    classifier.detectMultiScale(frame, faces, 1.1, 3, 0, min_face);
    for (const auto& r : faces) {
        // mosaic filter
        const double f = 0.05;
        cv::Mat tmp;
        cv::resize(frame(r), tmp, {}, f, f);
        cv::resize(tmp, frame(r), r.size(), 0, 0, cv::INTER_NEAREST);
        // draw area border
        cv::rectangle(frame, r.tl(), r.br(), {0,0,0});
    }
    return frame;
}

int main()
{
    const char * const window = "Capture (Press ESC to exit)";
    const double scale = 0.75;
    const int delay = 32;   // [msec]

    cv::VideoCapture cap(0);
    if (!cap.isOpened()) {
        std::cerr << "fail to open cv::VideoCapture" << std::endl;
        return 2; 
    }

    const double width = cap.get(cv::CAP_PROP_FRAME_WIDTH) * scale;
    const double height = cap.get(cv::CAP_PROP_FRAME_HEIGHT) * scale;
    cap.set(cv::CAP_PROP_FRAME_WIDTH, width);
    cap.set(cv::CAP_PROP_FRAME_HEIGHT, height);
    init(width, height);

    cv::namedWindow(window);
    cv::Mat frame;
    do {
        cap >> frame;
        cv::imshow(window, process(frame));
    } while (cv::waitKey(delay) != '\x1b');

    return 0;
}
$ brew install science/opencv3
...
$ clang++ -std=c++14 -W -Wall -O2 -o facemoz facemoz.cpp -I/usr/local/opt/opencv3/include -L/usr/local/opt/opencv3/lib -lopencv_core -lopencv_highgui -lopencv_videoio -lopencv_objdetect -lopencv_imgproc
$ cp /usr/local/opt/opencv3/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml haarsascade.xml
$ ./facemoz
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?