-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGaussianNoise.java
66 lines (52 loc) · 1.86 KB
/
GaussianNoise.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sample.use.my.sprawozdanie1;
import kimage.image.Image;
import kimage.plugin.Plugin;
import kimage.utils.gui.ImageFrame;
import kimage.plugins.color.Grayscale;
/**
*
* @author Weronika
*/
public class GaussianNoise extends Plugin {
private double average = 1;
private double deviation = 0.65;
private double probability = 0.9;
private int generateGauss() {
double U1, U2, Z, X;
// z definicji liczby te mają być różne od 0
do {
U1 = Math.random();
U2 = Math.random();
} while ((U1 == 0) || (U2 == 0));
// wykorzystanie odpowiednich wzorów
Z = Math.sqrt(-2 * Math.log(U1)) * Math.cos(2 * Math.PI * U2);
X = average + deviation * Z;
return (int) X;
}
public void process(Image imgIn, Image imgOut) {
Plugin pl = new Grayscale();
pl.process(imgIn, imgOut);
for (int i = 0; i < imgIn.getWidth(); ++i) {
for (int j = 0; j < imgIn.getHeight(); ++j) {
// dodajemy szum do każdego piksela z
// zadanym prawdopodobieństwem
double rand = Math.random();
if (rand < probability) {
int tmp = imgIn.getRed(i, j) + generateGauss();
if (tmp < 0) {
imgOut.setRGB(i, j, 0, 0, 0);
} else if (tmp > 255) {
imgOut.setRGB(i, j, 255, 255, 255);
} else {
imgOut.setRGB(i, j, (int) tmp, (int) tmp, (int) tmp);
}
}
}
}
}
}