[Image] Génération de bruit gaussien/uniforme sur une image
Pour du bruit gaussien :
Code:
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
| /**
* @author millie
*
*/
public class GaussianNoiseGenerator extends ImageOperator {
private Random gauss = new Random();
private Random sign = new Random();
private double sigma;
private double mean;
private double getGaussian() {
return gauss.nextGaussian() * sigma + mean;
}
public GaussianNoiseGenerator(double sigma, double mean) {
this.sigma = sigma;
this.mean = mean;
}
/* (non-Javadoc)
* @see millie.operator.commons.point.PointOperator#computePoint(double)
*/
@Override
public double computePoint(double point) {
int s = sign.nextInt(2);
if(s==0)
s--;
return point + getGaussian() * s;
}
}
public void compute(Image out, Image in, PointOperator op) {
if (out.getNumComponents() != in.getNumComponents())
throw new IllegalArgumentException(
"PointOperator : num components problem");
out.resize(in.getWidth(), in.getHeight());
for (int canal = 0; canal < in.getNumComponents(); canal++)
for (int y = 0; y < in.getHeight(); y++)
for (int x = 0; x < in.getWidth(); x++)
out.setPixel(x, y, canal, op.computePoint(in.getPixel(x, y,
canal)));
} |
Que l'on utilise via :
Code:
1 2 3
|
GaussianNoiseGenerator op = new GaussianNoiseGenerator(10, 0);
op.compute(out, in); |
Pour du bruit Uniforme, compute est identique, mais le reste est :
Code:
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
|
/**
* @author millie
*
*/
public class UniformNoiseGenerator extends PointOperator {
private Random randomizer = new Random();
private int distance;
private int distance2p1;
public UniformNoiseGenerator(int distance) {
this.distance = distance;
distance2p1 = distance*2 +1 ;
}
/* (non-Javadoc)
* @see millie.operator.commons.point.PointOperator#computePoint(double)
*/
@Override
public double computePoint(double point) {
int value = randomizer.nextInt(distance2p1 ) - distance;
return value + point;
}
} |
PRomu@ld : sources