Skip to content

Instantly share code, notes, and snippets.

@cagerton
Created May 15, 2011 20:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cagerton/973499 to your computer and use it in GitHub Desktop.
Save cagerton/973499 to your computer and use it in GitHub Desktop.
Edge detection in Processing
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
PImage input, output;
void setup() {
int width=400;
int height=300;
size(width,height);
output = createImage(width,height,RGB);
input = loadImage("car.jpg");
loadPixels();
// extract the BufferedImage
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
bimage.getGraphics().drawImage(input.getImage(), 0, 0, null);
// run edge detection
CannyEdgeDetector detector = new CannyEdgeDetector();
detector.setLowThreshold(0.5f);
detector.setHighThreshold(6f);
detector.setSourceImage(bimage);
detector.process();
BufferedImage edges = detector.getEdgesImage();
// build the PImage from edges
WritableRaster raster = edges.getRaster();
raster.getDataElements(0, 0, width, height, output.pixels);
output.save("/tmp/edges.png");
noLoop(); // Makes draw() only run once
}
void draw() {
image(output, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment