ECE 311 Lab Final:

Note: a 10% penalty will be applied for each hour your submission is late!

This lab final will review the import concepts from the course. Much of this lab should be familiar from previous labs. We encourage you to look back on your previous labs or look at the posted lab solutions on the course website to remind you how to produce your results and evaluate their correctness. Enough talking, let’s get started!

import numpy as np

import matplotlib.pyplot as plt

from skimage.io import imread

from scipy import signal

from pz_plot import pz_plot

from scipy.io import wavfile

from numpy.random import randn

from IPython.display import Audio

#Utility function for dB scaling of magnitude spectra

def sig2db(mag_spec):

return 20*np.log10(mag_spec)

%matplotlib inline

Exercise 1: Building an Edge Detector 2.0

In Lab 2 Exercise 4, we built a simple edge detector by applying a high-pass filter along the rows and columns of an image, then combined the two results to create an image of detected edges. In this exercise, we will build a more sophisticated edge detector by adding onto our original design. This improved detector is known as the Sobel operator.

The Sobel operator uses the same intuition of finding horizontal and vertical edges separately, then combining these results to form the image. Let $G_x$ be our resulting image from detecting vertical edges (through the x-axis) and $G_y$ be our resulting image from detecting horizontal edges (through the y-axis, then our final result will be given by

$$ G[i,j] = \sqrt{G^2_x[i,j]+G^2_y[i,j]}, $$

where $G[i,j]$ is the pixel value at row $i$, column $j$. We compute $G_x$ and $G_y$ via convolution as follows:

$$ G_x = I * \begin{bmatrix} 1 & 0 & -1\\ \end{bmatrix} * \begin{bmatrix} 1\\ 2\\ 1\\ \end{bmatrix} $$$$ G_y = I * \begin{bmatrix} 1\\ 0\\ -1\\ \end{bmatrix} * \begin{bmatrix} 1 & 2 & 1\\ \end{bmatrix}, $$

where $I$ is our original image and $*$ denotes the convolution operator. Note that in the computation of $G_x$ we convolve along the rows with a high-pass filter and convolve along the columns with a low-pass filter, while the computation of $G_y$ reverses this relationship with the same filters.

a. Compute the image $G_x$ by performing the two convolutions along the rows and columns, respectively, using $\textrm{signal.convolve()}$. Plot your resulting image in grayscale. Remember that you should apply the high-pass filter $\begin{bmatrix}1 & 0 & -1\end{bmatrix}$ to each row and the low-pass filter $\begin{bmatrix}1 & 2 & 1\end{bmatrix}$ to each column. Also, be sure to use the “same” mode when using $\textrm{signal.convolve()}$.

b. Compute the image $G_y$ by performing the two convolutions along the rows and columns, respectively, using $\textrm{signal.convolve()}$. Plot your resulting image in grayscale. Remember that you should apply the high-pass filter $\begin{bmatrix}1 & 0 & -1\end{bmatrix}$ to each column and the low-pass filter $\begin{bmatrix}1 & 2 & 1\end{bmatrix}$ to each row.

c. Create final result image $G$ according to the above formulation. Plot your resulting image in grayscale.

#load test-image.jpg

image = imread(‘test-image.jpg’)

n_rows,n_cols = image.shape

plt.figure(figsize=(15,15))

plt.subplot(411)

plt.imshow(image,’gray’)

plt.title(‘Original Image ‘)

#Code for part a.

hpf_x=np.array([1,0,-1])

lpf_x=np.array([1,2,1])

Gx=np.zeros(image.shape)

for i in range(n_rows):

Gx[i,:]=signal.convolve(image[i,:],hpf_x,’same’)

for i in range(n_cols):

Gx[:,i]=signal.convolve(Gx[:,i],lpf_x,’same’)

plt.subplot(412)

plt.imshow(Gx,’gray’)

plt.title(‘Sobel Operator for Vertical Edge Detection’)

#Code for part b:

Gy=np.zeros(image.shape)

for i in range(n_cols):

Gy[:,i]=signal.convolve(image[:,i],hpf_x,’same’)

for i in range(n_rows):

Gy[i,:]=signal.convolve(Gy[i,:],lpf_x,’same’)

plt.subplot(413)

plt.imshow(Gy,’gray’)

plt.title(‘Sobel Operator for Horizontal Edge Detection’)

#Code for part c:

G_norm=image

for i in range(n_rows):

for j in range(n_cols):

G_norm[i,j] = np.sqrt((Gx[i,j]**2)+(Gy[i,j]**2))

plt.subplot(414)

plt.imshow(G_norm,’gray’)

plt.title(‘Resulting Image after applying Sobel Operator Edge Detection’)

Text(0.5, 1.0, ‘Resulting Image after applying Sobel Operator Edge Detection’)