Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.numeric.RealType;
import org.scijava.common3.MersenneTwisterFast;
import org.scijava.ops.spi.Nullable;

/**
Expand Down Expand Up @@ -164,4 +165,48 @@ public static <I extends RealType<I>, O extends RealType<O>> void addPoissonNois
output.setReal(k - 1);
}

// -- UNIFORM NOISE -- //

/**
* 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 uniform noise.
*
* @param input the input {@link RandomAccessibleInterval}
* @param rangeMin the "most negative" value that can be added to each element
* @param rangeMax the "most positive" value that can be added to each element
* @param seed the seed to the random number generator
* @param output the output {@link RandomAccessibleInterval}
* @implNote op names='filter.addUniformNoise', type=Computer
*/
public static <I extends RealType<I>> void addUniformNoise( //
RandomAccessibleInterval<I> input, //
I rangeMin, //
I rangeMax, //
@Nullable Long seed, //
RandomAccessibleInterval<I> output //
) {
// Set seed to default if necessary
if (seed == null) {
seed = 0xabcdef1234567890L;
}
// Construct the Random Number Generator
MersenneTwisterFast rng = new MersenneTwisterFast(seed);
// Find the range
I range = rangeMax.createVariable();
range.set(rangeMax);
range.sub(rangeMin);

// Loop over the images
LoopBuilder.setImages(input, output).forEachPixel( (i, o) -> {
// Random value = next double * range
o.set(range);
o.mul(rng.nextDouble(true, true));
// Add the range minimum
o.add(rangeMin);
// Add the original value
o.add(i);
});

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class OpRegressionTest {

@Test
public void opDiscoveryRegressionIT() {
long expected = 1882;
long expected = 1884;
long actual = ops.infos().size();
assertEquals(expected, actual);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.scijava.ops.image.filter.addNoise;

import net.imglib2.img.array.ArrayImgs;
import net.imglib2.type.numeric.integer.ByteType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.scijava.ops.image.AbstractOpTest;

import java.util.Arrays;
import java.util.List;

public class NoiseAddersTest extends AbstractOpTest {

@Test
public void testAddUniformNoiseRegression() {
var input = ArrayImgs.bytes(2, 2);
ops.unary("image.fill").input(new ByteType((byte) 1)).output(input).compute();
var output = ArrayImgs.bytes(2, 2);
var rangeMin = new ByteType((byte) -1);
var rangeMax = new ByteType((byte) 1);
ops.ternary("filter.addUniformNoise").input(input, rangeMin, rangeMax).output(output).compute();
var cursor = output.cursor();
List<Byte> expected = Arrays.asList((byte) 0, (byte) 2, (byte) 1, (byte) 1);
for(var e : expected) {
Assertions.assertEquals(e, cursor.next().get());
}
Assertions.assertFalse(cursor.hasNext());
}
}