chan-vese模型

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

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38784098/article/details/82144106

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模型)代码:

  1.  
    import cv2
  2.  
    from pylab import*
  3.  
     
  4.  
    Image = cv2.imread('02.jpg', 1) # 读入原图
  5.  
    image = cv2.cvtColor(Image, cv2.COLOR_BGR2GRAY)
  6.  
    img = np.array(image, dtype=np.float64) # 读入到np的array中,并转化浮点类型
  7.  
     
  8.  
    # 初始水平集函数
  9.  
    IniLSF = np.ones((img.shape[0], img.shape[1]), img.dtype)
  10.  
    IniLSF[300:320, 300:320] = -1
  11.  
    IniLSF = -IniLSF
  12.  
     
  13.  
    # 画初始轮廓
  14.  
    Image = cv2.cvtColor(Image, cv2.COLOR_BGR2RGB)
  15.  
    plt.figure(1), plt.imshow(Image), plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
  16.  
    plt.contour(IniLSF, [0], color='b', linewidth=2) # 画LSF=0处的等高线
  17.  
    plt.draw(), plt.show(block=False)
  18.  
     
  19.  
     
  20.  
    def mat_math(intput, str):
  21.  
    output = intput
  22.  
    for i in range(img.shape[0]):
  23.  
    for j in range(img.shape[1]):
  24.  
    if str == "atan":
  25.  
    output[i, j] = math.atan(intput[i, j])
  26.  
    if str == "sqrt":
  27.  
    output[i, j] = math.sqrt(intput[i, j])
  28.  
    return output
  29.  
     
  30.  
     
  31.  
    # CV函数
  32.  
    def CV(LSF, img, mu, nu, epison, step):
  33.  
     
  34.  
    Drc = (epison / math.pi) / (epison*epison + LSF*LSF)
  35.  
    Hea = 0.5*(1 + (2 / math.pi)*mat_math(LSF/epison, "atan"))
  36.  
    Iy, Ix = np.gradient(LSF)
  37.  
    s = mat_math(Ix*Ix+Iy*Iy, "sqrt")
  38.  
    Nx = Ix / (s+0.000001)
  39.  
    Ny = Iy / (s+0.000001)
  40.  
    Mxx, Nxx = np.gradient(Nx)
  41.  
    Nyy, Myy = np.gradient(Ny)
  42.  
    cur = Nxx + Nyy
  43.  
    Length = nu*Drc*cur
  44.  
     
  45.  
    Lap = cv2.Laplacian(LSF, -1)
  46.  
    Penalty = mu*(Lap - cur)
  47.  
     
  48.  
    s1 = Hea*img
  49.  
    s2 = (1-Hea)*img
  50.  
    s3 = 1-Hea
  51.  
    C1 = s1.sum() / Hea.sum()
  52.  
    C2 = s2.sum() / s3.sum()
  53.  
    CVterm = Drc*(-1 * (img - C1)*(img - C1) + 1 * (img - C2)*(img - C2))
  54.  
     
  55.  
    LSF = LSF + step*(Length + Penalty + CVterm)
  56.  
    # plt.imshow(s, cmap ='gray'),plt.show()
  57.  
    return LSF
  58.  
     
  59.  
    # 模型参数
  60.  
    mu = 1
  61.  
    nu = 0.003 * 255 * 255
  62.  
    num = 20
  63.  
    epison = 1
  64.  
    step = 0.1
  65.  
    LSF = IniLSF
  66.  
    for i in range(1, num):
  67.  
    LSF = CV(LSF, img, mu, nu, epison, step) # 迭代
  68.  
    if i % 1 == 0: # 显示分割轮廓
  69.  
    plt.imshow(Image), plt.xticks([]), plt.yticks([])
  70.  
    plt.contour(LSF, [0], colors='r', linewidth=2)
  71.  
    plt.draw(), plt.show(block=False), plt.pause(0.01)

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

一、文章参考

Chan T F, Vese L. Active contours without edges[J]. Image processing, IEEE transactions on, 2001, 10(2): 266-277.
1
二、公式推导过程

---------------------
作者:jonson_zc
来源:CSDN
原文:https://blog.csdn.net/zhangchen1003/article/details/48930377
版权声明:本文为博主原创文章,转载请附上博文链接!

原文地址:https://www.cnblogs.com/shuimuqingyang/p/10860719.html