Total members 11895 |It is currently Tue Dec 03, 2024 6:09 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Image Noise Filter in Java
java code
package imageviewer.NoiseFilters;


import java.awt.image.*;


public class NoiseFilter extends Filter {


public final static int IMPULSE = 0;

public final static int GAUSSIAN = 1;

protected int noiseType = IMPULSE;

protected double stdDev = 10.0;

protected double impulseRatio = 0.05;


public NoiseFilter() {
}


public NoiseFilter(int noiseType) {
setNoiseType(noiseType);
}


public NoiseFilter(int noiseType, double parameter) {
setNoiseType(noiseType);

if (noiseType == IMPULSE) setImpulseRatio(parameter);
if (noiseType == GAUSSIAN) setGaussianStdDev(parameter);
}


public void setNoiseType(int noiseType) {
this.noiseType = noiseType;
}


public int getNoiseType() {
return noiseType;
}


public void setGaussianStdDev(double stdDev) {
this.stdDev = stdDev;
}


public double getGaussianStdDev() {
return stdDev;
}

public void setImpulseRatio(double impulseRatio) {
this.impulseRatio = impulseRatio;
}

public double getImpulseRatio() {
return impulseRatio;
}

public java.awt.image.BufferedImage filter(BufferedImage image, BufferedImage output) {
output = verifyOutput(output, image);

switch (noiseType) {
default:
case IMPULSE: return impulseNoise(image, output);
case GAUSSIAN: return gaussianNoise(image, output);
}
}


protected BufferedImage impulseNoise(BufferedImage image, BufferedImage output) {
output.setData(image.getData());

Raster source = image.getRaster();
WritableRaster out = output.getRaster();

double rand;
double halfImpulseRatio = impulseRatio / 2.0;
int bands = out.getNumBands();
int width = image.getWidth(); // width of the image
int height = image.getHeight(); // height of the image
java.util.Random randGen = new java.util.Random();

for (int j=0; j<height; j++) {
for (int i=0; i<width; i++) {
rand = randGen.nextDouble();
if (rand < halfImpulseRatio) {
for (int b=0; b<bands; b++) out.setSample(i, j, b, 0);
} else if (rand < impulseRatio) {
for (int b=0; b<bands; b++) out.setSample(i, j, b, 255);
}
}
}

return output;
}


protected BufferedImage gaussianNoise(BufferedImage image, BufferedImage output) {
Raster source = image.getRaster();
WritableRaster out = output.getRaster();

int currVal; // the current value
double newVal; // the new "noisy" value
double gaussian; // gaussian number
int bands = out.getNumBands(); // number of bands
int width = image.getWidth(); // width of the image
int height = image.getHeight(); // height of the image
java.util.Random randGen = new java.util.Random();

for (int j=0; j<height; j++) {
for (int i=0; i<width; i++) {
gaussian = randGen.nextGaussian();

for (int b=0; b<bands; b++) {
newVal = stdDev * gaussian;
currVal = source.getSample(i, j, b);
newVal = newVal + currVal;
if (newVal < 0) newVal = 0.0;
if (newVal > 255) newVal = 255.0;

out.setSample(i, j, b, (int)(newVal));
}
}
}

return output;
}
}

Original Project:
finished-projects/image-viewer-image-processing-filters-noise-enhancements-t639.html



_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Image Noise Filter in Java
 Image-Viewer-Image Processing-Filters-Noise-enhancements     -  
 Image Custom Filter In Java     -  
 make Noise on Image     -  
 Median filter to image     -  
 apply mean filter to image     -  
 Applying canny filter to image     -  
 apply robert filter to image     -  
 apply sobel filter to image     -  
 applying wiener filter to image     -  
 apply prewitt filter to image     -  



Topic Tags

Java Image






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com