图像处理之图像加噪

在线体验各类最新模型,更有模型 免费Token 额度领取!
立即体验
简介: 图像处理之图像加噪

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

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

像消噪手段。

一:图像加噪原理

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

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


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

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

 

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.

三:程序效果如下


加入白色椒盐噪声的图片

0_1325835760jBTo.png

加入高斯噪声的图片

0_1325835782ATz1.png


椒盐噪声的代码如下:

  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;
  }

相关文章
|
数据采集 PyTorch 数据处理
Pytorch学习笔记(3):图像的预处理(transforms)
Pytorch学习笔记(3):图像的预处理(transforms)
2634 1
Pytorch学习笔记(3):图像的预处理(transforms)
|
Python
python中3种获取cookie解决方案
python中3种获取cookie解决方案
547 0
|
程序员 Shell Linux
01 python - 历史起源
01 python - 历史起源
416 0
|
Kubernetes Devops jenkins
ArgoCD 简明教程
ArgoCD 简明教程
3449 0
ArgoCD 简明教程
|
数据库
FastAPI的数据库操作终于整明白了!
FastAPI的数据库操作终于整明白了!
3824 0
FastAPI的数据库操作终于整明白了!
|
12月前
|
安全 C语言
C语言中的字符、字符串及内存操作函数详细讲解
通过这些函数的正确使用,可以有效管理字符串和内存操作,它们是C语言编程中不可或缺的工具。
507 15
|
Android开发
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
Android Studio入门之图像显示解析及实战(附源码 超详细必看)(包括图像视图、图像按钮、同时展示文本与图像)
653 1
|
机器学习/深度学习 人工智能 编解码
告别潜在空间的黑箱操作,直接在原始像素空间建模!PixelFlow:港大团队开源像素级文生图模型
香港大学与Adobe联合研发的PixelFlow模型,通过流匹配和多尺度生成技术实现像素级图像生成,在256×256分辨率任务中取得1.98的FID分数,支持端到端训练并突破传统模型对预训练VAE的依赖。
846 36
告别潜在空间的黑箱操作,直接在原始像素空间建模!PixelFlow:港大团队开源像素级文生图模型
|
机器学习/深度学习
神经网络与深度学习---验证集(测试集)准确率高于训练集准确率的原因
本文分析了神经网络中验证集(测试集)准确率高于训练集准确率的四个可能原因,包括数据集大小和分布不均、模型正则化过度、批处理后准确率计算时机不同,以及训练集预处理过度导致分布变化。
|
机器学习/深度学习 数据采集 数据可视化
基于YOLOv8的PCB缺陷检测识别项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!
本项目基于YOLOv8实现PCB缺陷检测,提供一站式解决方案。包含完整训练代码、标注数据集、预训练权重及PyQt5图形界面,支持图片、文件夹、视频和摄像头四种检测模式。项目开箱即用,适合科研、工业与毕业设计。核心功能涵盖模型训练、推理部署、结果保存等,检测类型包括缺孔、鼠咬缺口、开路、短路、飞线和杂铜。项目具备高性能检测、友好界面、灵活扩展及多输入源支持等优势,未来可优化模型轻量化、多尺度检测及报告生成等功能。
基于YOLOv8的PCB缺陷检测识别项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!

热门文章

最新文章