图像噪声是指存在于图像数据中的不必要的或多余的干扰信息。为图像添加噪声,常见有以下几种方法:
(1)高斯噪声
(2)脉冲噪声
(3)泊松噪声
高斯噪声
高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。高斯分布,也称正态分布,又称常态分布,记为N(μ,σ2),其中μ,σ2为分布的参数,分别为高斯分布的期望和方差。当有确定值时,p(x)也就确定了,特别当μ=0,σ^2=1时,X的分布为标准正态分布。
给图像添加告诉高斯噪声的代码如下:
import java.awt.image.BufferedImage;
import java.util.Random;
public class GaussianNoise {
private Random rand = new Random(System.currentTimeMillis());
private int generateNoise(int color) {
double alpha, beta, sigma, value;
alpha = rand.nextDouble();
if (alpha == 0.0)
alpha = 1.0;
double SigmaGaussian = 4.0;
double TauGaussian = 20.0;
double tau;
beta = rand.nextDouble();
sigma = Math.sqrt(-2.0 * Math.log(alpha)) * Math.cos(2.0 * Math.PI * beta);
tau = Math.sqrt(-2.0 * Math.log(alpha)) * Math.sin(2.0 * Math.PI * beta);
value = (double) color + Math.sqrt((double) color) * SigmaGaussian * sigma + TauGaussian * tau;
if (value < 0.0)
return 0;
if (value > 255)
return (int) 255;
return (int) (value + 0.5);
}
public BufferedImage addNoiseImage(BufferedImage image) {
BufferedImage bimg = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
Pixel pixel = new Pixel();
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel.setRGB(image.getRGB(x, y));
pixel.red = generateNoise(pixel.red);
pixel.green = generateNoise(pixel.green);
pixel.blue = generateNoise(pixel.blue);
bimg.setRGB(x, y, pixel.getRGB());
}
}
return bimg;
}
}
可以通过改变SigmaGaussian,TauGaussian的值,改变噪声的分布。
脉冲噪声
脉冲噪声(pulse noise)在通信中出现的离散型噪声的统称。它由时间上无规则出现的突发性干扰组成。脉冲噪声(impulsive noise)是非连续的,由持续时间短和幅度大的不规则脉冲或噪声尖峰组成。
给图像添加告诉脉冲噪声的代码如下:
import java.awt.image.BufferedImage;
import java.util.Random;
public class ImpulseNoise {
private Random rand = new Random(System.currentTimeMillis());
private int generateNoise(int color) {
double alpha, value;
alpha = rand.nextDouble();
if (alpha == 0.0)
alpha = 1.0;
double SigmaImpulse = 0.1;
if (alpha < (SigmaImpulse / 2.0)) {
value = 0;
} else if (alpha >= (1.0 - (SigmaImpulse / 2.0))) {
value = 255;
} else {
value = color;
}
if (value < 0.0)
return 0;
if (value > 255)
return (int) 255;
return (int) (value + 0.5);
}
public BufferedImage addNoiseImage(BufferedImage image) {
BufferedImage bimg = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
Pixel pixel = new Pixel();
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel.setRGB(image.getRGB(x, y));
pixel.red = generateNoise(pixel.red);
pixel.green = generateNoise(pixel.green);
pixel.blue = generateNoise(pixel.blue);
bimg.setRGB(x, y, pixel.getRGB());
}
}
return bimg;
}
}
可以通过调整SigmaImpulse的值改变噪声分布
泊松噪声
泊松噪声,就是噪声分布符合泊松分布模型
泊松分布的概率函数为:
泊松分布的参数λ是单位时间(或单位面积)内随机事件的平均发生次数。 泊松分布适合于描述单位时间内随机事件发生的次数。
泊松分布的期望和方差均为
特征函数为
给图像添加泊松噪声代码如下:
import java.awt.image.BufferedImage;
import java.util.Random;
public class PoissonNoise {
private Random rand = new Random(System.currentTimeMillis());
private int generateNoise(int color) {
double alpha, beta, value;
alpha = rand.nextDouble();
if (alpha == 0.0)
alpha = 1.0;
double SigmaPoisson = 0.05;
int i = 0;
for (i = 0; alpha > Math.exp(-SigmaPoisson * color); i++) {
beta = rand.nextDouble();
alpha = alpha * beta;
}
value = i / SigmaPoisson;
if (value < 0.0)
return 0;
if (value > 255)
return (int) 255;
return (int) (value + 0.5);
}
public BufferedImage addNoiseImage(BufferedImage image) {
BufferedImage bimg = new BufferedImage(image.getWidth(),image.getHeight(),BufferedImage.TYPE_INT_RGB);
Pixel pixel = new Pixel();
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel.setRGB(image.getRGB(x, y));
pixel.red = generateNoise(pixel.red);
pixel.green = generateNoise(pixel.green);
pixel.blue = generateNoise(pixel.blue);
bimg.setRGB(x, y, pixel.getRGB());
}
}
return bimg;
}
}
可以通过改变SigmaPoisson的值,改变造成分布。
辅助类Pixel实现
辅助类Pixel代码实现如下:
import java.awt.image.*;
public class Pixel {
public int red;
public int green;
public int blue;
public int alpha=0xFF;
public double hue;
public double saturation;
public double luminosity;
private int rgb;
public Pixel() {
}
public void setRGB(int rgb) {
red = (rgb & 0x00FF0000) / 0x00010000;
green = (rgb & 0x0000FF00) / 0x00000100;
blue = rgb & 0x000000FF;
alpha = (rgb >> 24)&0x0ff;
this.rgb = rgb;
}
public int getRGB() {
rgb = alpha<<24 | 0x00010000 * red | 0x00000100 * green | blue;
return this.rgb;
}
public static void setRgb(BufferedImage image, int x, int y, int red, int green, int blue) {
int rgb = 0xFF000000 | red * 0x00010000 | green * 0x00000100 | blue;
image.setRGB(x, y, rgb);
}
public static int pixelIntensity(int rgb) {
int red = (rgb&0x00FF0000)/0x00010000;
int green = (rgb&0x0000FF00)/0x00000100;
int blue = rgb&0x000000FF;
return (int) (0.299 * red + 0.587 * green + 0.114 * blue + 0.5);
}
}
测试及效果
使用以下代码进行测试:
public class ImageEffectTest {
public static void main(String[] argv) throws IOException {
BufferedImage img = read(new File("girl.jpg"));
GaussianNoise gauss = new GaussianNoise();
ImpulseNoise impulse = new ImpulseNoise();
PoissonNoise poisson = new PoissonNoise();
BufferedImage img2 = gauss.addNoiseImage(img);
ImageIO.write(img2, "jpeg", new File("noise-girl-gaussian.jpg"));
img2 = impulse.addNoiseImage(img);
ImageIO.write(img2, "jpeg", new File("noise-girl-impulse.jpg"));
img2 = poisson.addNoiseImage(img);
ImageIO.write(img2, "jpeg", new File("noise-girl-poisson.jpg"));
}
}
产生的图像效果如下:(第一张为原图,第二张为高斯噪声效果,第三张为脉冲噪声效果,第四张为泊松噪声效果)