Skip to content

Instantly share code, notes, and snippets.

@emmanuelle
Created May 19, 2016 07:36
Show Gist options
  • Save emmanuelle/c741f9656d3e70efbd736bb32d75fee6 to your computer and use it in GitHub Desktop.
Save emmanuelle/c741f9656d3e70efbd736bb32d75fee6 to your computer and use it in GitHub Desktop.
from skimage.segmentation import active_contour
from skimage import io, color, transform, filters, util
import numpy as np
import matplotlib.pyplot as plt
image = io.imread('statue.jpg')
print('Rescaling image...')
image = transform.rescale(image, 0.05)
image = color.rgb2gray(image)
image = util.pad(image, ((3, 3), (0, 0)), mode='constant',
constant_values=image.max())
image = filters.gaussian(image, 2)
r, c = np.array(image.shape)[:2]
# Build inital contour
horiz = np.linspace(0, c, 50, endpoint=False)
vert = np.linspace(0, r, 50, endpoint=False)
up = np.array([horiz, np.ones_like(horiz)]).T
right = np.array([(c - 1) * np.ones_like(vert), vert]).T
down = np.array([horiz[::-1], (r - 1) * np.ones_like(horiz)]).T
left = np.array([np.ones_like(vert), vert[::-1]]).T
init = np.vstack((up, right, down, left))
print('Iterating active contour...')
snake = active_contour(image, init,
alpha=0.01, beta=0.1, w_line=-0.5, w_edge=1.5,
max_iterations=2000, gamma=0.01)
plt.imshow(image, cmap='gray', interpolation='nearest')
plt.plot(init[:, 0], init[:, 1])
plt.plot(snake[:, 0], snake[:, 1])
plt.show()
@emmanuelle
Copy link
Author

Using
statue

@emmanuelle
Copy link
Author

Here is the result
contour

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