In the previous section, we picked out objects of interest (i.e., the flowers) by focusing on the brightest regions, since we know that the flowers are lighter than the other parts of the image. Another way you can pick out an object of interest from image is through edge detection. The basic idea behind edge detection methods is that they check how quickly intensity values change across an image. This assumes that within a single object, the intensity does not change too much, but the intensities change a lot between objects. For example, within a single flower petal, there are small variations in color, but the pixels are mostly similar in intensity to one another. But when you cross over to a leaf, the pixels suddenly become much darker.
One widely-used edge detection method is called the Roberts algorithm, which is also included as a function in the scikit-image library. We apply this algorithm to the same image of flowers we analyzed in the previous section, and we see that the processed image mostly consists of the edges of the flowers, with some contribution from the wooden frame on the side and the leaves on the bottom of the image.
1 #import image processing package
2 from skimage import io
3 from skimage.filters import roberts
4 #import plotting package
5 import matplotlib.pyplot as plt
6 #read in image
7 imagefile = io.imread("Flowers.jpg", as_gray = True)
9 #find object edges
10 edges = roberts(imagefile)
11 plt.imshow(edges, cmap = "gray")