---
author: Sanchayan Maity
title: OpenCV on Beagleboard-xM | Getting started
tags: beagleboard, facedetection, opencv
---

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.

<p style='text-align: justify;'>Get the Angstrom Image from the below link and set up the SD card as per the instructions given.</p>

[http://elinux.org/Getting_a_Workshop_SD_Image](http://elinux.org/Getting_a_Workshop_SD_Image)

<p style='text-align: justify;'>After following the set up procedure, you will have a boot partition and root filesystem partition on the SD card.</p>

<p style='text-align: justify;'>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.</p>

<p style='text-align: justify;'>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.</p>

<p style='text-align: justify;'>Use vi and open up a file faces.c by typing "vi faces.c" on the command line. Write the below code in it.</p>

```c
#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.
   if(img)
   {
       printf("Success");
       printf("\n");
   }
   else
   {
       printf("Failed");
       printf("\n");
   }

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

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

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

   return 0;

}
```

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

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

<p style='text-align: justify;'>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.</p>

<p style='text-align: justify;'>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.</p>

<p style='text-align: justify;'>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.</p>

<p style='text-align: justify;'>I am currently working on face detection in video and facing a few glitches, but, I will post it when I am successful.</p>

<p style='text-align: justify;'>To learn what the functions actually do, refer to the OpenCV 2.2 C reference on the below link.</p>

[http://opencv.jp/opencv-2.2_org/c/](http://http://opencv.jp/opencv-2.2_org/c/).

<p style='text-align: justify;'>**Disclaimer: I have modified the example code given in the reference documentation to suit my own needs.**</p>
