07 Nov 2016
Simple Edge Detection
Let’s make a picture that looks like a pencil sketch.
Here is a simple technique that can help you to achieve the above said results.
- Find an area of high contrast between a pixel and the pixel below it
- if contrast is high we can make pixel black otherwise white
Question here is how to find high or low contrast pixels? Follow the steps below :
- Average the RGB of current pixel and the pixel below it
- Find the difference between both average values
- If the resultant value is higher than a given amount, set current pixel to black, otherwise white
Here is an example program. Have a look on it and add it in to your MyPicture class used in our tutorial.
public void edgeDetection(double amount) { Pixel currPixel = null; Pixel belowPixel = null; int endY = this.getHeight() - 1; // loop through y values from 0 to height - 1 for (int y = 0; y < endY; y++) { // loop through the x values from 0 to width for (int x = 0; x < this.getWidth(); x++) { // get the top and bottom pixels currPixel = this.getPixel(x, y); belowPixel = this.getPixel(x, y + 1); /* * get the color averages for the two pixels, check if the * absolute value of the difference is less than the amount */ if (Math.abs(currPixel.getAverage() - belowPixel.getAverage()) < amount) currPixel.setColor(Color.WHITE); // else set the color to black else currPixel.setColor(Color.BLACK); } } }
Watch Video Tutorial
No Responses