图像处理------添加高斯与泊松噪声

简介: <p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;"><span style="font-size: 18px;">数学基础:</span></p><p style="color: rgb(51, 51, 51); font-family: Arial; font-

数学基础:

什么是泊松噪声,就是噪声分布符合泊松分布模型。泊松分布(Poisson Di)的公

式如下:


关于泊松分布的详细解释看这里:http://zh.wikipedia.org/wiki/泊松分佈

关于高斯分布与高斯噪声看这里:

http://blog.csdn.net/jia20003/article/details/7181463

 二:程序实现

以前在图像加噪博文中现实的加高斯噪声,比较复杂。是自己完全实现了高斯随

机数的产生,这里主要是利用JAVA的随机数API提供的nextGaussion()方法来得

到高斯随机数。泊松噪声为了简化计算,Google到一位神人完成的C++代码于是

我翻译成Java的。

三:程序效果


滤镜源代码:

[java]  view plain copy
  1. package com.gloomyfish.filter.study;  
  2.   
  3. import java.awt.image.BufferedImage;  
  4. import java.util.Random;  
  5.   
  6. public class NoiseAdditionFilter extends AbstractBufferedImageOp {  
  7.     public final static double MEAN_FACTOR = 2.0;  
  8.     public final static int POISSON_NOISE_TYPE = 2;  
  9.     public final static int GAUSSION_NOISE_TYPE = 1;  
  10.     private double _mNoiseFactor = 25;  
  11.     private int _mNoiseType = POISSON_NOISE_TYPE;  
  12.       
  13.     public NoiseAdditionFilter() {  
  14.         System.out.println("Adding Poisson/Gaussion Noise");  
  15.     }  
  16.       
  17.     public void setNoise(double power) {  
  18.         this._mNoiseFactor = power;  
  19.     }  
  20.       
  21.     public void setNoiseType(int type) {  
  22.         this._mNoiseType = type;  
  23.     }  
  24.       
  25.     @Override  
  26.     public BufferedImage filter(BufferedImage src, BufferedImage dest) {  
  27.         int width = src.getWidth();  
  28.         int height = src.getHeight();  
  29.         Random random = new Random();  
  30.         if ( dest == null )  
  31.             dest = createCompatibleDestImage( src, null );  
  32.   
  33.         int[] inPixels = new int[width*height];  
  34.         int[] outPixels = new int[width*height];  
  35.         getRGB( src, 00, width, height, inPixels );  
  36.         int index = 0;  
  37.         for(int row=0; row<height; row++) {  
  38.             int ta = 0, tr = 0, tg = 0, tb = 0;  
  39.             for(int col=0; col<width; col++) {  
  40.                 index = row * width + col;  
  41.                 ta = (inPixels[index] >> 24) & 0xff;  
  42.                 tr = (inPixels[index] >> 16) & 0xff;  
  43.                 tg = (inPixels[index] >> 8) & 0xff;  
  44.                 tb = inPixels[index] & 0xff;  
  45.                 if(_mNoiseType == POISSON_NOISE_TYPE) {  
  46.                     tr = clamp(addPNoise(tr, random));  
  47.                     tg = clamp(addPNoise(tg, random));  
  48.                     tb = clamp(addPNoise(tb, random));  
  49.                 } else if(_mNoiseType == GAUSSION_NOISE_TYPE) {  
  50.                     tr = clamp(addGNoise(tr, random));  
  51.                     tg = clamp(addGNoise(tg, random));  
  52.                     tb = clamp(addGNoise(tb, random));  
  53.                 }  
  54.                 outPixels[index] = (ta << 24) | (tr << 16) | (tg << 8) | tb;  
  55.             }  
  56.         }  
  57.   
  58.         setRGB( dest, 00, width, height, outPixels );  
  59.         return dest;  
  60.     }  
  61.       
  62.     private int addGNoise(int tr, Random random) {  
  63.         int v, ran;  
  64.         boolean inRange = false;  
  65.         do {  
  66.             ran = (int)Math.round(random.nextGaussian()*_mNoiseFactor);  
  67.             v = tr + ran;  
  68.             // check whether it is valid single channel value  
  69.             inRange = (v>=0 && v<=255);   
  70.             if (inRange) tr = v;  
  71.         } while (!inRange);  
  72.         return tr;   
  73.     }  
  74.   
  75.     public static int clamp(int p) {  
  76.         return p > 255 ? 255 : (p < 0 ? 0 : p);  
  77.     }  
  78.       
  79.     private int addPNoise(int pixel, Random random) {  
  80.         // init:  
  81.         double L = Math.exp(-_mNoiseFactor * MEAN_FACTOR);  
  82.         int k = 0;  
  83.         double p = 1;  
  84.         do {  
  85.             k++;  
  86.             // Generate uniform random number u in [0,1] and let p ← p × u.  
  87.             p *= random.nextDouble();  
  88.         } while (p >= L);  
  89.         double retValue = Math.max((pixel + (k - 1) / MEAN_FACTOR - _mNoiseFactor), 0);  
  90.         return (int)retValue;  
  91.     }  
  92.   
  93. }  
相关文章
|
机器学习/深度学习 算法 数据挖掘
使用NetworkX绘制深度神经网络结构图(Python)
使用NetworkX绘制深度神经网络结构图(Python)
使用NetworkX绘制深度神经网络结构图(Python)
|
Java API 计算机视觉
图像处理之添加高斯与泊松噪声
图像处理之添加高斯与泊松噪声
334 1
|
PyTorch 算法框架/工具
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
2723 0
Pytorch中Trying to backward through the graph和one of the variables needed for gradient错误解决方案
|
11月前
|
传感器 机器学习/深度学习 人工智能
智能电网巡检与传感器数据AI自动分析
智能电网设备巡检与传感器数据分析利用AI技术实现自动化分析和预警。通过信息抽取、OCR技术和机器学习,系统可高效处理巡检报告和实时数据,生成精准报告并提供故障预判和早期识别。AI系统24小时监控设备状态,实时发出异常警报,确保设备正常运行,提升运维效率和可靠性。
582 6
|
编解码 算法 Python
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
ImportError: cannot import name ‘_update_worker_pids’ from ‘torch._C’
254 0
|
机器学习/深度学习 并行计算 PyTorch
从零开始下载torch+cu(无痛版)
这篇文章提供了一个详细的无痛版教程,指导如何从零开始下载并配置支持CUDA的PyTorch GPU版本,包括查看Cuda版本、在官网检索下载包名、下载指定的torch、torchvision、torchaudio库,并在深度学习环境中安装和测试是否成功。
从零开始下载torch+cu(无痛版)
|
数据安全/隐私保护
网络文件夹目前是以其他用户名和密码进行映射的——映射盘更换登录用户名问题
网络文件夹目前是以其他用户名和密码进行映射的——映射盘更换登录用户名问题
5273 0
网络文件夹目前是以其他用户名和密码进行映射的——映射盘更换登录用户名问题
|
机器学习/深度学习 人工智能 计算机视觉
【CVPR小目标检测】- ISNet红外小目标检测
【CVPR小目标检测】- ISNet红外小目标检测
982 1
|
算法 数据挖掘 BI
❤️ Python 利用NetworkX绘制精美网络图 ❤️
NetworkX 是一个用 Python 语言开发的图论与复杂网络建模工具,内置了常用的图与复杂网络分析算法,可以方便的进行复杂网络数据分析、仿真建模等工作。networkx支持创建简单无向图、有向图和多重图;内置许多标准的图论算法,节点可为任意数据;支持任意的边值维度,功能丰富。主要用于创造、操作复杂网络,以及学习复杂网络的结构、动力学及其功能。用于分析网络结构,建立网络模型,设计新的网络算法,绘制网络等等。
2956 0
❤️ Python 利用NetworkX绘制精美网络图 ❤️
|
Python
conda升级python版本
conda升级python版本
1971 0