图像处理------图像加噪

图像噪声源于现实世界中数字信号总会受到各种各样的干扰,最终接受的图像和源于的数字信号之间总

是存在一定的差异,对于图像噪声,使用均值滤波和中值滤波来消除图像噪声的做法已经是很常见的图

像消噪手段。

 

一:图像加噪原理

1.     椒盐噪声(Salt And Pepper Noise)

椒盐噪声是一种因为信号脉冲强度引起的噪声,信噪比(Signal NoiseRate)是衡量图像噪声的一个数字指标。

给一副数字图像加上椒盐噪声的处理顺序应该如下:

  1. 指定信噪比 SNR 其取值范围在[0, 1]之间
  2. 计算总像素数目 SP, 得到要加噪的像素数目 NP = SP * (1-SNR)
  3. 随机获取要加噪的每个像素位置P(i, j)
  4. 指定像素值为255或者0。
  5. 重复c, d两个步骤完成所有像素的NP个像素
  6. 输出加噪以后的图像

 

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.

 

三:程序效果如下


加入白色椒盐噪声的图片

 

加入高斯噪声的图片

椒盐噪声的代码如下:

[java] view plaincopy

  1. private BufferedImage addSaltAndPepperNoise(BufferedImage src, BufferedImage dst) {  
  2.     int width = src.getWidth();  
  3.        int height = src.getHeight();  
  4.   
  5.        if ( dst == null )  
  6.            dst = createCompatibleDestImage( src, null );  
  7.   
  8.        int[] inPixels = new int[width*height];  
  9.        getRGB( src, 0, 0, width, height, inPixels );  
  10.          
  11.        int index = 0;  
  12.        int size = (int)(inPixels.length * (1-SNR));  
  13.   
  14.        for(int i=0; i<size; i++) {  
  15.         int row = (int)(Math.random() * (double)height);  
  16.         int col = (int)(Math.random() * (double)width);  
  17.         index = row * width + col;  
  18.         inPixels[index] = (255 << 24) | (255 << 16) | (255 << 8) | 255;  
  19.        }  
  20.   
  21.        setRGB( dst, 0, 0, width, height, inPixels );  
  22.        return dst;  
  23. }  

高斯噪声的代码如下:

[java] view plaincopy

  1. private BufferedImage gaussianNoise(BufferedImage src, BufferedImage dst) {  
  2.         int width = src.getWidth();  
  3.         int height = src.getHeight();  
  4.   
  5.         if ( dst == null )  
  6.             dst = createCompatibleDestImage( src, null );  
  7.   
  8.         int[] inPixels = new int[width*height];  
  9.         int[][][] tempPixels = new int[height][width][4];   
  10.         int[] outPixels = new int[width*height];  
  11.         getRGB( src, 0, 0, width, height, inPixels );  
  12.         int index = 0;  
  13.         float inMax = 0;  
  14.         float outMax = 0;  
  15.         for(int row=0; row<height; row++) {  
  16.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  17.             for(int col=0; col<width; col++) {  
  18.                 index = row * width + col;  
  19.                 ta = (inPixels[index] >> 24) & 0xff;  
  20.                 tr = (inPixels[index] >> 16) & 0xff;  
  21.                 tg = (inPixels[index] >> 8) & 0xff;  
  22.                 tb = inPixels[index] & 0xff;  
  23.                 if(inMax < tr) {  
  24.                     inMax = tr;  
  25.                 }  
  26.                 if(inMax < tg) {  
  27.                     inMax = tg;  
  28.                 }  
  29.                 if(inMax < tb) {  
  30.                     inMax = tb;  
  31.                 }  
  32.                 tr = (int)((float)tr + getGaussianValue() + this.means);  
  33.                 tg = (int)((float)tg + getGaussianValue() + this.means);  
  34.                 tb = (int)((float)tb + getGaussianValue() + this.means);  
  35.                 if(outMax < tr) {  
  36.                     outMax = tr;  
  37.                 }  
  38.                 if(outMax < tg) {  
  39.                     outMax = tg;  
  40.                 }  
  41.                 if(outMax < tb) {  
  42.                     outMax = tb;  
  43.                 }  
  44.                 tempPixels[row][col][0] = ta;  
  45.                 tempPixels[row][col][1] = tr;  
  46.                 tempPixels[row][col][2] = tg;  
  47.                 tempPixels[row][col][3] = tb;  
  48.             }  
  49.         }  
  50.   
  51.         // Normalization  
  52.         index = 0;  
  53.         float rate = inMax/outMax;  
  54.         for(int row=0; row<height; row++) {  
  55.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  56.             for(int col=0; col<width; col++) {  
  57.                 index = row * width + col;  
  58.                 ta = tempPixels[row][col][0];  
  59.                 tr = tempPixels[row][col][1];  
  60.                 tg = tempPixels[row][col][2];  
  61.                 tb = tempPixels[row][col][3];  
  62.   
  63.                 tr = (int)((float)tr * rate);  
  64.                 tg = (int)((float)tg * rate);  
  65.                 tb = (int)((float)tb * rate);  
  66.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  67.             }  
  68.         }  
  69.         setRGB( dst, 0, 0, width, height, outPixels );  
  70.         return dst;  
  71.     }  
时间: 2024-08-02 17:17:44

图像处理------图像加噪的相关文章

图像加减运算,为什么输出结果不对

问题描述 图像加减运算,为什么输出结果不对 #include #include #include #include #include using namespace std; //图像加减运算 //unsigned char *lpSrc1:原图像1像素指针 //unsigned char *lpSrc2:原图像2像素指针 //unsigned char *lpDst:目标像素指针 int main() { IplImage * image1, *image2, *image3; image1

信道噪声-用labview进行2fsk通信系统的仿真,需要在信道中加噪然后进行观察

问题描述 用labview进行2fsk通信系统的仿真,需要在信道中加噪然后进行观察 哪位大神可以解答以下我的问题哒~需要在信道中添加高斯白噪声,然后观察眼图什么的,怎么加呢?是直接在调制.解调之间的那部分增加噪声就可以了吗?

matlab上使用gui时, 加载图像加载不出,好像是图像路径的错误,试了几张图也不行

问题描述 matlab上使用gui时, 加载图像加载不出,好像是图像路径的错误,试了几张图也不行 解决方案 http://zhidao.baidu.com/link?url=1wMAV1NZgvw3seT3q9Cbjs0E1mtI9KQ3LTfEuO2i_i8P0fh-jaYoEfnOTVOIpMp0yqBTuaVG9TUkqZsZt7CyJjMLRIr4hM1FReR1xY2nppK

matlab-求大神解答使用Matlab实现将一张自拍经FFT变换、加噪、去噪、IFFT变换后还原

问题描述 求大神解答使用Matlab实现将一张自拍经FFT变换.加噪.去噪.IFFT变换后还原 使用Matlab实现将一张自拍经FFT变换.加噪.去噪.IFFT变换后还原 解决方案 http://wenku.baidu.com/link?url=8WfgXxm-Im4_0QjMekDjs02pGbamtDN6x2OWRRhR6GQlN1KXV3wm3yCQCpmshYAHPtRTnyf5BSe6DFgZtjv7CST17VfseKgIgmjz1BGHwp3

图像处理-图像数据怎么从内存加载到CImage类,哪个函数可用?

问题描述 图像数据怎么从内存加载到CImage类,哪个函数可用? 比如说现在有个RGBQUAD类型的数组,怎么把里面的图象数据转换成CImage类图像,难道只有用指针循环取像素值然后赋值吗? 解决方案 RGBQUAD类型不知道你说的是什么,但是CImage其实就是内存中的一种. 解决方案二: CreateDIBitmap () ??

ImageTTFText函数实现图像加文字水印

一个ImageTTFText函数包含了如下的参数,imagettftext (image,size,angle, x, y,color,fontfile,text)意思是 imagettftext() 将字符串 text 画到 image 所代表的图像上,从坐标 x,y(左上角为 0, 0)开始,角度为 angle,颜色为 color,使用 fontfile 所指定的 TrueType 字体文件. 根据 PHP 所使用的 GD 库的不同,如果 fontfile 没有以 '/'开头,则 '.ttf

PHP:上传图像程序,为图像加水印

程序|上传 <?php$uptypes=array('image/jpg',   //上传文件类型列表    'image/jpeg',     'image/png',     'image/pjpeg',    'image/gif',    'image/bmp',    'image/x-png'); $max_file_size=2000000;     //上传文件大小限制, 单位BYTE$destination_folder="uploadimg/"; //上传文件

图像处理------图像梯度效果

基本思想: 利用X方向与Y方向分别实现一阶微分,求取振幅,实现图像梯度效果. 使用的两种微分算子分别为Prewitt与Sobel,其中Soble在X, Y两个方向算子分别为: Prewitt在X, Y方向上梯度算子分别为: 二:程序思路及实现 梯度滤镜提供了两个参数: – 方向,用来要决定图像完成X方向梯度计算, Y方向梯度计算,或者是振幅计算 – 算子类型,用来决定是使用sobel算子或者是prewitt算子. 计算振幅的公式可以参见以前<图像处理之一阶微分应用>的文章  三:运行效果 原图

图像处理-图像二维匹配和立体匹配的区别?

问题描述 图像二维匹配和立体匹配的区别? 图像的二维匹配是两幅照片之间的特征点的匹配,但是立体匹配 也是两幅照片的匹配然后生成视差值,得到深度图,为什么前者就是二维匹配,而后者却叫立体匹配(立体也就意味是三维匹配),这是为什么呢,三维匹配不应该是点云匹配才叫三维吗