OpenCV on Beagleboard-xM | Getting started

Posted on June 24, 2012
Tags: , ,
by Sanchayan Maity

Before I start, let me mention a few things which I am assuming.

  1. You know how to use the vi Editor.

  2. You are connected over a serial link to the BB - xM and are doing all operations from the terminal.

Get the Angstrom Image from the below link and set up the SD card as per the instructions given.

After following the set up procedure, you will have a boot partition and root filesystem partition on the SD card.

After setting up the SD card, transfer an image in which you want faces to be detected. This image you can transfer to the /home/root directory in the root filesystem on the SD card.

Plug the SD card and boot up. The board will boot up and you will get the login prompt. Log in using root as the password, after which, you will be placed in the /home/root directory. This is the reason I told you to copy the image in this particular directory.

Use vi and open up a file faces.c by typing “vi faces.c” on the command line. Write the below code in it.

#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include "stdio.h"
#include "string.h"
int main(void)
    const char * cascade_path = "/usr/share/opencv/haarcascades/haarcascade_frontalface_default.xml";
    IplImage * img;
    CvHaarClassifierCascade * hc;
    CvSeq * faces;
    CvMemStorage * storage = cvCreateMemStorage(0);
    CvSize minSize = cvSize(20, 20);
    CvSize maxSize = cvSize(60, 60);

   img = cvLoadImage("faces.jpg",0);  //Assuming faces is in the directory the code is in.

   hc = (CvHaarClassifierCascade*)cvLoad(cascade_path,NULL,NULL,NULL);
   faces = cvHaarDetectObjects(img, hc, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, minSize, maxSize);

       printf("faces detected %d\n",faces->total);
       printf("cvHaarDetectObjects returned null\n");

   cvReleaseHaarClassifierCascade( &hc );
   cvReleaseImage( &img );
   cvReleaseMemStorage( &storage );

   return 0;


After saving the above program, compile it using the command:

$ arm-angstrom-linux-gnueabi-gcc faces.c /usr/lib/libopencv_*.so -o faces

I have mentioned /usr/lib/libopencv_*.so on the command line, as the compiler would then dynamically link all the OpenCV library files against this code.

This will create a executable called faces. Run it from the command line by typing “./faces”. The code would run and print the number of faces detected in the image on the command line.

I didn’t have a HDMI to DVI-D cable to connect my monitor to the board, so I printed the output on the command line. If you can connect a monitor, use the functions from the highgui library, and you can display your output on the monitor. You can draw rectangles around the faces detected.

I am currently working on face detection in video and facing a few glitches, but, I will post it when I am successful.

To learn what the functions actually do, refer to the OpenCV 2.2 C reference on the below link.

Disclaimer: I have modified the example code given in the reference documentation to suit my own needs.