// This program takes an image and runs edge detection on it. // Edge detection finds where the 'lines' in the image are. // This program does a simple version of edge detection in which // we compare each pixel to the one to the right and the one below it. // If there is a large difference in color, we say that there must // be a line there. Lines are drawn in black and the rest is in // white. PImage img; void setup() { //img = loadImage(""); //make the size the size of your image, which you will have to look up in your computer size(300, 300); } void draw() { if (mousePressed) { loadPixels(); img.loadPixels(); // running to 1 LESS than the width and the height means // we are always guaranteed that there are pixels to the // bottom and right int pix; int pixright; int pixbottom; int pixbottomright; for (int x = 0; x < img.width - 1; x++) { for (int y = 0; y < img.height - 1; y++) { pix = y * img.width + x; pixright = pix + 1; pixbottom = pix + img.width; pixels[pix] = edge_detection(img.pixels[pix], img.pixels[pixright], img.pixels[pixbottom]); } } updatePixels(); } else { image(img, 0, 0); } } // if the difference in grayscale is more than 'linedifference' then // we'll say there is a line there // A good number to use depends on the image. int lineThreshold = 10; // The algorithm used in this function is from // "Introduction to Computing and Programming in // Python: A Multimedia Approach", by Mark Guzdial // and Barbara Ericson. color edge_detection(color c, color below, color next) { int lineThreshold = round(map(mouseX,0,width,1,50)); // convert all colors to grayscale c = round(brightness(c)); below = round(brightness(below)); next = round(brightness(next)); // see if C differs greatly from its two neighbors if ((abs(c-below) > lineThreshold) && (abs(c-next) > lineThreshold)) { // if yes, make it black return color(0); } else { // otherwise, make it white return color(255); } }