Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uniform noise op #554

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 64 additions & 11 deletions src/main/java/net/imagej/ops/filter/FilterNamespace.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import net.imglib2.outofbounds.OutOfBoundsFactory;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.ComplexType;
import net.imglib2.type.numeric.IntegerType;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;
Expand All @@ -64,36 +65,36 @@ public class FilterNamespace extends AbstractNamespace {

// -- addNoise --

@OpMethod(ops = { net.imagej.ops.filter.addNoise.AddNoiseRealType.class,
net.imagej.ops.filter.addNoise.AddNoiseRealTypeCFI.class })
public <I extends RealType<I>, O extends RealType<O>> O addNoise(final O out,
@OpMethod(ops = { net.imagej.ops.filter.addGaussianNoise.AddGaussianNoiseRealType.class,
net.imagej.ops.filter.addGaussianNoise.AddGaussianNoiseRealTypeCFI.class })
public <I extends RealType<I>, O extends RealType<O>> O addGaussianNoise(final O out,
final I in, final double rangeMin, final double rangeMax,
final double rangeStdDev)
{
@SuppressWarnings("unchecked")
final O result = (O) ops().run(Ops.Filter.AddNoise.class, out, in, rangeMin,
final O result = (O) ops().run(Ops.Filter.AddGaussianNoise.class, out, in, rangeMin,
rangeMax, rangeStdDev);
return result;
}

@OpMethod(ops = { net.imagej.ops.filter.addNoise.AddNoiseRealType.class,
net.imagej.ops.filter.addNoise.AddNoiseRealTypeCFI.class })
public <I extends RealType<I>, O extends RealType<O>> O addNoise(final O out,
@OpMethod(ops = { net.imagej.ops.filter.addGaussianNoise.AddGaussianNoiseRealType.class,
net.imagej.ops.filter.addGaussianNoise.AddGaussianNoiseRealTypeCFI.class })
public <I extends RealType<I>, O extends RealType<O>> O addGaussianNoise(final O out,
final I in, final double rangeMin, final double rangeMax,
final double rangeStdDev, final long seed)
{
@SuppressWarnings("unchecked")
final O result = (O) ops().run(Ops.Filter.AddNoise.class, out, in, rangeMin,
final O result = (O) ops().run(Ops.Filter.AddGaussianNoise.class, out, in, rangeMin,
rangeMax, rangeStdDev, seed);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addNoise.AddNoiseRealTypeCFI.class)
public <T extends RealType<T>> T addNoise(final T in, final double rangeMin,
@OpMethod(op = net.imagej.ops.filter.addGaussianNoise.AddGaussianNoiseRealTypeCFI.class)
public <T extends RealType<T>> T addGaussianNoise(final T in, final double rangeMin,
final double rangeMax, final double rangeStdDev)
{
@SuppressWarnings("unchecked")
final T result = (T) ops().run(Ops.Filter.AddNoise.class, in, rangeMin,
final T result = (T) ops().run(Ops.Filter.AddGaussianNoise.class, in, rangeMin,
rangeMax, rangeStdDev);
return result;
}
Expand Down Expand Up @@ -130,6 +131,58 @@ public <I extends RealType<I>, O extends RealType<O>> O addPoissonNoise(
Ops.Filter.AddPoissonNoise.class, out, in);
return result;
}

// -- Uniform Noise --

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final double rangeMin, final double rangeMax)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseRealType.class)
public <I extends RealType<I>> I addUniformNoise(final I out,
final I in, final double rangeMin, final double rangeMax, final Long seed)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax, seed);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
public <I extends IntegerType<I>> I addUniformNoise(final I out,
final I in, final long rangeMin, final long rangeMax)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
public <I extends IntegerType<I>> I addUniformNoise(final I out,
final I in, final long rangeMin, final long rangeMax, final boolean clampOutput)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax, clampOutput);
return result;
}

@OpMethod(op = net.imagej.ops.filter.addUniformNoise.AddUniformNoiseIntegerType.class)
public <I extends IntegerType<I>> I addUniformNoise(final I out,
final I in, final long rangeMin, final long rangeMax, final boolean clampOutput, final Long seed)
{
@SuppressWarnings("unchecked")
final I result = (I) ops().run(Ops.Filter.AddUniformNoise.class, out, in, rangeMin,
rangeMax, clampOutput, seed);
return result;
}

// -- bilateral --

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* #L%
*/

package net.imagej.ops.filter.addNoise;
package net.imagej.ops.filter.addGaussianNoise;

import java.util.Random;

Expand All @@ -42,9 +42,9 @@
* Sets the real component of an output real number to the addition of the real
* component of an input real number with an amount of Gaussian noise.
*/
@Plugin(type = Ops.Filter.AddNoise.class)
public class AddNoiseRealType<I extends RealType<I>, O extends RealType<O>>
extends AbstractUnaryComputerOp<I, O> implements Ops.Filter.AddNoise
@Plugin(type = Ops.Filter.AddGaussianNoise.class)
public class AddGaussianNoiseRealType<I extends RealType<I>, O extends RealType<O>>
extends AbstractUnaryComputerOp<I, O> implements Ops.Filter.AddGaussianNoise
{

@Parameter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* #L%
*/

package net.imagej.ops.filter.addNoise;
package net.imagej.ops.filter.addGaussianNoise;

import java.util.Random;

Expand All @@ -44,12 +44,12 @@
* <p>
* This op is a hybrid computer/function/inplace, since input and output types
* are the same. For input and output of different types, see
* {@link AddNoiseRealType}.
* {@link AddGaussianNoiseRealType}.
* </p>
*/
@Plugin(type = Ops.Filter.AddNoise.class, priority = Priority.HIGH)
public class AddNoiseRealTypeCFI<T extends RealType<T>> extends
AbstractUnaryHybridCFI<T, T> implements Ops.Filter.AddNoise
@Plugin(type = Ops.Filter.AddGaussianNoise.class, priority = Priority.HIGH)
public class AddGaussianNoiseRealTypeCFI<T extends RealType<T>> extends
AbstractUnaryHybridCFI<T, T> implements Ops.Filter.AddGaussianNoise
{

@Parameter
Expand All @@ -71,7 +71,7 @@ public class AddNoiseRealTypeCFI<T extends RealType<T>> extends
@Override
public void compute(final T input, final T output) {
if (rng == null) rng = new Random(seed);
AddNoiseRealType.addNoise(input, output, rangeMin, rangeMax, rangeStdDev,
AddGaussianNoiseRealType.addNoise(input, output, rangeMin, rangeMax, rangeStdDev,
rng);
}

Expand All @@ -80,7 +80,7 @@ public void compute(final T input, final T output) {
@Override
public void mutate(final T arg) {
if (rng == null) rng = new Random(seed);
AddNoiseRealType.addNoise(arg, arg, rangeMin, rangeMax, rangeStdDev, rng);
AddGaussianNoiseRealType.addNoise(arg, arg, rangeMin, rangeMax, rangeStdDev, rng);
}

// -- UnaryOutputFactory methods --
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* #%L
* ImageJ software for multidimensional image processing and analysis.
* %%
* Copyright (C) 2014 - 2018 ImageJ developers.
* %%
* 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 COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDERS OR CONTRIBUTORS 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.
* #L%
*/

package net.imagej.ops.filter.addUniformNoise;

import java.math.BigInteger;

import net.imagej.ops.Ops;
import net.imagej.ops.special.computer.AbstractUnaryComputerOp;
import net.imglib2.type.numeric.IntegerType;

import org.scijava.Priority;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import org.scijava.util.MersenneTwisterFast;

/**
* Adds a pseudorandomly generated value {@code x} to a {@link IntegerType}
* {@code I}, such that {@code x} is (inclusively) bounded by {@code rangeMin}
* and {@code rangeMax} parameters, i.e. {@code rangeMin <= x <= rangeMax}.
*
* @author Gabe Selzer
*/
@Plugin(type = Ops.Filter.AddUniformNoise.class, priority = Priority.HIGH)
public class AddUniformNoiseIntegerType<I extends IntegerType<I>> extends
AbstractUnaryComputerOp<I, I> implements Ops.Filter.AddUniformNoise
{

/**
* The greatest that an input value can be decreased.
*/
@Parameter
private long rangeMin;

/**
* The greatest that an input value can be <b> increased </b>
*/
@Parameter
private long rangeMax;

/**
* If false, the Op will wrap outputs that are outside of the type bounds,
* instead of clamping them
*/
@Parameter(required = false)
private boolean clampOutput = true;

@Parameter(required = false)
private Long seed;
private long range;

private MersenneTwisterFast rng;

@Override
public void initialize() {
// setup rng with seed only if one was provided.
if (rng == null) rng = seed == null ? new MersenneTwisterFast()
: new MersenneTwisterFast(seed);
if (rangeMax < rangeMin) {
long temp = rangeMax;
rangeMax = rangeMin;
rangeMin = temp;
}
// MersenneTwister can only generate numbers that can fit into a long.
range = Math.subtractExact(rangeMax + 1, rangeMin);
}

@Override
public void compute(I input, I output) {
final double newVal = rng.nextLong(range) + rangeMin + input
.getRealDouble();

AddUniformNoiseRealType.setOutput(newVal, clampOutput, output);
}

}