Skip to content

Instantly share code, notes, and snippets.

@mohemohe
Created February 20, 2016 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohemohe/ce870f9d83bb27c91e6f to your computer and use it in GitHub Desktop.
Save mohemohe/ce870f9d83bb27c91e6f to your computer and use it in GitHub Desktop.
アニメ顔検出数 - require https://github.com/nagadomi/lbpcascade_animeface
// AnimeFaceDetect.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include "stdafx.h"
using namespace std;
using namespace cv;
const string CLASSIFIERFILENAME = "lbpcascade_animeface.xml";
CascadeClassifier cascadeClassifier;
wstring s2ws(const std::string& s);
int main(int argc, char *argv[])
{
if (argc == 1) {
cout << "引数で画像のパスを指定してください" << endl;
return -1;
}
wstring wsClassifierPath = s2ws(CLASSIFIERFILENAME);
LPCWSTR wcClassifierPath = wsClassifierPath.c_str();
if (!PathFileExists(wcClassifierPath)) {
cout << "顔検出器がカレントディレクトリに存在しません" << endl;
return -1;
}
string imagePath = argv[1];
wstring wsImagePath = s2ws(imagePath);
LPCWSTR wcImagePath = wsImagePath.c_str();
if (!PathFileExists(wcImagePath)) {
cout << "対象の画像が存在しません" << endl;
return -1;
}
// start
cascadeClassifier.load(CLASSIFIERFILENAME);
Mat image = imread(imagePath);
cvtColor(image, image, COLOR_BGR2GRAY);
equalizeHist(image, image);
vector<Rect> rects;
cascadeClassifier.detectMultiScale(image, rects, 1.1, 3, 0, Size(20, 20));
int rectsSize = rects.size();
cout << rectsSize << endl;
return rectsSize;
}
// http://stackoverflow.com/questions/27220/how-to-convert-stdstring-to-lpcwstr-in-c-unicode
wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
// stdafx.h : 標準のシステム インクルード ファイルのインクルード ファイル、または
// 参照回数が多く、かつあまり変更されない、プロジェクト専用のインクルード ファイル
// を記述します。
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: プログラムに必要な追加ヘッダーをここで参照してください
#include <opencv2\opencv.hpp>
#include <opencv2\opencv_modules.hpp>
#include <shlwapi.h>
#pragma comment( lib, "Shlwapi.lib" )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment