Studio 2

Edge Detection & Face Detection with OpenCV

You should already have Processing installed, if not: Install Processing

Install OpenCV for Processing:

1. In the Processing IDE development environment, go to “Import Library” under the “Sketch” menu

2. Search for ‘OpenCV’

3. Install “OpenCV for Processing” by Greg Borenstein.

4. Complete the Edge Detection and Face Detection Exercises Below

Edge Detection - Still Image:

Choose Edge Detection Type & Save

Under the Sketch menu, go to “Show Sketch Folder”. This will open up a file browser window.

Copy a test image into that folder - call it, for example, “test-image.jpg”.

This code will save a new image in that folder called “edge-detection.jpg” when you click the mouse on the image.

In the draw() function, select which type of edge detection you’d like to try by uncommenting the line you want to display and commenting out the others.

In the setup function, the type of edge detection you’ve selected can be customized by changing these variables: lowThresh, hiThresh (Canny); horizontal or vertical (Scharr); or dx and dy (Sobel).

import gab.opencv.*;
OpenCV opencv;
PImage src, canny, scharr, sobel;
void setup() {
src = loadImage("test-image.jpg");
size(640, 480, P2D);
opencv = new OpenCV(this, src);
int lowThresh = 20;
int hiThresh = 75;
opencv.findCannyEdges(lowThresh,hiThresh);
canny = opencv.getSnapshot();
// the function findScharrEdges will take one of these arguments:
// OpenCV.HORIZONTAL or OpenCV.VERTICAL. You can swap them quickly
// by giving the function the variables v or h as defined below
int h = OpenCV.HORIZONTAL;
int v = OpenCV.VERTICAL;
opencv.loadImage(src);
opencv.findScharrEdges(v);
scharr = opencv.getSnapshot();
int dx = 1;
int dy = 0;
opencv.loadImage(src);
opencv.findSobelEdges(dx, dy);
sobel = opencv.getSnapshot();
}
void draw() {
//image(canny, 0, 0);
//image(scharr, 0, 0);
image(sobel, 0, 0);
}
void mousePressed() {
save("edge-detection.jpg");
}

Face Detection Exercise - Still Image

Under Sketch, Go to Import Library -> Add Library 'Video'

Under the Sketch menu, go to “Show Sketch Folder”. This will open up a file browser window.

Copy a test image into that folder - call it, for example, “test-faces.jpg”.

This code will save a new image in that folder called “found-faces.jpg” when you click the mouse on the image.

Code for the IDE:
import gab.opencv.*;
import java.awt.Rectangle;
OpenCV opencv;
Rectangle[] faces;
void setup() {
opencv = new OpenCV(this, "face-test.jpg");
size(640, 480);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
faces = opencv.detect();
}
void draw() {
image(opencv.getInput(), 0, 0);
noFill();
stroke(0, 255, 0);
strokeWeight(3);
for (int i = 0; i < faces.length; i++) {
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
}
void mousePressed() {
save("found-faces.jpg");
}

Face Detection Exercise - From Webcam

After running this code, go to Tools>MovieMaker to turn the frames into a Quicktime video.

import gab.opencv.*;
import processing.video.*;
import java.awt.*;

Capture video;
OpenCV opencv;

void setup() {

size(640, 480);
video = new Capture(this, 640/2, 480/2);
opencv = new OpenCV(this, 640/2, 480/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
video.start();

}

void draw() {

scale(2);
opencv.loadImage(video);
image(video, 0, 0 );
noFill();
stroke(0, 255, 0);
strokeWeight(3);
Rectangle[] faces = opencv.detect();
println(faces.length);
for (int i = 0; i < faces.length; i++) {
println(faces[i].x + "," + faces[i].y);
rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height);
}
saveFrame("cam-face-detect-######.jpg");
}
void captureEvent(Capture c) {
c.read();
}

Edge Detection - Still Image - Quick Comparison of Edge Detection Types

[Note: This is the provided edge detection demo from OpenCV for Processing. I’ve included it as an illustration of the available edge detection methods.]

Under the Sketch menu, go to “Show Sketch Folder”. This will open up a file browser window.
Copy a test image into that folder - call it, for example, “test-image.jpg”.

import gab.opencv.*;
OpenCV opencv;
PImage src, canny, scharr, sobel;

void setup() {
src = loadImage("test-image.jpg");
size(640,480 P2D);
opencv = new OpenCV(this, src);
opencv.findCannyEdges(20,75);
canny = opencv.getSnapshot();

opencv.loadImage(src);
opencv.findScharrEdges(OpenCV.HORIZONTAL);
scharr = opencv.getSnapshot();
opencv.loadImage(src);
opencv.findSobelEdges(1,0);
sobel = opencv.getSnapshot();

}

void draw() {

pushMatrix();
scale(0.5);
image(src, 0, 0);
image(canny, src.width, 0);
image(scharr, 0, src.height);
image(sobel, src.width, src.height);
popMatrix();
text("Source", 10, 25);
text("Canny", src.width/2 + 10, 25);
text("Scharr", 10, src.height/2 + 25);
text("Sobel", src.width/2 + 10, src.height/2 + 25);
}

Background Subtraction using live feed from webcam

Run the code below.
import gab.opencv.*;
import processing.video.*;
import java.awt.*;

//Movie video;
Capture video;
OpenCV opencv;
void setup() {
size(640, 480, P2D);

// size(720, 480, P2D);

video = new Capture(this, 640/2, 480/2);

// video = new Movie(this, "street.mov");

// opencv = new OpenCV(this, 720, 480);

opencv = new OpenCV(this, 640/2, 480/2);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);
opencv.startBackgroundSubtraction(5, 3, 0.5);

// video.loop();
// video.play();
video.start();

}

void draw() {
scale(2);
image(video, 0, 0);
opencv.loadImage(video);
opencv.updateBackground();
opencv.dilate();
opencv.erode();
noFill();
stroke(255, 0, 0);
strokeWeight(3);
for (Contour contour : opencv.findContours()) {
contour.draw();

}

}
void movieEvent(Movie m) {
m.read();

}

Background Subtraction using video


Download a video of your choice, preferably of smaller file size.
Note that the bigger the file, the longer it would take for the video to load and process.

Under Sketch Menu, go to "Show Sketch Folder". This will open up a file browser window.

Copy the video into that folder - call it. For example, "test.mov".

Run the code below. You will be able to detect moving objects.

Adjust the thresholds in the function 'startBackgroundSubtraction' to distinguish background from foreground and contour tracking to track the foreground objects.

import gab.opencv.*;
import processing.video.*;
Movie video;

OpenCV opencv;

void setup() {

size(720, 480);
video = new Movie(this, "test.mov");
opencv = new OpenCV(this, 720, 480);
opencv.startBackgroundSubtraction(5, 3, 0.5);
video.loop();
video.play();

}

void draw() {
image(video, 0, 0);
opencv.loadImage(video);
opencv.updateBackground();
opencv.dilate();
opencv.erode();
noFill();
stroke(255, 0, 0);
strokeWeight(3);
for (Contour contour : opencv.findContours()) {
contour.draw();
}

}

void movieEvent(Movie m) {

m.read();

}