《programming computer vision with python 》中denoise 算法有误,从网上好了可用的代码贴上,以便以后使用。

书中错误的代码:

def denoise(im,U_init,tolerance=0.1,tau=0.125,tv_weight=100):
m,n = im.shape
U = U_init
Px = im
Py = im
error = 1 while (error > tolerance):
Uold = U
GradUx = roll(U,-1,axis=1)-U
GradUy = roll(U,-1,axis=0)-U PxNew = Px + (tau/tv_weight)*GradUx
PyNew = Py + (tau/tv_weight)*GradUy
NormNew = maximum(1,sqrt(PxNew**2+PyNew**2)) Px = PxNew/NormNew
py = PyNew/NormNew RxPx = roll(Px,1,axis=1)
RyPy = roll(Py,1,axis=0) DivP = (Px - RxPx) + (Py - RyPy)
U = im + tv_weight*DivP error = linalg.norm(U-Uold)/sqrt(n*m)
return U,im-U

网上可用的代码:

def denoise(im, U_init, tolerance=0.1, tau=0.125, tv_weight=100):
""" An implementation of the Rudin-Osher-Fatemi (ROF) denoising model
using the numerical procedure presented in Eq. (11) of A. Chambolle
(2005). Implemented using periodic boundary conditions
(essentially turning the rectangular image domain into a torus!). Input:
im - noisy input image (grayscale)
U_init - initial guess for U
tv_weight - weight of the TV-regularizing term
tau - steplength in the Chambolle algorithm
tolerance - tolerance for determining the stop criterion Output:
U - denoised and detextured image (also the primal variable)
T - texture residual""" #---Initialization
m,n = im.shape #size of noisy image U = U_init
Px = im #x-component to the dual field
Py = im #y-component of the dual field
error = 1
iteration = 0 #---Main iteration
while (error > tolerance):
Uold = U #Gradient of primal variable
LyU = vstack((U[1:,:],U[0,:])) #Left translation w.r.t. the y-direction
LxU = hstack((U[:,1:],U.take([0],axis=1))) #Left translation w.r.t. the x-direction GradUx = LxU-U #x-component of U's gradient
GradUy = LyU-U #y-component of U's gradient #First we update the dual varible
PxNew = Px + (tau/tv_weight)*GradUx #Non-normalized update of x-component (dual)
PyNew = Py + (tau/tv_weight)*GradUy #Non-normalized update of y-component (dual)
NormNew = maximum(1,sqrt(PxNew**2+PyNew**2)) Px = PxNew/NormNew #Update of x-component (dual)
Py = PyNew/NormNew #Update of y-component (dual) #Then we update the primal variable
RxPx =hstack((Px.take([-1],axis=1),Px[:,0:-1])) #Right x-translation of x-component
RyPy = vstack((Py[-1,:],Py[0:-1,:])) #Right y-translation of y-component
DivP = (Px-RxPx)+(Py-RyPy) #Divergence of the dual field.
U = im + tv_weight*DivP #Update of the primal variable #Update of error-measure
error = linalg.norm(U-Uold)/sqrt(n*m);
iteration += 1; print iteration, error #The texture residual
T = im - U
print 'Number of ROF iterations: ', iteration return U,T

测试代码:

from numpy import *
from numpy import random
from scipy.ndimage import filters
import rof
from scipy.misc import imsave im = zeros((500,500))
im[100:400,100:400] = 128
im[200:300,200:300] = 255 im = im + 30*random.standard_normal((500,500)) imsave('synth_ori.pdf',im) U,T = rof.denoise(im,im,0.07) G = filters.gaussian_filter(im,10) imsave('synth_rof.pdf',U)
imsave('synth_gaussian.pdf',G)

最新文章

  1. 20145213《Java程序设计学习笔记》第六周学习总结
  2. dbca:Exception in thread "main" java.lang.UnsatisfiedLinkError: get
  3. 多线程 - CyclicBarrier
  4. Python设计模式——设计原则
  5. [转]NHibernate之旅(9):探索父子关系(一对多关系)
  6. HDU-1241Oil Deposits
  7. BZOJ 1497 最大获利(最大权闭合子图)
  8. Vim 基本配置和经常使用的命令
  9. nagios总结二
  10. HTML 5 Web 存储、应用程序缓存、Web Workers
  11. JavaScript 版数据结构与算法(四)集合
  12. MYSQL:alter语句中change和modify的区别
  13. libevent之event
  14. Pandas系列(一)-Series详解
  15. 什么是web标准??
  16. Zepto的使用以及注意事项
  17. There is no Action mapped for namespace / and action name .解答
  18. centos7更换镜像源
  19. Android游戏开发基本知识
  20. linux基础命令:

热门文章

  1. C#学习笔记——常量、字段以及事件
  2. python课程:python3函数
  3. [Angular] Angular Advanced Features - ng-template , ng-container, ngTemplateOutlet
  4. Apache与weblogic整合实战(独家研究)
  5. 8、hzk16的介绍以及简单的使用方法
  6. 神经网络 vs 大脑
  7. surfingkeys
  8. [JS Compose] 1. Refactor imperative code to a single composed expression using Box
  9. php实现 字符串加密(分类分布分工,化不可能为可能)
  10. php函数实现显示几秒前,几分钟前,几天前等方法(网络上什么都有)