Skip to content

Instantly share code, notes, and snippets.

@takamin
Last active August 29, 2015 14:08
Show Gist options
  • Save takamin/4cc1098387bcbc7629d0 to your computer and use it in GitHub Desktop.
Save takamin/4cc1098387bcbc7629d0 to your computer and use it in GitHub Desktop.
/**
* # cvCascade.cpp
*
* ## CMakeLists.txt
* ```
* cmake_minimum_required(VERSION 2.8)
* project( cvCascade )
* find_package( OpenCV REQUIRED )
* add_executable( cvCascade cvCascade.cpp )
* target_link_libraries( cvCascade ${OpenCV_LIBS} )
* ```
* ## Generate Makefile by cmake
*
* 1. mkdir build
* 2. cd build
* 3. cmake -D OpenCV_DIR="path/to/OpenCVConfig.cmake" ..
*
*/
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main(int argc, char* argv[]) {
cout << "opencv-" << CV_VERSION << endl;
VideoCapture cap;
int image_index = -1;
char* image_path = 0;
char* cascade_path = argv[1];
if(argc > 2) {
image_index = 2;
} else {
cap.open(0);
}
CascadeClassifier cascade(cascade_path);
Mat empty;
Mat image;
Mat image_gray;
while (true) {
if(image_index < 0) {
cap >> image;
} else {
if(image_index < argc) {
image_path = argv[image_index];
image_index++;
image = cv::imread(image_path);
} else {
image = empty;
}
}
if(image.empty()) {
break;
}
cvtColor(image, image_gray, CV_BGR2GRAY);
vector<Rect> result;
cascade.detectMultiScale(image_gray, result, 1.1, 3, 0, cv::Size(1,1), cv::Size(200,200));
vector<Rect>::iterator rect = result.begin();
for (; rect != result.end(); rect++) {
rectangle(image, rect->tl(), rect->br(), Scalar(0, 255, 0), 2);
}
imshow("cvCascade.cpp", image);
if(image_index < 0) {
if(waitKey(1) != -1) {
break;
}
} else {
if (waitKey() == 'Q') {
break;
}
}
}
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment