Created
October 15, 2017 06:26
-
-
Save atsisy/7ea55b249a264de2a1be247b3362d232 to your computer and use it in GitHub Desktop.
OpenCV トラッキングAPIで顔面追尾 ref: http://qiita.com/AkihiroTakai/items/af0670207535b7cd145f
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/opencv.hpp> | |
cv::Mat detect_face(const cv::Mat &image, const std::string &cascade_file) | |
{ | |
cv::CascadeClassifier cascade; | |
std::vector<cv::Rect> faces; | |
//検出器読み込み | |
cascade.load(cascade_file); | |
//顔検出 | |
cascade.detectMultiScale(image, faces, 1, 3, 0, cv::Size(50, 50)); | |
//結果描画 | |
for (const cv::Rect &face_rect : faces) | |
{ | |
cv::rectangle(image, cv::Point(face_rect.x, face_rect.y), cv::Point(face_rect.x + face_rect.width, face_rect.y + face_rect.height), cv::Scalar(0, 200, 0), 3, CV_AA); | |
} | |
return image; | |
} | |
int main(int argc, char const **argv) | |
{ | |
//画像読み込み | |
cv::Mat image = cv::imread(argv[1]); | |
//検出器のパス | |
std::string filename = argv[2]; | |
//顔検出 | |
cv::Mat face_image = detect_face(image, filename); | |
//表示 | |
cv::imshow("detect face", face_image); | |
cv::waitKey(0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <opencv2/core.hpp> | |
#include <opencv2/imgcodecs.hpp> | |
#include <opencv2/videoio.hpp> | |
#include <opencv2/highgui.hpp> | |
#include <opencv2/tracking/tracker.hpp> | |
#include <opencv2/tracking/tldDataset.hpp> | |
#include <opencv2/imgproc.hpp> | |
#include <iostream> | |
int tracker_init(cv::Ptr<cv::TrackerMedianFlow> &tracker, cv::Rect2d &rect) | |
{ | |
cv::VideoCapture cap(0); | |
if (!cap.isOpened()) | |
{ | |
std::cout << "CAN'T OPEN CAMERA." << std::endl; | |
return -1; | |
} | |
cv::Mat frame; | |
cv::namedWindow("Select face"); | |
while (true) | |
{ | |
cap >> frame; | |
if (frame.empty()) | |
{ | |
std::cout << "FAILD TO READ A FRAME" << std::endl; | |
cv::destroyAllWindows(); | |
break; | |
} | |
cv::imshow("Select face", frame); | |
switch (cv::waitKey(1)) | |
{ | |
case 'q': | |
return -1; | |
case 't': | |
rect = cv::selectROI("tracker", frame); | |
tracker->init(frame, rect); | |
cv::destroyAllWindows(); | |
return 1; | |
default: | |
break; | |
} | |
} | |
return -1; | |
} | |
int main(int argc, char **argv) | |
{ | |
cv::Ptr<cv::TrackerMedianFlow> tracker = cv::TrackerMedianFlow::create(); | |
cv::Rect2d roi; | |
if (tracker_init(tracker, roi) == -1) | |
{ | |
/* | |
* 初期化に失敗 | |
*/ | |
return -1; | |
} | |
cv::Scalar color = cv::Scalar(0, 255, 0); | |
cv::Mat frame; | |
cv::VideoCapture cap(0); | |
if (!cap.isOpened()) | |
{ | |
std::cout << "CAN'T OPEN CAMERA." << std::endl; | |
return -1; | |
} | |
while (cv::waitKey(1) != 'q') | |
{ | |
cap >> frame; | |
if (frame.empty()) | |
{ | |
break; | |
} | |
//更新 | |
tracker->update(frame, roi); | |
//結果表示 | |
cv::rectangle(frame, roi, color, 1, 1); | |
cv::imshow("tracker", frame); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment