Python--level set (水平集)和 chan-vese模型

level set :https://www.zhihu.com/question/22608763?sort=created

https://blog.csdn.net/xiangyong58/article/details/11876019

chan-vese模型(公式推导):https://blog.csdn.net/zhangchen1003/article/details/48930377

水平集(CV模型)代码:

import cv2
from pylab import*

Image = cv2.imread('02.jpg', 1)  # 读入原图
image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
img = np.array(image, dtype=np.float64)  # 读入到np的array中,并转化浮点类型

# 初始水平集函数
IniLSF = np.ones((img.shape[0], img.shape[1]), img.dtype)
IniLSF[300:320, 300:320] = -1
IniLSF = -IniLSF

# 画初始轮廓
Image = cv2.cvtColor(Image, cv2.COLOR_BGR2RGB)
plt.figure(1), plt.imshow(Image), plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
plt.contour(IniLSF, [0], color='b', linewidth=2)  # 画LSF=0处的等高线
plt.draw(), plt.show(block=False)


def mat_math(intput, str):
    output = intput
    for i in range(img.shape[0]):
        for j in range(img.shape[1]):
            if str == "atan":
                output[i, j] = math.atan(intput[i, j])
            if str == "sqrt":
                output[i, j] = math.sqrt(intput[i, j])
    return output


# CV函数
def CV(LSF, img, mu, nu, epison, step):

    Drc = (epison / math.pi) / (epison*epison + LSF*LSF)
    Hea = 0.5*(1 + (2 / math.pi)*mat_math(LSF/epison, "atan"))
    Iy, Ix = np.gradient(LSF)
    s = mat_math(Ix*Ix+Iy*Iy, "sqrt")
    Nx = Ix / (s+0.000001)
    Ny = Iy / (s+0.000001)
    Mxx, Nxx = np.gradient(Nx)
    Nyy, Myy = np.gradient(Ny)
    cur = Nxx + Nyy
    Length = nu*Drc*cur

    Lap = cv2.Laplacian(LSF, -1)
    Penalty = mu*(Lap - cur)

    s1 = Hea*img
    s2 = (1-Hea)*img
    s3 = 1-Hea
    C1 = s1.sum() / Hea.sum()
    C2 = s2.sum() / s3.sum()
    CVterm = Drc*(-1 * (img - C1)*(img - C1) + 1 * (img - C2)*(img - C2))

    LSF = LSF + step*(Length + Penalty + CVterm)
    # plt.imshow(s, cmap ='gray'),plt.show()
    return LSF

# 模型参数
mu = 1
nu = 0.003 * 255 * 255
num = 20
epison = 1
step = 0.1
LSF = IniLSF
for i in range(1, num):
    LSF = CV(LSF, img, mu, nu, epison, step)  # 迭代
    if i % 1 == 0:  # 显示分割轮廓
        plt.imshow(Image), plt.xticks([]), plt.yticks([])
        plt.contour(LSF, [0], colors='r', linewidth=2)
        plt.draw(), plt.show(block=False), plt.pause(0.01)

为什么上传图片这么麻烦。

  • 4
    点赞
  • 64
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GlassySky0816

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值