Skip to content

Instantly share code, notes, and snippets.

@PauloMigAlmeida
Created February 18, 2015 21:31
Show Gist options
  • Save PauloMigAlmeida/43b3654054f735d6d091 to your computer and use it in GitHub Desktop.
Save PauloMigAlmeida/43b3654054f735d6d091 to your computer and use it in GitHub Desktop.
OpenCV CascadeClassifier example for face detection
#include <iostream>
#include <opencv2/opencv.hpp>
#include <stdexcept>
using namespace std;
using namespace cv;
vector<Rect> find_faces(Mat src)
{
vector<Rect> faces;
double scale_factor = 1.3;
int min_neighbor = 5;
int flags = 0;
Size min_size = Size(20,20);
CascadeClassifier cascadeDefault, cascadeAlt, cascadeAltTree, cascadeAlt2, cascadeProfile;
if (cascadeDefault.load("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml") == true)
{
cout << "using haarcascade_frontalface_default.xml" << endl;
cascadeDefault.detectMultiScale(src, faces, scale_factor, min_neighbor, flags, min_size);
if(!faces.empty())
return faces;
}
else
{
throw runtime_error("cascadeDefault.load() failed...");
}
if (cascadeAlt.load("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml") == true)
{
cout << "using haarcascade_frontalface_alt.xml" << endl;
cascadeAlt.detectMultiScale(src, faces, scale_factor, min_neighbor, flags, min_size);
if(!faces.empty())
return faces;
}
else
{
throw runtime_error("cascadeAlt.load() failed...");
}
if (cascadeAltTree.load("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt_tree.xml") == true)
{
cout << "using haarcascade_frontalface_alt_tree.xml" << endl;
cascadeAltTree.detectMultiScale(src, faces, scale_factor, min_neighbor, flags, min_size);
if(!faces.empty())
return faces;
}
else
{
throw runtime_error("cascadeAltTree.load() failed...");
}
if (cascadeAlt2.load("/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml") == true)
{
cout << "using haarcascade_frontalface_alt2.xml" << endl;
cascadeAlt2.detectMultiScale(src, faces, scale_factor, min_neighbor, flags, min_size);
if(!faces.empty())
return faces;
}
else
{
throw runtime_error("cascadeAlt2.load() failed...");
}
if (cascadeProfile.load("/usr/local/share/OpenCV/haarcascades/haarcascade_profileface.xml") == true)
{
cout << "using haarcascade_profileface.xml" << endl;
cascadeProfile.detectMultiScale(src, faces, scale_factor, min_neighbor, flags, min_size);
if(!faces.empty())
return faces;
}
else
{
throw runtime_error("cascadeProfile.load() failed...");
}
return faces;
}
int main(int argc, char* argv[])
{
cv::Mat src_img, gray_img, eq_img, result_img;
cout << "input argument " << argv[1] << endl;
src_img = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);
if (src_img.data == NULL) {
cout << "cv::imread() failed...\n" << endl;
return -1;
}
cvtColor(src_img, gray_img, CV_RGB2GRAY);
equalizeHist(gray_img, eq_img);
result_img = src_img.clone();
vector<Rect> faces = find_faces(eq_img);
vector<Rect>::const_iterator i;
for (i = faces.begin(); i != faces.end(); ++i) {
rectangle(
result_img,
Point(i->x, i->y),
Point(i->x + i->width, i->y + i->height),
CV_RGB(255, 0, 0),
2);
}
while(true) {
imshow("result_img", result_img);
int c = waitKey(0);
if (c == 27) break;
}
return 0;
}
# Makefile
ifeq ($(shell pkg-config --modversion opencv),)
$(error Package gtkmm-2.4 needed to compile)
endif
CXXFLAGS += `pkg-config --cflags opencv`
LDLIBS += `pkg-config --libs opencv`
BINS = program
program_OBJS = main.o
all: $(BINS)
program: $(program_OBJS)
$(CXX) $(LDFLAGS) $^ $(LOADLIBES) $(LDLIBS) -o $@
# this part is actually optional, since it's covered by gmake's implicit rules
%.o: %.cc
$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment