From 7691fdc3c62ec99c2721d9bfe6e0fb3a9a953018 Mon Sep 17 00:00:00 2001 From: Stefan van der Walt Date: Wed, 30 Nov 2011 13:38:46 -0800 Subject: [PATCH] DOC: Add contour finding example. --- doc/examples/plot_contours.py | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 doc/examples/plot_contours.py diff --git a/doc/examples/plot_contours.py b/doc/examples/plot_contours.py new file mode 100644 index 00000000..8f707d52 --- /dev/null +++ b/doc/examples/plot_contours.py @@ -0,0 +1,44 @@ +""" +=============== +Contour finding +=============== + +``skimage.measure.find_contours`` uses a marching squares method to +find constant valued contours in an image. Array values are linearly +interpolated to provide better precision of the output contours. +Contours which intersect the image edge are open; all others +are closed. + +The `marching squares algorithm +`__ is a special +case of the marching cubes algorithm (Lorensen, William and Harvey +E. Cline. Marching Cubes: A High Resolution 3D Surface Construction +Algorithm. Computer Graphics (SIGGRAPH 87 Proceedings) 21(4) July +1987, p. 163-170). + +""" + +from skimage import data +from skimage import measure + +import numpy as np +import matplotlib.pyplot as plt + +# Construct some test data +x, y = np.ogrid[-np.pi:np.pi:100j, -np.pi:np.pi:100j] +r = np.sin(np.exp((np.sin(x)**3 + np.cos(y)**2))) + +# Find contours at a constant value of 0.8 +contours = measure.find_contours(r, 0.8) + +# Display the image and plot all contours found +plt.imshow(r, interpolation='nearest') + +for n, contour in enumerate(contours): + plt.plot(contour[:, 1], contour[:, 0], linewidth=2) + +plt.axis('image') +plt.xticks([]) +plt.yticks([]) +plt.show() +