《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)

python去噪算法的更多相关文章

  1. Atitit.java图片图像处理attilax总结  BufferedImage extends java.awt.Image获取图像像素点image.getRGB(i, lineIndex); 图片剪辑/AtiPlatf_cms/src/com/attilax/img/imgx.javacutImage图片处理titit 判断判断一张图片是否包含另一张小图片 atitit 图片去噪算法的原理与

    Atitit.java图片图像处理attilax总结 BufferedImage extends java.awt.Image 获取图像像素点 image.getRGB(i, lineIndex); ...

  2. Python基础算法综合:加减乘除四则运算方法

    #!usr/bin/env python# -*- coding:utf-8 -*-#python的算法加减乘除用符号:+,-,*,/来表示#以下全是python2.x写法,3.x以上请在python ...

  3. 三维网格去噪算法(L0 Minimization)

    [He et al. 2013]文章提出了一种基于L0范数最小化的三角网格去噪算法.该思想最初是由[Xu et al. 2011]提出并应用于图像平滑,假设c为图像像素的颜色向量,▽c为颜色向量的梯度 ...

  4. 三维网格去噪算法(two-step framework)

    基于两步法的网格去噪算法顾名思义包含两个步骤:首先对网格表面的法向进行滤波,得到调整后的网格法向信息,然后根据调整后的法向更新顶点坐标位置,下面介绍三篇该类型的文章. [Sun et al. 2007 ...

  5. 三维网格去噪算法(bilateral filter)

    受图像双边滤波算法的启发,[Fleishman et al. 2003]和[Jones et al. 2003]分别提出了利用双边滤波算法对噪声网格进行光顺去噪的算法,两篇文章都被收录于当年的SIGG ...

  6. xsank的快餐 » Python simhash算法解决字符串相似问题

    xsank的快餐 » Python simhash算法解决字符串相似问题 Python simhash算法解决字符串相似问题

  7. python聚类算法实战详细笔记 (python3.6+(win10、Linux))

    python聚类算法实战详细笔记 (python3.6+(win10.Linux)) 一.基本概念:     1.计算TF-DIF TF-IDF是一种统计方法,用以评估一字词对于一个文件集或一个语料库 ...

  8. python排序算法实现(冒泡、选择、插入)

    python排序算法实现(冒泡.选择.插入) python 从小到大排序 1.冒泡排序: O(n2) s=[3,4,2,5,1,9] #count = 0 for i in range(len(s)) ...

  9. Python C3 算法 手动计算顺序

    Python C3 算法 手动计算顺序   手动计算类继承C3算法原则: 以所求类的直接子类的数目分成相应部分 按照从左往右的顺序依次写出继承关系 继承关系第一个第一位,在所有后面关系都是第一个出现的 ...

随机推荐

  1. 10.4 android输入系统_框架、编写一个万能模拟输入驱动程序、reader/dispatcher线程启动过程源码分析

    1. 输入系统框架 android输入系统官方文档 // 需FQhttp://source.android.com/devices/input/index.html <深入理解Android 卷 ...

  2. OC学习篇之---协议的概念和用法

    这一篇文章我们在来看一下OC中协议的概念以及用法,协议也是OC中的一个重点,Foundation框架以及我们后面在写代码都会用到. OC中的协议就是相当于Java中的接口(抽象类),只不过OC中的名字 ...

  3. 【BZOJ 2754】[SCOI2012]喵星球上的点名

    [链接]h在这里写链接 [题意]     n个人;     由姓和名组成.s1[i]和s2[i];     有m个询问串.     问你第j个询问串,是否为某个人的姓或者名的子串.     如果是的话 ...

  4. redis学习笔记之虚拟内存

    首先说明下redis的虚拟内存与os的虚拟内存不是一码事,但是思路和目的都是相同的.就是暂时把不经常访问的数据从内存交换到磁盘中,从而腾出宝贵的 内存空间用于其他需要访问的数据.尤其是对于redis这 ...

  5. ANSCII码和BCD码互转

    bool AtoBCD(unsigned char* Asc,unsigned char* BCD,int len) { int i; unsigned char ch; //高位 unsigned ...

  6. Swift 带有动画效果的TabBarItem

    额...貌似挺长时间没有总结新知识了,最近在看swift,之前swift刚出来的时候大体看了一遍,后来时间长了没看加之swift2.0做了比较大的调整,公司项目也不是用swift写的,也就没怎么看了, ...

  7. Docker CE for Windows安装使用

    原文:Docker CE for Windows安装使用 官网下载并安装Docker CE for Windows IDEA连接Docker Docker一些常用命令 Docker for windo ...

  8. form表单上传附件的几种方法

    问题描述:在网页开发过程中,当需要上传附件(图片,音频,视频等)时,常规方法是使用form表单进行提交,这里总结一下form表单提交的几种方法. 参考地址:http://www.cnblogs.com ...

  9. 网络编程02---HTTP协议

    1.URL简单介绍 1.client怎样找到server 我们都知道网络中部署着各种各样的server.比方腾讯的server.百度的server.那么问题来了.client怎样找到想要连接的serv ...

  10. 给自己加油,一定要学会MFC!(刚刚发现一篇文章,兼听则明: MFC,一开始就错了)

    我自己对于没有学会MFC始终耿耿于怀,都什么时代了啊,但是我仍然坚持会去学MFC,因为MFC虽然落后与复杂,但是在Windows平台上仍然是无所不能的(其实Windows平台仍然是唯一可以赚钱的平台, ...