Processing Month, Day 31 - Building Interactive Installations with OpenCV

Posted by Jan Vantomme on 31 May 2011.
Tags: , ,

For the last day in my Processing Month series, I’m going to cover the basics of building interactive installations with Processing. The name of the installation we’ll build is “The Blurry Mirror”. The concept is easy: the computer screen will act as a digital mirror. The closer somebody gets to the installation, the more the image in the digital mirror will be blurred. We’re going to do some facetracking with the OpenCV library for Processing. You should download and install the OpenCV library to get started.

Setting up OpenCV

Inside the setup() function, we need to setup OpenCV and tell it what we’re going to track. I have set OpenCV to capture the video from my webcam at a resolution of 640 x 480 pixels. This should be enough for most installations. You can use a higher resolution if your camera supports it, but this will require more processing time for the CPU. The last OpenCV method I used in the setup() of my sketch is cascade(). I’m using one of the standard cascades (CASCADE_FRONTALFACE_ALT) to track a face in a video. This method can also be used to load a custom xml file with a description for tracking objects.

size1024768 );
opencv = new OpenCVthis );
opencv.capture640480 );
opencv.cascadeOpenCV.CASCADE_FRONTALFACE_ALT ); 

Preparing the Image

The next thing we need to do is to prepare the image from the camera for face tracking. This code below should be the first part inside the draw() function. The read() method will get the current image from the camera so we can use it in our installation. We also need to flip our image with FLIP_HORIZONTAL to get the mirror effect. You can also adjust the contrast and brightness of the image to get better tracking when needed. These methods need an integer between -128 and 128. I’ve used the arrow keys to adjust the values. You can display the image by using the OpenCV image() method as the first parameter for the standard Processing image() function.

opencv.read();
opencv.flipOpenCV.FLIP_HORIZONTAL );
opencv.contrastcontrastValue );
opencv.brightnessbrightnessValue );
imageopencv.image(), 0); 

Detecting Faces

Now that our image is prepared, we need to use the detect() method to scan the image for frontal faces. We can do this with the following line of code. I’ve imported java.awt.Rectangle because the detect() method will return an array of rectangles. These rectangles have a location, width and height we can use as parameters for blurring the final output of our installation.

Rectangle[] faces opencv.detect1.22OpenCV.HAAR_DO_CANNY_PRUNING4040 ); 

Because a mirror is something personal, I only want to track one face, so only one person at a time will be able to interact with the installation. The first if statement is really important. We only need to do things with the image or the rectangles if a face is found in the image, otherwise the installation would crash. I’ve mapped the coordinate and the width of the first face to the size of our Processing sketch. The variable f is then used to decide how much the final image should be blurred. If the size of the face is small (far away from the camera), the image doesn’t get blurred, if the size is big (close to the camera) the image gets blurred a lot. I also added an if statement for debugging. This can be toggled by pressing the d key on your keyboard. The image below shows you the blurred image when somebody gets close to the camera.

if ( faces.length {
    
// values needed for debugging
    
float x mapfaces[0].x064001024 );
    
float y mapfaces[0].y04800768 );
    
float f mapfaces[0].width064001024 );       
    if ( 
<= 200{
        
// no blurring
    
else if ( 200 && <= 300 {
        opencv
.blurOpenCV.GAUSSIAN11 );    
    
else if ( 300 && <= 400 {
        opencv
.blurOpenCV.GAUSSIAN31 );    
    
else if ( 400 && <= 500 {
        opencv
.blurOpenCV.GAUSSIAN51 );   
    
else {
        opencv
.blurOpenCV.GAUSSIAN71 );
    
}
    image
opencv.image(), 00widthheight );
    if ( 
DEBUG {
        noFill
();
        
stroke2550);
        
rectxyf);
    
}

A blurred face close to our Mirror installation

The last thing we need to do is to add an extra function to our application to stop OpenCV when our application quits.

public void stop()
{
    opencv
.stop();
    
super.stop();

Download

We covered the basics of OpenCV, so you should be able to get started and build your own interactive installation. You can download the example for the Blurry Mirror installation and experiment with it.

Tweet this article