Edge Detection

In this post we will experiment with edge detection techniques using the Roberts and Sobel edge detection algorithms.

The motivation for this post came from the PluralSight course Building Image Processing Applications Using scikit-image by Janani Ravi. She uses the Jupyter notebook for the exercises. In this post I am using VSCode with GitHub Copilot. I would like to disclose that I am a Microsoft employee and have been using VSCode for many years.

Typically edge detection is extracted by running a convolution operation over a matrix. The kernel is a smaller matrix with specific values determined based on the target operation. The kernel is applied from the top left corner moving to the right and eventually ending in the lower right corner of the matrix.

# **** folder of interest ****
cd C:\Documents\_Image Processing\scikit-image-building-image-processing-applications\02\demos

# **** open file of interest using VSCode ****
(base) C:\Documents\_Image Processing\scikit-image-building-image-processing-applications\02\demos>code EdgeDetection.py

# **** execute python script of interest ****
(base) C:\Documents\_Image Processing\scikit-image-building-image-processing-applications\02\demos>python EdgeDetection.py

We start by opening an Anaconda Prompt and switching to the folder of interest. In that folder we use VSCode to create the Python script of interest. In this case the Python script is named EdgeDetection.py. The file needs to be saved before executing it. Everytime we edit a change, we save the file and run the script.

# **** ****
import numpy as np
import matplotlib.pyplot as plt

# **** ****
import skimage
from skimage import color, data, io
from skimage.filters import roberts, sobel

In this code we just import the libraries and modules we will use to perform edge extraction. In particular we will use the Roberts and Sobel algorithms.

# **** read the image ****
skyline_color = skimage.io.imread('./images/pexels-skyline.jpg')    # 3D array

# **** convert RGB to grayscale before we perfomr edge detection ****
skyline = color.rgb2gray(skyline_color)                             # 2D array

# **** ****
plt.figure(figsize=(12, 12))
plt.imshow( skyline,
plt.show()                                                          # show the image

We then read an image of a skyline. The image is RGB formatted as a JPEG. We need to convert it to grayscale in order to detect edges. The image is then displayed.

# **** use the roberts filter to detect edges ****
skyline_edge_roberts = roberts(skyline)

# **** show the image ****
plt.figure(figsize=(12, 12))

plt.imshow( X=skyline_edge_roberts,
plt.show()                                                          # show the image

We use the skyline grayscale image to extract edges. The image is then shown.

# **** use the sobel filter to detect edges 
#      Sobel is a more sensitive to diagonal edges than Roberts ****
skyline_edge_sobel = sobel(skyline)

# **** show the image ****
plt.figure(figsize=(12, 12))
plt.imshow( X=skyline_edge_sobel,
plt.show()                                                          # show the image

We now repeat the process of extracting edges with the Sobel algorithm.  This algorithm is better at detecting diagonal edges.  The image is then displayed.

# **** ****
fig, axes = plt.subplots(   1, 
                            figsize=(12, 12),                         # width, height in inches
                            sharex=True,                            # x-axis will be shared among all subplots
                            sharey=True)                            # y-axis will be shared among all subplots
# **** flatten axes ****
ax = axes.ravel()
# **** plot roberts skyline ****
ax[0].imshow(   skyline_edge_roberts,
# **** plot sobel skyline ****
ax[1].imshow(   skyline_edge_sobel,
# **** fit plots within your figure cleanly (an alternative to tight_layout is constrained_layout) ****
# **** display the plots****

Now that we have both the results of the Roberts and Sobel algorithms, we displayed both images side by side.

If interested in the code for this post you may find it in my GitHub repository.

Keep on reading, experimenting, and repeating. It is the best way to learn.



Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.