mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-21 12:50:27 +08:00
Merge pull request #2 from emmanuelle/active-contour-model
Avoids crash of active contour example when run with old scipy version
This commit is contained in:
@@ -31,6 +31,13 @@ from skimage import data
|
||||
from skimage.filters import gaussian_filter
|
||||
from skimage.segmentation import active_contour
|
||||
|
||||
# Test scipy version, since active contour is only possible
|
||||
# with recent scipy version
|
||||
import scipy
|
||||
scipy_version = list(map(int, scipy.__version__.split('.')))
|
||||
new_scipy = scipy_version[0] > 0 or \
|
||||
(scipy_version[0] == 0 and scipy_version[1] >= 14)
|
||||
|
||||
img = data.astronaut()
|
||||
img = rgb2gray(img)
|
||||
|
||||
@@ -39,17 +46,23 @@ x = 220 + 100*np.cos(s)
|
||||
y = 100 + 100*np.sin(s)
|
||||
init = np.array([x, y]).T
|
||||
|
||||
snake = active_contour(gaussian_filter(img, 3),
|
||||
init, alpha=0.015, beta=10, gamma=0.001)
|
||||
if not new_scipy:
|
||||
print('You are using an old version of scipy. '
|
||||
'Active contours is implemented for scipy versions '
|
||||
'0.14.0 and above.')
|
||||
|
||||
fig = plt.figure(figsize=(7, 7))
|
||||
ax = fig.add_subplot(111)
|
||||
plt.gray()
|
||||
ax.imshow(img)
|
||||
ax.plot(init[:, 0], init[:, 1], '--r')
|
||||
ax.plot(snake[:, 0], snake[:, 1], '-b')
|
||||
ax.set_xticks([]), ax.set_yticks([])
|
||||
ax.axis([0, img.shape[1], img.shape[0], 0])
|
||||
if new_scipy:
|
||||
snake = active_contour(gaussian_filter(img, 3),
|
||||
init, alpha=0.015, beta=10, gamma=0.001)
|
||||
|
||||
fig = plt.figure(figsize=(7, 7))
|
||||
ax = fig.add_subplot(111)
|
||||
plt.gray()
|
||||
ax.imshow(img)
|
||||
ax.plot(init[:, 0], init[:, 1], '--r')
|
||||
ax.plot(snake[:, 0], snake[:, 1], '-b')
|
||||
ax.set_xticks([]), ax.set_yticks([])
|
||||
ax.axis([0, img.shape[1], img.shape[0], 0])
|
||||
|
||||
"""
|
||||
.. image:: PLOT2RST.current_figure
|
||||
@@ -66,17 +79,18 @@ x = np.linspace(5, 424, 100)
|
||||
y = np.linspace(136, 50, 100)
|
||||
init = np.array([x, y]).T
|
||||
|
||||
snake = active_contour(gaussian_filter(img, 1), init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
if new_scipy:
|
||||
snake = active_contour(gaussian_filter(img, 1), init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
|
||||
fig = plt.figure(figsize=(9, 5))
|
||||
ax = fig.add_subplot(111)
|
||||
plt.gray()
|
||||
ax.imshow(img)
|
||||
ax.plot(init[:, 0], init[:, 1], '--r')
|
||||
ax.plot(snake[:, 0], snake[:, 1], '-b')
|
||||
ax.set_xticks([]), ax.set_yticks([])
|
||||
ax.axis([0, img.shape[1], img.shape[0], 0])
|
||||
fig = plt.figure(figsize=(9, 5))
|
||||
ax = fig.add_subplot(111)
|
||||
plt.gray()
|
||||
ax.imshow(img)
|
||||
ax.plot(init[:, 0], init[:, 1], '--r')
|
||||
ax.plot(snake[:, 0], snake[:, 1], '-b')
|
||||
ax.set_xticks([]), ax.set_yticks([])
|
||||
ax.axis([0, img.shape[1], img.shape[0], 0])
|
||||
|
||||
plt.show()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user