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).

../_images/plot_contours_1.png

import numpy as np
import matplotlib.pyplot as plt

from skimage import measure


# 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
fig, ax = plt.subplots()
ax.imshow(r, interpolation='nearest', cmap=plt.cm.gray)

for n, contour in enumerate(contours):
    ax.plot(contour[:, 1], contour[:, 0], linewidth=2)

ax.axis('image')
ax.set_xticks([])
ax.set_yticks([])
plt.show()

STDOUT


        

STDERR


        

Python source code: download (generated using skimage 0.11dev)

IPython Notebook: download (generated using skimage 0.11dev)

aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCBtYXRwbG90bGliLnB5cGxvdCBhcyBwbHQKCmZyb20gc2tpbWFnZSBpbXBvcnQgbWVhc3VyZQoKCiMgQ29uc3RydWN0IHNvbWUgdGVzdCBkYXRhCngsIHkgPSBucC5vZ3JpZFstbnAucGk6bnAucGk6MTAwaiwgLW5wLnBpOm5wLnBpOjEwMGpdCnIgPSBucC5zaW4obnAuZXhwKChucC5zaW4oeCkqKjMgKyBucC5jb3MoeSkqKjIpKSkKCiMgRmluZCBjb250b3VycyBhdCBhIGNvbnN0YW50IHZhbHVlIG9mIDAuOApjb250b3VycyA9IG1lYXN1cmUuZmluZF9jb250b3VycyhyLCAwLjgpCgojIERpc3BsYXkgdGhlIGltYWdlIGFuZCBwbG90IGFsbCBjb250b3VycyBmb3VuZApmaWcsIGF4ID0gcGx0LnN1YnBsb3RzKCkKYXguaW1zaG93KHIsIGludGVycG9sYXRpb249J25lYXJlc3QnLCBjbWFwPXBsdC5jbS5ncmF5KQoKZm9yIG4sIGNvbnRvdXIgaW4gZW51bWVyYXRlKGNvbnRvdXJzKToKICAgIGF4LnBsb3QoY29udG91cls6LCAxXSwgY29udG91cls6LCAwXSwgbGluZXdpZHRoPTIpCgpheC5heGlzKCdpbWFnZScpCmF4LnNldF94dGlja3MoW10pCmF4LnNldF95dGlja3MoW10pCnBsdC5zaG93KCk=