图像处理之图像加噪
来源:互联网 发布:淘宝联盟pid是什么 编辑:程序博客网 时间:2024/05/17 00:49
from:http://blog.csdn.net/jia20003/article/details/7181463
图像噪声源于现实世界中数字信号总会受到各种各样的干扰,最终接受的图像和源于的数字信号之间总
是存在一定的差异,对于图像噪声,使用均值滤波和中值滤波来消除图像噪声的做法已经是很常见的图
像消噪手段。
一:图像加噪原理
1. 椒盐噪声(Salt And Pepper Noise)
椒盐噪声是一种因为信号脉冲强度引起的噪声,信噪比(Signal NoiseRate)是衡量图像噪声的一个数字指标。
给一副数字图像加上椒盐噪声的处理顺序应该如下:
- 指定信噪比 SNR 其取值范围在[0, 1]之间
- 计算总像素数目 SP, 得到要加噪的像素数目 NP = SP * (1-SNR)
- 随机获取要加噪的每个像素位置P(i, j)
- 指定像素值为255或者0。
- 重复c, d两个步骤完成所有像素的NP个像素
- 输出加噪以后的图像
2. 高斯噪声(Gaussian Noise)
高斯噪声的密度取决于公式G(x, sigma) 其中X是代表平均值,sigma代表的标准方差,每个输入像素 Pin,
一个正常的高斯采样分布公式G(d), 得到输出像素Pout.
Pout = Pin + XMeans + sigma *G(d)
其中d为一个线性的随机数,G(d)是随机数的高斯分布随机值。
给一副数字图像加上高斯噪声的处理顺序如下:
a. 输入参数sigam 和 X mean
b. 以系统时间为种子产生一个伪随机数
c. 将伪随机数带入G(d)得到高斯随机数
d. 根据输入像素计算出输出像素
e. 重新将像素值防缩在[0 ~ 255]之间
f. 循环所有像素
g. 输出图像
二:关键程序解析
1. 椒盐噪声
根据信噪比,获取要加入椒盐噪声的像素数目
int size= (int)(inPixels.length * (1-SNR));
随机得到像素,完成椒盐噪声的加入
for(int i=0; i<size; i++) {
int row = (int)(Math.random()* (double)height);
int col = (int)(Math.random()* (double)width);
index= row * width + col;
inPixels[index]= (255 << 24) | (255 << 16) | (255 << 8) | 255;
}
2. 高斯噪声
根据标准方差,和伪随机数的范围,首先计算出一个伪随机数d ,根据d得到高斯分布的随机数值,整个代码如下:
float d = (float)Math.random()*RANDOM_SCOPE - RANDOM_SCOPE/2;
float sigma2 = sigma*sigma*2;
float PI2 = (float)Math.PI * 2;
float sigmaPI2 = (float)Math.sqrt(PI2*sigma);
float result = (float)Math.exp(-d/sigma2)/sigmaPI2;
伪随机数的范围为[-127~ 127]之间。
获取高斯噪声的像素代码如下:
tr = (int)((float)tr + getGaussianValue() + this.means);
tg = (int)((float)tg + getGaussianValue() + this.means);
tb = (int)((float)tb + getGaussianValue() + this.means);
mean是的值为0.
三:程序效果如下
加入白色椒盐噪声的图片
加入高斯噪声的图片
椒盐噪声的代码如下:
- private BufferedImage addSaltAndPepperNoise(BufferedImage src, BufferedImage dst) {
- int width = src.getWidth();
- int height = src.getHeight();
- if ( dst == null )
- dst = createCompatibleDestImage( src, null );
- int[] inPixels = new int[width*height];
- getRGB( src, 0, 0, width, height, inPixels );
- int index = 0;
- int size = (int)(inPixels.length * (1-SNR));
- for(int i=0; i<size; i++) {
- int row = (int)(Math.random() * (double)height);
- int col = (int)(Math.random() * (double)width);
- index = row * width + col;
- inPixels[index] = (255 << 24) | (255 << 16) | (255 << 8) | 255;
- }
- setRGB( dst, 0, 0, width, height, inPixels );
- return dst;
- }
高斯噪声的代码如下:
- private BufferedImage gaussianNoise(BufferedImage src, BufferedImage dst) {
- int width = src.getWidth();
- int height = src.getHeight();
- if ( dst == null )
- dst = createCompatibleDestImage( src, null );
- int[] inPixels = new int[width*height];
- int[][][] tempPixels = new int[height][width][4];
- int[] outPixels = new int[width*height];
- getRGB( src, 0, 0, width, height, inPixels );
- int index = 0;
- float inMax = 0;
- float outMax = 0;
- for(int row=0; row<height; row++) {
- int ta = 0, tr = 0, tg = 0, tb = 0;
- for(int col=0; col<width; col++) {
- index = row * width + col;
- ta = (inPixels[index] >> 24) & 0xff;
- tr = (inPixels[index] >> 16) & 0xff;
- tg = (inPixels[index] >> 8) & 0xff;
- tb = inPixels[index] & 0xff;
- if(inMax < tr) {
- inMax = tr;
- }
- if(inMax < tg) {
- inMax = tg;
- }
- if(inMax < tb) {
- inMax = tb;
- }
- tr = (int)((float)tr + getGaussianValue() + this.means);
- tg = (int)((float)tg + getGaussianValue() + this.means);
- tb = (int)((float)tb + getGaussianValue() + this.means);
- if(outMax < tr) {
- outMax = tr;
- }
- if(outMax < tg) {
- outMax = tg;
- }
- if(outMax < tb) {
- outMax = tb;
- }
- tempPixels[row][col][0] = ta;
- tempPixels[row][col][1] = tr;
- tempPixels[row][col][2] = tg;
- tempPixels[row][col][3] = tb;
- }
- }
- // Normalization
- index = 0;
- float rate = inMax/outMax;
- for(int row=0; row<height; row++) {
- int ta = 0, tr = 0, tg = 0, tb = 0;
- for(int col=0; col<width; col++) {
- index = row * width + col;
- ta = tempPixels[row][col][0];
- tr = tempPixels[row][col][1];
- tg = tempPixels[row][col][2];
- tb = tempPixels[row][col][3];
- tr = (int)((float)tr * rate);
- tg = (int)((float)tg * rate);
- tb = (int)((float)tb * rate);
- outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;
- }
- }
- setRGB( dst, 0, 0, width, height, outPixels );
- return dst;
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 上一篇在Android上实现图像颜色过滤与反转
- 下一篇超纯超美的曲线(Peter De Jong Attractor)
- 顶
- 1
- 踩
- 0
- 主题推荐
- 图像处理
- 猜你在找
- NinePatch图片制作从入门到精通
- C语言及程序设计提高
- C语言及程序设计初步
- C语言及程序设计进阶
- JavaScript for Qt Quick(QML)
- 6楼 金友彬2014-03-13 17:35发表 [回复]
- 你好~上面的两个源代码~我非常非常需要~ 请问能给到我的邮箱吗?flowerkim1991@gmail.com 非常感谢
- 5楼 青石桥边2014-02-09 22:40发表 [回复]
- 请问一下加入椒盐噪声的时候为什么只有白点,没有黑点?还有setRGB的具体方法您是怎么写的?
- 4楼 a2632383862013-09-11 20:09发表 [回复]
- 一篇文章中最重要的 getGaussianValue()没放出来啊。。
- 3楼 saozen2012-11-28 21:11发表 [回复]
- 请问有完整代码提供学习么?
- 2楼 JY_L2012-07-23 16:10发表 [回复]
- 你好,请教一下怎么把该列应用到android上呢?
- Re: gloomyfish2012-07-23 22:27发表 [回复]
- getGaussianValue()是取得下个随机生成的高斯分布值,means是调整系数,具体建议你看看高斯噪声的理论回复meiyan0622:
- Re: JY_L2012-07-23 22:31发表 [回复]
- 回复jia20003:哦 谢谢了 能不能给我推荐一下文档啊 我还是学生知识面还是太窄了
- 1楼 JY_L2012-07-22 14:37发表 [回复]
- 请问一下getGaussianValue()和this.means是什么啊 它俩的各自的含义是什么啊?
- 图像处理之图像加噪
- 图像处理之图像加噪
- 图像处理------图像加噪
- 图像处理------图像加噪
- 图像处理 之 图像保存
- 图像处理之图像复原
- 图像处理之旋转图像
- 图像处理之图像平滑
- 图像处理之图像金字塔
- 图像处理之图像直方图
- 图像处理之图像距
- 图像处理之二值化图像
- 图像处理之分割图像
- matlab图像加噪
- 图像加噪
- 【Android图像处理】图像处理之-噪点特效
- 【Android图像处理】图像处理之-哈哈镜
- 图像加噪和图像滤波
- 欢迎使用CSDN-markdown编辑器
- 虚拟化催生变革 IP网络走向管理自动化
- 浅谈UIScrollView
- 如何将win7系统的电脑屏幕改成护眼的豆沙绿
- web测试方法总结(一)
- 图像处理之图像加噪
- php 计算3公里内 用户的距离
- 在11g安装oracle text组件
- 添加一个矩形背景与一个圆形背景
- 一台机器开多个tomcat
- 数据库中的升序和降序
- 打开小米安全中心的权限管理
- web测试方法总结(二)
- Weblogic Server Exception: weblogic.management.ManagementException: [Deployer:149007]