Skip to content

Instantly share code, notes, and snippets.

@chbrandt
Created August 11, 2023 14:19
Show Gist options
  • Save chbrandt/63ba38142630a0586ba2a13eabedf94b to your computer and use it in GitHub Desktop.
Save chbrandt/63ba38142630a0586ba2a13eabedf94b to your computer and use it in GitHub Desktop.
Maplotlib 3D plot of image used in the Carpentries Image-Processing episode.

Visualizing image in 3D with Matplotlib

This visualization was created to address a request for better visualization of the effects of blurring (smoothing). The (petri dish) image used and the exact context of the discussion can be recovered from the corresponding Carpentries' lesson and related discussion:

import imageio.v3 as iio
import skimage.color

image = iio.imread('data/colonies-01.tif')
image_gray = skimage.color.rgb2gray(image)

fig,ax = plt.subplots()
ax.imshow(image_gray, cmap='gray')

image

from skimage.util import img_as_ubyte

size = min(image_gray.shape)
x = np.arange(0, size)
y = np.arange(0, size)
X,Y = np.meshgrid(x,y)

img = image_gray[:size,:size]
img = img_as_ubyte(img)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X,Y,img, cmap='viridis')

ax.view_init(elev=215, azim=-60)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('L')

image

from skimage.util import img_as_ubyte
from skimage.filters import gaussian

image_blur = gaussian(image_gray, sigma=3)

size = min(image_blur.shape)
x = np.arange(0, size)
y = np.arange(0, size)
X,Y = np.meshgrid(x,y)

img = image_blur[:size,:size]
img = img_as_ubyte(img)

fig = plt.figure()
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X,Y,img, cmap='viridis')

ax.view_init(elev=215, azim=-60)

ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('L')

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment