mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-18 12:40:14 +08:00
pep8 compliance
This commit is contained in:
@@ -2,7 +2,7 @@ import numpy as np
|
||||
from skimage import img_as_float
|
||||
import scipy.linalg
|
||||
from scipy.interpolate import RectBivariateSpline
|
||||
from skimage.filters import gaussian_filter, sobel
|
||||
from skimage.filters import sobel
|
||||
|
||||
def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
w_line=0, w_edge=1, gamma=0.01,
|
||||
@@ -58,6 +58,7 @@ def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
--------
|
||||
>>> #from skimage.segmentation import active_contour_model
|
||||
>>> from skimage.draw import circle_perimeter
|
||||
>>> from skimage.filters import gaussian_filter
|
||||
>>> img = np.zeros((100, 100))
|
||||
>>> rr, cc = circle_perimeter(35, 45, 25)
|
||||
>>> img[rr, cc] = 1
|
||||
@@ -71,7 +72,7 @@ def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
"""
|
||||
|
||||
max_iterations = int(max_iterations)
|
||||
if max_iterations<=0:
|
||||
if max_iterations <= 0:
|
||||
raise ValueError("max_iterations should be >0.")
|
||||
convergence_order = 10
|
||||
valid_bcs = ['periodic', 'free', 'fixed', 'free-fixed',
|
||||
@@ -80,25 +81,26 @@ def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
raise ValueError("Invalid boundary condition.\n"+
|
||||
"Should be one of: "+", ".join(valid_bcs)+'.')
|
||||
img = img_as_float(image)
|
||||
RGB = len(img.shape)==3
|
||||
RGB = len(img.shape) == 3
|
||||
|
||||
# Find edges using sobel:
|
||||
if w_edge!=0:
|
||||
if w_edge != 0:
|
||||
if RGB:
|
||||
edge = [sobel(img[:,:,0]),sobel(img[:,:,1]),sobel(img[:,:,2])]
|
||||
edge = [sobel(img[:, :, 0]), sobel(img[:, :, 1]),
|
||||
sobel(img[:, :, 2])]
|
||||
else:
|
||||
edge = [sobel(img)]
|
||||
for i in xrange(3 if RGB else 1):
|
||||
edge[i][0,:] = edge[i][1,:]
|
||||
edge[i][-1,:] = edge[i][-2,:]
|
||||
edge[i][:,0] = edge[i][:,1]
|
||||
edge[i][:,-1] = edge[i][:,-2]
|
||||
edge[i][0, :] = edge[i][1, :]
|
||||
edge[i][-1, :] = edge[i][-2, :]
|
||||
edge[i][:, 0] = edge[i][:, 1]
|
||||
edge[i][:, -1] = edge[i][:, -2]
|
||||
else:
|
||||
edge = [0]
|
||||
|
||||
# Superimpose intensity and edge images:
|
||||
if RGB:
|
||||
img = w_line*np.sum(img,axis=2) \
|
||||
img = w_line*np.sum(img, axis=2) \
|
||||
+ w_edge*sum(edge)
|
||||
else:
|
||||
img = w_line*img + w_edge*edge[0]
|
||||
@@ -108,8 +110,8 @@ def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
np.arange(img.shape[0]), img.T, kx=2, ky=2, s=0)
|
||||
|
||||
x, y = snake[:, 0].copy(), snake[:, 1].copy()
|
||||
xsave = np.empty((convergence_order,len(x)))
|
||||
ysave = np.empty((convergence_order,len(x)))
|
||||
xsave = np.empty((convergence_order, len(x)))
|
||||
ysave = np.empty((convergence_order, len(x)))
|
||||
|
||||
# Build snake shape matrix
|
||||
n = len(x)
|
||||
@@ -187,9 +189,9 @@ def active_contour_model(image, snake, alpha=0.01, beta=0.1,
|
||||
|
||||
# Convergence criteria:
|
||||
j = i%(convergence_order+1)
|
||||
if j<convergence_order:
|
||||
xsave[j,:] = x
|
||||
ysave[j,:] = y
|
||||
if j < convergence_order:
|
||||
xsave[j, :] = x
|
||||
ysave[j, :] = y
|
||||
else:
|
||||
dist = np.min(np.max(np.abs(xsave-x[None, :])
|
||||
+ np.abs(ysave-y[None, :]), 1))
|
||||
|
||||
@@ -1,97 +1,97 @@
|
||||
import numpy as np
|
||||
from skimage import data
|
||||
from skimage.color import rgb2gray
|
||||
from skimage.filters import gaussian_filter, sobel
|
||||
from skimage.filters import gaussian_filter
|
||||
from skimage.segmentation import active_contour_model
|
||||
from numpy.testing import assert_equal, assert_allclose, assert_raises
|
||||
|
||||
def periodic_reference_test():
|
||||
img = data.astronaut()
|
||||
img = rgb2gray(img)
|
||||
s = np.linspace(0,2*np.pi,400)
|
||||
s = np.linspace(0, 2*np.pi, 400)
|
||||
x = 220 + 100*np.cos(s)
|
||||
y = 100 + 100*np.sin(s)
|
||||
init = np.array([x, y]).T
|
||||
snake = active_contour_model(gaussian_filter(img,3), init,
|
||||
snake = active_contour_model(gaussian_filter(img, 3), init,
|
||||
alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001)
|
||||
refx = [299, 298, 298, 298, 298, 297, 297, 296, 296, 295]
|
||||
refy = [98, 99, 100, 101, 102, 103, 104, 105, 106, 108]
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
|
||||
|
||||
def fixed_reference_test():
|
||||
img = data.text()
|
||||
x = np.linspace(5,424,100)
|
||||
y = np.linspace(136,50,100)
|
||||
x = np.linspace(5, 424, 100)
|
||||
y = np.linspace(136, 50, 100)
|
||||
init = np.array([x, y]).T
|
||||
snake = active_contour_model(gaussian_filter(img,1), init, bc='fixed',
|
||||
snake = active_contour_model(gaussian_filter(img, 1), init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
refx = [5, 9, 13, 17, 21, 25, 30, 34, 38, 42]
|
||||
refy = [136, 135, 134, 133, 132, 131, 129, 128, 127, 125]
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
|
||||
|
||||
def free_reference_test():
|
||||
img = data.text()
|
||||
x = np.linspace(5,424,100)
|
||||
y = np.linspace(70,40,100)
|
||||
x = np.linspace(5, 424, 100)
|
||||
y = np.linspace(70, 40, 100)
|
||||
init = np.array([x, y]).T
|
||||
snake = active_contour_model(gaussian_filter(img,3), init, bc='free',
|
||||
snake = active_contour_model(gaussian_filter(img, 3), init, bc='free',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
refx = [10, 13, 16, 19, 23, 26, 29, 32, 36, 39]
|
||||
refy = [76, 76, 75, 74, 73, 72, 71, 70, 69, 69]
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
|
||||
|
||||
def RGB_test():
|
||||
img = gaussian_filter(data.text(),1)
|
||||
imgR = np.zeros((img.shape[0],img.shape[1],3))
|
||||
imgG = np.zeros((img.shape[0],img.shape[1],3))
|
||||
imgRGB = np.zeros((img.shape[0],img.shape[1],3))
|
||||
imgR[:,:,0] = img
|
||||
imgG[:,:,1] = img
|
||||
imgRGB[:,:,:] = img[:, :, None]
|
||||
x = np.linspace(5,424,100)
|
||||
y = np.linspace(136,50,100)
|
||||
img = gaussian_filter(data.text(), 1)
|
||||
imgR = np.zeros((img.shape[0], img.shape[1], 3))
|
||||
imgG = np.zeros((img.shape[0], img.shape[1], 3))
|
||||
imgRGB = np.zeros((img.shape[0], img.shape[1], 3))
|
||||
imgR[:, :, 0] = img
|
||||
imgG[:, :, 1] = img
|
||||
imgRGB[:, :, :] = img[:, :, None]
|
||||
x = np.linspace(5, 424, 100)
|
||||
y = np.linspace(136, 50, 100)
|
||||
init = np.array([x, y]).T
|
||||
snake = active_contour_model(imgR, init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
refx = [5, 9, 13, 17, 21, 25, 30, 34, 38, 42]
|
||||
refy = [136, 135, 134, 133, 132, 131, 129, 128, 127, 125]
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
snake = active_contour_model(imgG, init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5, w_edge=0, gamma=0.1)
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
snake = active_contour_model(imgRGB, init, bc='fixed',
|
||||
alpha=0.1, beta=1.0, w_line=-5/3., w_edge=0, gamma=0.1)
|
||||
assert_equal(np.array(snake[:10,0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10,1], dtype=np.int32), refy)
|
||||
assert_equal(np.array(snake[:10, 0], dtype=np.int32), refx)
|
||||
assert_equal(np.array(snake[:10, 1], dtype=np.int32), refy)
|
||||
|
||||
|
||||
def end_points_tests():
|
||||
img = data.astronaut()
|
||||
img = rgb2gray(img)
|
||||
s = np.linspace(0,2*np.pi,400)
|
||||
s = np.linspace(0, 2*np.pi, 400)
|
||||
x = 220 + 100*np.cos(s)
|
||||
y = 100 + 100*np.sin(s)
|
||||
init = np.array([x, y]).T
|
||||
snake = active_contour_model(gaussian_filter(img,3), init,
|
||||
snake = active_contour_model(gaussian_filter(img, 3), init,
|
||||
bc='periodic', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001,
|
||||
max_iterations=100)
|
||||
assert np.sum(np.abs(snake[0,:]-snake[-1,:]) ) < 2
|
||||
snake = active_contour_model(gaussian_filter(img,3), init,
|
||||
assert np.sum(np.abs(snake[0, :]-snake[-1, :])) < 2
|
||||
snake = active_contour_model(gaussian_filter(img, 3), init,
|
||||
bc='free', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001,
|
||||
max_iterations=100)
|
||||
assert np.sum(np.abs(snake[0,:]-snake[-1,:])) > 2
|
||||
snake = active_contour_model(gaussian_filter(img,3), init,
|
||||
assert np.sum(np.abs(snake[0, :]-snake[-1, :])) > 2
|
||||
snake = active_contour_model(gaussian_filter(img, 3), init,
|
||||
bc='fixed', alpha=0.015, beta=10, w_line=0, w_edge=1, gamma=0.001,
|
||||
max_iterations=100)
|
||||
assert_allclose(snake[0,:], [x[0], y[0]], atol=1e-5)
|
||||
assert_allclose(snake[0, :], [x[0], y[0]], atol=1e-5)
|
||||
|
||||
|
||||
def bad_input_tests():
|
||||
@@ -99,9 +99,9 @@ def bad_input_tests():
|
||||
x = np.linspace(5, 424, 100)
|
||||
y = np.linspace(136, 50, 100)
|
||||
init = np.array([x, y]).T
|
||||
np.testing.assert_raises(ValueError, active_contour_model, img, init,
|
||||
assert_raises(ValueError, active_contour_model, img, init,
|
||||
bc='wrong')
|
||||
np.testing.assert_raises(ValueError, active_contour_model, img, init,
|
||||
assert_raises(ValueError, active_contour_model, img, init,
|
||||
max_iterations=-15)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user