Detect edges (2)
Edge detection can be performed along multiple axes, then combined into a single edge value. For 2D images, the horizontal and vertical "edge maps" can be combined using the Pythagorean theorem:
$$z = \sqrt{x^2 + y^2}$$
One popular edge detector is the Sobel filter. The Sobel filter provides extra weight to the center pixels of the detector:
weights = [[ 1, 2, 1],
[ 0, 0, 0],
[-1, -2, -1]]
For this exercise, improve upon your previous detection effort by merging the results of two Sobel-filtered images into a composite edge map.
This exercise is part of the course
Biomedical Image Analysis in Python
Exercise instructions
- Apply
ndi.sobel()
toim
along the first and second axes. - Calculate the overall edge magnitude using the Pythagorean theorem. Use
np.sqrt()
andnp.square()
. - Display the magnitude image. Use a grayscale colormap and set
vmax
to75
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Apply Sobel filter along both axes
sobel_ax0 = ndi.sobel(____, axis=____)
sobel_ax1 = ____
# Calculate edge magnitude
edges = ____
# Plot edge magnitude
plt.imshow(____, ____, ____)
format_and_render_plot()