Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

stough 202-

Preview: Heightmap Demo

In this quick demo I wanted to show how an image can be seen as a height-map, where the xy-plane is the domain of the image, while the z-axis represents the image intensity. This is easy enough with matplotlib’s 3D plotting capability. Specifically I would refer you to the surface plot examples.

Why bother? Because thinking of an image as terrain is one of the most useful mental models in this book. Bright regions are plateaus, dark regions are valleys, and edges are cliffs. Later, when we filter images, blurring will mean eroding the terrain smooth, and finding edges will mean measuring how steep the slope is.

((960, 640), dtype('float64'), np.float64(0.0), np.float64(0.9521090196078432))
<Figure size 400x300 with 1 Axes>
<Figure size 500x500 with 1 Axes>

The argument projection='3d' means the ax returned is an Axes3D object. We then call that object’s plot_surface method with a colormap so that the colors change as a function of the image intensity, or z coordinate.

Note also that 3D plotting normalizes the units for a square output, even while the original image may not be. Lastly, code that does this surface plotting has been added to vis_utils as vis_surface if we need it in the future.

One more subtle thing: our image is 960×640960 \times 640, but the surface above looks fairly coarse. That’s because plot_surface quietly samples the data down to at most 50 rows and 50 columns by default (its rcount and ccount arguments), so that the 3D rendering stays fast. That’s a reduction in spatial resolution, exactly the subject of this chapter. You can ask for more with, e.g., rcount=200, ccount=200, at the cost of a slower, laggier interactive plot.

 

Smoothing the Terrain

As a preview of where we’re headed, let’s blur the image a bit (each pixel replaced by a weighted average of its neighbors, which we’ll study in spatial filtering) and look at the terrain again. I’ll also downsample by 8 in each dimension first, so we can see more of the surface’s detail with fewer points.

<Figure size 800x400 with 2 Axes>

The jagged peaks of the smoke have been worn down into rolling hills, and the cliffs at the edge of the letter now slope more gently. Keep this picture in mind: smoothing lowers slopes, and edges are where the slopes are steep.