How to Install a Library in Processing, using OpenCV as an Example

Libraries are extremely useful as they are developed for a specialized purpose, letting you solve more complex problems. OpenCV is an extensive open source computer vision library that allows you to track faces, objects, and other things seen by a camera. It focuses mainly towards real-time image processing. It allows you to all sorts of things that require camera tracking -- Such as tracking a faces, bodies, or movements.

To install OpenCV, you need to install the library. Follow these steps to install a library for Processing.

1. Download, unzip, and move the OpenCV Processing Library from here.

2. Option-click the Processing app in your Applications folder and click Show Package Contents



3. Browse to Contents/MacOS/Resources/Java/libraries/ and drop the OpenCV folder into it.



4. Re-Open Processing and try this really cool real-time motion-blender code from Andy Best.:

/**
Title: Processing Tutorials: Getting started with Video Processing via OpenCV
Author: Andy Best
Author URL: http://www.andybest.net/
Requires:
Processing http://processing.org
OpenCV http://opencv.willowgarage.com/wiki/
Processing Library http://ubaa.net/shared/processing/opencv/
*/

import hypermedia.video.*; // Imports the OpenCV library
OpenCV opencv; // Creates a new OpenCV Object
PImage trailsImg; // Image to hold the trails
int hCycle; // A variable to hold the hue of the image tint

void setup()
{

size( 320, 240 );

opencv = new OpenCV( this ); // Initialises the OpenCV object
opencv.capture( 320, 240 ); // Opens a video capture stream
trailsImg = new PImage( 320, 240 ); // Initialises trailsImg
hCycle = 0; // Initialise hCycle
}

void draw()
{

opencv.read(); // Grabs a frame from the camera
PImage camImage; // Creates an image and
camImage = opencv.image(); // stores the unprocessed camera frame in it

opencv.absDiff(); // Calculates the absolute difference
opencv.convert( OpenCV.GRAY ); // Converts the difference image to greyscale
opencv.blur( OpenCV.BLUR, 3 ); // I like to blur before taking the difference image to reduce camera noise
opencv.threshold( 20 );

trailsImg.blend( opencv.image(), 0, 0, 320, 240, 0, 0, 320, 240, SCREEN ); // Blends the movement image with the trails image

colorMode(HSB); // Changes the colour mode to HSB so that we can change the hue
tint(color(hCycle, 255, 255)); // Sets the tint so that the hue is equal to hcycle and the saturation and brightness are at 100%
image( trailsImg, 0, 0 ); // Display the blended difference image
noTint(); // Turns tint off
colorMode(RGB); // Changes the colour mode back to the default

blend( camImage, 0, 0, 320, 240, 0, 0, 320, 240, SCREEN ); // Blends the original image with the trails image

opencv.copy( trailsImg ); // Copies trailsImg into OpenCV buffer so we can put some effects on it
opencv.blur( OpenCV.BLUR, 4 ); // Blurs the trails image
opencv.brightness( -20 ); // Sets the brightness of the trails image to -20 so it will fade out
trailsImg = opencv.image(); // Puts the modified image from the buffer back into trailsImg

opencv.remember(); // Remembers the current frame

hCycle++; // Increments the hCycle variable by 1 so that the hue changes each frame
if (hCycle > 255) hCycle = 0; // If hCycle is greater than 255 (the maximum value for a hue) then make it equal to 0
}

Video

Processing OpenCV Tutorial Video #1- Psychedelic Blur! from Andy Best on Vimeo.