import numpy as np
import mysubroutines as mrt
import WL2D
import matplotlib.pyplot as plt
def gen_A(dt,mu,X,Y):
def A(u):
return u-mu*dt*(np.roll(mrt.dx(mrt.dx(u,'f')/X,'f'),1)+np.roll(mrt.dy(mrt.dy(u,'f')/Y,'f'),1,0))
return A
def CV(I,u,T,N,options,n_plot=20):
mu = options['mu']
nu = options['nu']
lamb1 = options['lamb1']
lamb2 = options['lamb2']
n= options['n']
eps=options['eps']
tol=options['tol']
dt = T/N
for i in range(N):
Hu = mrt.heaviside(u,eps)
c1 = np.sum(I*Hu)/(np.sum(Hu))
c2 = np.sum(I*(1.-Hu))/(np.sum(1.- Hu))
dx = mrt.dx(u,'f')
dy = mrt.dy(u,'f')
dx_c = mrt.dx(u,'c')
dy_c = mrt.dy(u,'c')
u_dirac = dt*mrt.dirac(u,eps)
b = u+u_dirac*(-nu-lamb1*(I-c1)**2+lamb2*(I-c2)**2)
X = np.sqrt(dx**2+dx_c**2+1e-10)
Y = np.sqrt(dy**2+dy_c**2+1e-10)
A = gen_A(u_dirac,mu,X,Y)
u = mrt.CG(A,b,u,tol)
if (i+1)%n ==0:
u = mrt.Reinitial2D(u,10)
if (i+1)%n_plot ==0:
plt.imshow(u>0,cmap='gray')
plt.axis('off')
plt.show()
return u
def threshold(x,lamb):
x_norm = np.sqrt(np.sum(x**2,-1))+1e-8
x = x*np.expand_dims(np.maximum(x_norm-lamb,0)/x_norm,-1)
return x
def CVC(I,u,options,n_plot=20):
mu = options['mu']
N = options['iter']
alpha = options['alpha']
lamb = options['lamb']
eps = options['eps']
tol = options['tol']
method = options['method']
g = 1./(1+alpha*np.sum(mrt.grad(I)**2,-1))
H,W = np.shape(I)
b = np.zeros((H,W,2))
d = np.zeros((H,W,2))
eps = eps*H*W
for i in range(N):
Hu = mrt.heaviside(u,eps)
c1 = np.sum(I*Hu)/(np.sum(Hu))
c2 = np.sum(I*(1.-Hu))/(np.sum(1.- Hu))
r = (I-c1)**2-(I-c2)**2
s = mrt.div(d-b)
if method == 'grad':
for j in range(options['inner']):
newu = np.minimum(np.maximum(u-options['beta']*(mu*r+lamb*(s-mrt.div(mrt.grad(u,'f')))),0.),1.)
if np.sqrt(np.sum((newu-u)**2))<tol:
u=newu
break
u=newu
du = mrt.grad(u,'f')
d = threshold(du+b,g/lamb)
err = du-d
if (i+1)%n_plot ==0:
plt.imshow(u>0,cmap='gray')
plt.axis('off')
plt.show()
if np.sqrt(np.sum(err**2))<eps:
break
b += err
return u
def LSM(I,u,T,N,options,n_plot=20):
alpha = options['alpha']
K = options['K']
n= options['n']
dt = T/N
grad_I = mrt.grad(I,'c')
norm_gI = np.sqrt(np.sum(grad_I**2,-1))
gnI = mrt.g(norm_gI,K)
grad_g = mrt.grad(gnI,'c')
for i in range(N):
dx = mrt.dx(u,'f')
dy = mrt.dy(u,'f')
dx_c = mrt.dx(u,'c')
dy_c = mrt.dy(u,'c')
norm_g = np.sqrt(dx**2+dy**2)
K = np.roll(mrt.dx(dx/(norm_g+1e-10),'f'),1)+np.roll(mrt.dy(dy/(norm_g+1e-10),'f'),1,0)
v1 = gnI*np.sqrt(dx_c**2+dy_c**2)*K
v2 = alpha*gnI*np.sqrt(np.minimum(dx,0)**2+np.minimum(dy,0)**2\
+np.maximum(np.roll(dx,1),0)**2+np.maximum(np.roll(dy,1,0),0)**2)
v3 = np.minimum(grad_g[:,:,0],0)*dx+np.minimum(grad_g[:,:,1],0)*dy\
+np.maximum(grad_g[:,:,1],0)*np.roll(dy,1,0)+np.maximum(grad_g[:,:,0],0)*np.roll(dx,1)
u = u+dt*(v1+v2+v3)
if (i+1)%n ==0:
u = mrt.Reinitial2D(u,10)
if (i+1)%n_plot ==0:
plt.imshow(u>0,cmap='gray')
plt.axis('off')
plt.show()
return u
def FBSM(I,u,name,options,n_plot=20):
mu = options['mu']
lamb = options['lamb']
gamma = options['gamma']
N = options['iter']
eps = options['eps']
wlparam = options['wl']
frame= wlparam['frame']
Level= wlparam['L']
D,R = WL2D.GenerateFrameletFilter(frame)
delta = 1.618
d = WL2D.FraDecMultiLevel2D(u,D,Level)
b = np.zeros_like(d)
H,W = np.shape(I)
eps = eps*H*W
c1 = 0
c2 = 0
xi = (I-c1)**2-(I-c2)**2
for i in range(N):
u = WL2D.FraRecMultiLevel2D(d-b,R,Level)-xi/mu
u = np.maximum(np.minimum(u,1.),0.)
g = WL2D.FraDecMultiLevel2D(u,D,Level)
d = WL2D.soft_thresh(g+b,lamb/mu)
err = g - d
if (i+1)%n_plot ==0:
plt.imshow(u>0,cmap='gray')
plt.axis('off')
plt.show()
if np.sqrt(np.sum(err**2))<eps and i>0:
break
b += delta*err
c1 = np.mean((u>gamma)*I)
c2 = np.mean((u<=gamma)*I)
xi = (I-c1)**2-(I-c2)**2
return u
seg.rar_swinghnh_图像分割_小波 python
版权申诉
9 浏览量
2022-09-22
14:41:25
上传
评论
收藏 5KB RAR 举报
刘良运
- 粉丝: 69
- 资源: 1万+
最新资源
- 基于JavaScript和CSS的随寻订购网页设计源码 - web-order
- 基于MATLAB的声纹识别系统设计源码 - VoiceprintRecognition
- 基于Java的微服务插件集合设计源码 - wsy-plugins
- 基于Vue和微信小程序的监理日志系统设计源码 - supervisionLog
- 基于Java和LCN分布式事务框架的设计源码 - tx-lcn
- 基于Java和JavaScript的茶叶评级管理系统设计源码 - tea
- IMG_5680.JPG
- IMG_0437.jpg
- 基于Java的JAVA项目分析工具设计源码 - JAVAProjectAnalysis
- top888.json
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈