PIC - Simple Edge Detection and Creating a Collage (Lesson)
Simple Edge Detection and Creating a Collage
Introduction
Detecting edges is a common image processing problem. For example, digital cameras often feature face detection. Some robotics competitions required the robots to find a ball using a digital camera, so the robot needs to be able to “see” a ball.
Simple Edge Detection
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 a 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. We can use the colorDistance method in the Pixel class to calculate and return the difference between the current pixel color and a passed color.
A New Copy Method
Examine the copy method in the Picture class. You will notice that to copy one picture to another, we are copying the color of the pixels in one picture to the other picture. To do this we need to keep track of the row and column for both pictures. You will see that we have a fromRow as well as a toRow, a fromCol and toCol. We also need a fromPixel and a toPixel. The easiest way to do this is to declare and initialize both from and to counters in the for loop and increment them both at the end of the loop. A for loop can have more than one variable declaration and initialization and modification / step expression, just separate each item with a comma.
Modify the main method of the Picture class to the following so that you can see how the copy method works in create a collage.
Examine the createCollage method. You will see that we create 2 new pictures for flower1.jpg and flower2.jpg. We copy the picture several times and mirror the picture vertically so that the pattern appears on the other side of the picture as well. We can use the write method to save this picture.
Additional Picture Methods
Add the following methods to the Picture class. You will need these methods for the final lab project.
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. Modify the edgeDetection method so that it also compares the current pixel with the one below and sets the current pixel color to black as well when the color distance is greater than the specified edge distance.
2. Create a second copy method that adds parameters to allow you to copy just part of the fromPic. You will need to add parameters that specify the start row, end row, start column and end column to copy from.
- Be sure to test all the new methods to make sure they work as intended.
IMAGES CREATED BY GAVS