# Picture Lab A9: Simple Edge Detection

Each coding exercise has its own folder. Open its `.java` file and press **Run** to try it. **Run Tests** checks every graded exercise in this lesson at once.

## Edge Detection

Detecting edges is a common image processing problem. For example, digital cameras often feature face detection. Some robotic competitions require the robots to find a ball using a digital camera, so the robot needs to be able to “see” a ball. One way to look for an edge in a picture is to compare the color at the current pixel with the pixel in the next column to the right. If the colors differ by more than some specified amount, this indicates that an edge has been detected and the current pixel color should be set to black. Otherwise, the current pixel is not part of an edge and its color should be set to white (Figure 1).

![Original picture and after edge detection](images/picturelabedge.png)

*Original picture and after edge detection*

How do you calculate the difference between two colors? The formula for the difference between two points (x1,y1) and (x2,y2) is \sqrt{(x2 - x1)^{2} + (y2 - y1)^{2}}  Similarly, the difference between two colors (red1,green1,blue1) and (red2,green2,blue2) is   \sqrt{(red2 - red1)^{2} +(green2 - green1)^{2} +(blue2 - blue1)^{2}}

The `colorDistance` method in the `Pixel` class uses this calculation to return the difference between the current pixel color and a passed color.

The following method implements this simple algorithm. Notice that the nested for loop stops earlier than when it reaches the number of columns. That is because in the nested loop the current color is compared to the color at the pixel in the next column. If the loop continued to the last column this would cause an out-of-bounds error.

```java
public void edgeDetection(int edgeDist)
{
     Pixel leftPixel = null;
     Pixel rightPixel = null;
     Pixel[][] pixels = this.getPixels2D();
     Color rightColor = null;
     for (int row = 0; row < pixels.length; row++)
     {
          for (int col = 0; col < pixels[0].length-1; col++)
          {
               leftPixel = pixels[row][col];
               rightPixel = pixels[row][col+1];
               rightColor = rightPixel.getColor();
               if (leftPixel.colorDistance(rightColor) > edgeDist)
                   leftPixel.setColor(Color.BLACK);
               else
                   leftPixel.setColor(Color.WHITE);
          }
     }
}
```

You can test this with the `edgeDetection` method below.

### Exercise 1

Picture Lab A9: Run to see edgeDetection working.

> Open the `01-picture-lab-A9-edgeDetection` folder and run `Picture.java`. It is an example, so it has no tests. It draws pictures: **Run** saves the drawing as an image in its folder.

1. Notice that the current edge detection method works best when there are big color changes from left to right but not when the changes are from top to bottom. Add another nested loop that compares the current pixel with the pixel below it and sets the current pixel color to black as well when the color distance is greater than the specified edge distance.

### Exercise 2

Picture Lab A9: Improve the edgeDetection method by adding another nested loop that compares the current pixel with the pixel below it and sets the current pixel color to black as well, when the color distance is greater than the specified edge distance.

> Open the `02-picture-lab-A9-edgeDetection2` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (3 tests). Its tests check your code, not what it prints: “edgeDetection method”, “Checking that edgeDetection contains 4 (2 nested) for loops”, “Checking that edgeDetection calls colorDistance twice”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.

1. Work in groups to come up with another algorithm for edge detection and test it below.

### Exercise 3

Picture Lab A9: Come up with another algorithm for edgeDetection in a method called myEdgeDetection.

> Open the `03-picture-lab-A9-myEdgeDetection` folder and edit `Picture.java`. Press **Run** to try it; **Run Tests** checks it (3 tests). Its tests check your code, not what it prints: “myEdgeDetection( method”, “Checking that myEdgeDetection contains at least 2 for loops”, “Checking that myEdgeDetection calls colorDistance at least once”. It draws pictures: **Run** saves the drawing as an image in its folder, and **Run Tests** checks the code without the drawing.
