Get startedGet started for free

Detect edges (1)

Filters can also be used as "detectors." If a part of the image fits the weighting pattern, the returned value will be very high (or very low).

In the case of edge detection, that pattern is a change in intensity along a plane. A filter detecting horizontal edges might look like this:

weights = [[+1, +1, +1],
           [ 0,  0,  0],
           [-1, -1, -1]]

For this exercise, create a vertical edge detector and see how well it performs on the hand x-ray (im).

This exercise is part of the course

Biomedical Image Analysis in Python

View Course

Exercise instructions

  • Create a 3x3 array of filter weights that detects when intensity changes from the left to right. Use only the values 1, 0 and -1.
  • Convolve im with the edge detector.
  • Plot the horizontal edges with the seismic colormap. Use vmin=-150 and vmax=150 to control adjust your colormap scale.
  • Add a colorbar and render the results.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Set weights to detect vertical edges
weights = [[____], [____], [____]]

# Convolve "im" with filter weights
edges = ____

# Draw the image in color
plt.imshow(____, ____, vmin=-150, vmax=150)
____
format_and_render_plot()
Edit and Run Code