# Level Set图像分割方法及python实例

Level Set 模型介绍

Level Set是基于能量的图像分割方法，通过求解最小能量泛函，得到目标轮廓的表达式。其轮廓表示借鉴了水平集的概念，将低维度的曲线嵌入了高维度的曲面中。

### 二 . 水平集思想

```from scipy.misc import imread
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import scipy.ndimage.filters as filters
from skimage import measure
import lv_set.drlse_algo as drlse
import numpy as np
import cv2

# im_t = img[:, :, 1]
# plt.imshow(img)
# plt.show()
# parameters
timestep = 1        # time step
mu = 0.2/timestep   # coefficient of the distance regularization term R(phi)
iter_inner = 4
iter_outer = 25
lmda = 2            # coefficient of the weighted length term L(phi)
alfa = -9           # coefficient of the weighted area term A(phi)
epsilon = 2.0       # parameter that specifies the width of the DiracDelta function

sigma = 0.8         # scale parameter in Gaussian kernel
img_smooth = filters.gaussian_filter(img, sigma)    # smooth image by Gaussian convolution
f = np.square(Ix) + np.square(Iy)
g = 1 / (1+f)    # edge indicator function.

# initialize LSF as binary step function
c0 = 2
initialLSF = c0 * np.ones(img.shape)
cv2.imwrite('initialLSF.png', initialLSF)
# generate the initial region R0 as two rectangles
# initialLSF[24:35, 19:25] = -c0
print('initialLSF.shape', initialLSF.shape)
initialLSF[24:35, 20:26] = -c0
phi = initialLSF.copy()
cv2.imwrite('phi.png', phi)
plt.ion()
fig1 = plt.figure(1)
# plt.imshow(phi)
# plt.show()
# plt.pause(30)
def show_fig1():
y, x = phi.shape
x = np.arange(0, x, 1)
y = np.arange(0, y, 1)
X, Y = np.meshgrid(x, y)
ax1.plot_surface(X, Y, -phi, rstride=2, cstride=2, color='r', linewidth=0, alpha=0.6, antialiased=True)
ax1.contour(X, Y, phi, 0, colors='g', linewidths=2)

show_fig1()
fig2 = plt.figure(2)

def show_fig2():
contours = measure.find_contours(phi, 0)
ax2.imshow(img, interpolation='nearest', cmap=plt.cm.gray)
for n, contour in enumerate(contours):
ax2.plot(contour[:, 1], contour[:, 0], linewidth=2)

show_fig2()
print('show fig 2 first time')

potential = 2
if potential == 1:
potentialFunction = 'single-well'  # use single well potential p1(s)=0.5*(s-1)^2, which is good for region-based model
elif potential == 2:
potentialFunction = 'double-well'  # use double-well potential in Eq. (16), which is good for both edge and region based models
else:
potentialFunction = 'double-well'  # default choice of potential function

# start level set evolution
for n in range(iter_outer):
phi = drlse.drlse_edge(phi, g, lmda, mu, alfa, epsilon, timestep, iter_inner, potentialFunction)
if np.mod(n, 2) == 0:
print('show fig 2 for %i time' % n)
fig2.clf()
show_fig2()
fig1.clf()
show_fig1()
plt.pause(10)

# refine the zero level contour by further level set evolution with alfa=0
alfa = 0
iter_refine = 10
phi = drlse.drlse_edge(phi, g, lmda, mu, alfa, epsilon, timestep, iter_refine, potentialFunction)

finalLSF = phi.copy()
print('show final fig 2')
fig2.clf()
show_fig2()
fig1.clf()
show_fig1()
plt.pause(10)
plt.show()```

0 条评论

• ### 强化学习（三）算法概述

前面的文章我们已经介绍过强化学习的强化学习（一）入门介绍和强化学习（二）重要概念，这篇文章介绍强化学习的算法概述。

• ### 2018年全国计算机二级Python大纲

为了顺应大数据和人工智能的发展，Python作为最重要的编程语言被纳入全国计算机等级考试中，显得理所应当了，而考试的目的也是作为Python编程水平的一个相对公...

• ### 计算机二级Python考点解析2

第二部分：程序的基本语法元素：基本输入输出函数：input()、eval()、print();源程序的书写风格;Python语言的特点。

• ### Python进阶之Matplotlib入门(八)

Matplotlib是Python的画图领域使用最广泛的绘图库，它能让使用者很轻松地将数据图形化以及利用它可以画出许多高质量的图像，是用Python画图的必备技...

• ### 【须弥SUMERU】宜信分布式安全服务编排实践

在笔者看来，安全防御的本质之一是增加攻击者的攻击成本，尤其是时间成本。那么从防御的角度来说，如何尽早和及时地发现潜在的安全风险变得尤为重要，因此安全扫描对时效性...

• ### netty案例，netty4.1中级拓展篇十《Netty接收发送多种协议消息类型的通信处理方案》

在我们实际做应用级开发的过程中，客户端与服务端需要发送多种消息类型，比如一个聊天室场景包括的消息类型；登录验证、组建群聊、发送消息、退出登录等等，但如果我们都是...