import ij.IJ; import ij.ImagePlus; import ij.gui.GenericDialog; import ij.plugin.filter.PlugInFilter; import ij.process.ImageProcessor; /** * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: *
* 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. *
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
* This plugin adds Poisson distributed noise to each pixel of an image. *
* Poisson noise or (particularly in electronics) as shot noise is a type of
* electronic noise that occurs when the finite number of particles that carry
* energy, such as electrons in an electronic circuit or photons in an optical
* device, is small enough to give rise to detectable statistical fluctuations
* in a measurement. It is important in electronics, telecommunications, and
* fundamental physics.
*
* @author Ignazio Gallo(ignazio.gallo@gmail.com)
* @since 18/nov/2008
*/
public class Poisson_Noise implements PlugInFilter {
private final int SIZE = 1000;
private double percentage = 2;
ImagePlus imp;
public int setup(String arg, ImagePlus imp) {
if (IJ.versionLessThan("1.17j"))
return DONE;
// Registration may help static fields from being reset but may
// also prevent 'Compile and Run' from reloading this class.
IJ.register(Poisson_Noise.class);
this.imp = imp;
return DOES_8G;
}
public void run(ImageProcessor ip) {
if (!showDialog())
return;
int poissonVal;
double[] probability = new double[SIZE];
double[] prob2 = new double[SIZE];
int width = ip.getWidth(); // width of the image
int height = ip.getHeight(); // height of the image
percentage = percentage*7.45;
// Initializes and sets probability array (Poisson)
probability[0] = Math.exp(-percentage);
prob2[0] = 0;
for (int i=1; i