Skip to content

Instantly share code, notes, and snippets.

@latrokles
Created October 26, 2009 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save latrokles/218956 to your computer and use it in GitHub Desktop.
Save latrokles/218956 to your computer and use it in GitHub Desktop.
/*
* edgeDetection
* Author: Latrokles - latrokles_at_gmail_dot_com
* DISCLAIMER: The Edge Detection algorithm used in this sketch comes from "An
* Introduction to Image Processing" by Frederic Patin, which is available at:
* http://www.gamedev.net/reference/articles/article2007.asp
*
* This sketch demonstrates a very simple algorithm for edge detection.
* The algorithm iterates through the each pixel in the frame, compares it with
* it's right and bottom neighbor, if the distance between the colors is greater
* than a given threshold, you've found an edge.
*
* For some interactivity:
* 1. pressing '+' will increase the threshold value
* 2. pressing '-' will decrease the threshold value
* 3. pressing ' ' (the space bar) will invert the threshold image.
*
*/
import processing.video.*;
Capture vidCam;
PImage modFrame;
int threshold; //Threshold to determine edge
color edgeColor = color(255, 255, 255);
color fillColor = color(0, 0, 0);
void setup()
{
size(600, 400);
background(100);
vidCam = new Capture(this, width, height, 25);
threshold = 20;
}
void captureEvent(Capture vidCam)
{
vidCam.read();
}
void draw()
{
loadPixels(); //Loads built-in pixel array(pixels), makes it accessible
edgeDetect(); //Edge detection routine, it accesses and modifies "pixels"
updatePixels(); //Updates the the display by loading the pixel array into the display buffer ...
//at least that is my understanding of how it works.
image(vidCam, 0, height - 120, 160, 120);
}
void edgeDetect()
{
int px, rpx, bpx; //pixel, right pixel, bottom pixel
int r, g, b; //rgb values for pixel
int r1, g1, b1; //same for pixel to the right
int r2, g2, b2; //same for pixel on the bottom
for(int x = 0; x < width - 1; x++)
{
for(int y = 0; y < height - 1; y++)
{
//Processing represents bitmaps as one dimensional arrays
//so we need to convert the pixels x, y coordinates (the way we would see
//it on screen) to a one dimensional index.
//We calculate this the following way: index = ypos * width of bitmap + xpos
px = (y * width) + x;
rpx = (y * width) + x + 1;
bpx = ((y+1) * width) + x;
//Get the red, green, and blue values for the pixels being compared
r = (int)red(vidCam.pixels[px]);
g = (int)green(vidCam.pixels[px]);
b = (int)blue(vidCam.pixels[px]);
r1 = (int)red(vidCam.pixels[rpx]);
g1 = (int)green(vidCam.pixels[rpx]);
b1 = (int)blue(vidCam.pixels[rpx]);
r2 = (int)red(vidCam.pixels[bpx]);
g2 = (int)green(vidCam.pixels[bpx]);
b2 = (int)blue(vidCam.pixels[bpx]);
//Determine whether distance between colors greater than threshold
if( ((int)sqrt( (r-r1)*(r-r1) + (g-g1)*(g-g1) + (b-b1)*(b-b1)) > threshold) || ((int)sqrt( (r-r2)*(r-r2) + (g-g2)*(g-g2) + (b-b2)*(b-b2) ) > threshold) )
{
pixels[px] = edgeColor;
}
else
{
pixels[px] = fillColor;
}
}
}
}
/* Simple interactive behavior */
void keyPressed()
{
if(key == '+')
{
threshold++;
}
if(key == '-')
{
threshold--;
}
if(key == ' ')
{
color tmp = edgeColor;
edgeColor = fillColor;
fillColor = tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment