Edge Detection in Processing Using the Sobel Operator (Code)
Here is a quick run through of the code used to do an edge detection in Processing. First up I create an array that can contain all of the values of the screen intensities, place an image on the screen and load the image pixels into the system Pixels[] array:
int[] pixelarray = new int[width*height]; PImage img = loadImage("image.jpg"); image(img,0,0); loadPixels();
Iterating through each individual pixel, I get red, green and blue intensity values for that pixel add them together and depending on the values in the Sobel operator I have multiplied and added them to the overall intensity. For example the top left pixel in the Sobel matrix:
px = pixels[((y-1)*width)+(x-1)]; redvalue = red(px); bluevalue = blue(px); greenvalue = green(px); intensity = redvalue + greenvalue + bluevalue; Gx += -intensity; Gy += intensity;
Once I have worked out values for all of the surrounding pixels I work out using Pythagoras the overall gradient length. Then I normalise it so that it can be used as a output value. //calculate normalised length of gradient glength = sqrt((Gx*Gx)+(Gy*Gy)); glength = (glength/4328) * 255;
I load this new information into the array of intensities which is used to create the image of the outline. I have also used a threshold value to remove the detail from within the picture.
if (glength > 10) pixelarray[x+(width*y)] = color(glength); else pixelarray[x+(width*y)] = color(0);
Finally I load the array into the system array of pixels which is used to display the new image. for(int i = 0; i<width*height;i++) { pixels[i] = pixelarray[i]; }
Full code - https://github.com/bigrichardc/sketchbook/blob/master/sobelImageDetection.pde







