Normalizing faces with OpenCV

Tuesday, January 8th, 2013

One of the things that comes up when doing facial recognition is that you have to build a profile for each face being recognized. If we simply use a cascade to detect the face and then throw it into the profile set things usually work, but I have found that you get much better results by normalizing the faces as much as possible before processing.

I have been playing with is attempting to correct for face rotation by detecting the eyes and then rotating and zooming the face so the eyes always appear at as close to the same place in the training image as possible.

This example uses OpenCV. It uses one haar cascade to detect the face then uses two more cascades to detect the two eyes. It averages all of the eye detection results which will “often” give you an estimate of where the pupil is. Once you have that the rotating and scaling is pretty easy.

This technique only works really well on head-on faces with a rotation of less than 15 degrees in either direction. It could probably be expanded to rotate the image while doing the eye detection to get a better angle range but faces rotated more than 15 degrees is somewhat rare.

Here are some examples of this normalization:

 Download source code at GitHub

(this code is very sloppy, sorry)

Compiling opencv statically on OSX

Wednesday, February 4th, 2009

OpenCV is rapidly changing and there is no bundled version that comes with OSX. I’m working on some command line apps and don’t feel like explaining to my users how to install Macports so I decided I would need to statically compile it into my application. There is a OSX Framework available, but I’m not making an application bundle so that was a no-go.

Here is what needed to be done:

First off, this document on the OpenCv Wiki doesn’t seem to work anymore if you are pulling directly from subversion.

Build System – Open CV seems to have 3 types of build scripts in subversion: Make, Autotools, and CMake. It took a lot of trial and error before I realized that only CMake appears to be working, at least on my system.

You can get a copy of CMake here here.

Before you run CMake, you have to change the following files:

  • src/cxcore/CMakeLists.txt
  • src/highgui/CMakeLists.txt
  • src/cvaux/CMakeLists.txt
  • src/cv/CMakeLists.txt
  • src/ml/CMakeLists.txt
  • tests/cxts/CMakeLists.txt

Look for the add_library lines and change this:

add_library(${the_target} DYNAMIC ${lib_srcs} ${lib_hdrs})

to this:

add_library(${the_target} STATIC ${lib_srcs} ${lib_hdrs})

Once you have made that change, follow the CMake instructions here

In Your Own Project
Assuming the above worked well, you can now statically compile your application pointing to the .a files generated by your opencv build.

One thing to keep in mind, is that your application now has to be linked to OSX system frameworks. You can resolve this by adding the frameworks to your linker in your Makefile: