Skip to content

Commit e9cbbfc

Browse files
committed
Uniform Noise Adder
1 parent 61ca449 commit e9cbbfc

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

scijava-ops-image/src/main/java/org/scijava/ops/image/filter/addNoise/NoiseAdders.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@
3131

3232
import java.util.Random;
3333

34+
import net.imglib2.Localizable;
3435
import net.imglib2.RandomAccessibleInterval;
3536
import net.imglib2.loops.LoopBuilder;
3637
import net.imglib2.type.numeric.RealType;
3738

39+
import org.scijava.common3.MersenneTwisterFast;
3840
import org.scijava.function.Computers;
41+
import org.scijava.ops.spi.Nullable;
3942

4043
/**
4144
* Contains Ops designed to add noise to populated images.
@@ -192,4 +195,48 @@ public static <I extends RealType<I>, O extends RealType<O>> void addPoissonNois
192195
return;
193196
}
194197

198+
// -- UNIFORM NOISE -- //
199+
200+
/**
201+
* Sets the real component of an output real number to the addition of the real
202+
* component of an input real number with an amount of uniform noise.
203+
*
204+
* @param input the input {@link RandomAccessibleInterval}
205+
* @param rangeMin the "most negative" value that can be added to each element
206+
* @param rangeMax the "most positive" value that can be added to each element
207+
* @param seed the seed to the random number generator
208+
* @param output the output {@link RandomAccessibleInterval}
209+
* @implNote op names='filter.addUniformNoise', type=Computer
210+
*/
211+
public static <I extends RealType<I>> void addUniformNoise( //
212+
RandomAccessibleInterval<I> input, //
213+
I rangeMin, //
214+
I rangeMax, //
215+
@Nullable Long seed, //
216+
RandomAccessibleInterval<I> output //
217+
) {
218+
// Set seed to default if necessary
219+
if (seed == null) {
220+
seed = 0xabcdef1234567890L;
221+
}
222+
// Construct the Random Number Generator
223+
MersenneTwisterFast rng = new MersenneTwisterFast(seed);
224+
// Find the range
225+
I range = rangeMax.createVariable();
226+
range.set(rangeMax);
227+
range.sub(rangeMin);
228+
229+
// Loop over the images
230+
LoopBuilder.setImages(input, output).forEachPixel( (i, o) -> {
231+
// Random value = next double * range
232+
o.set(range);
233+
o.mul(rng.nextDouble(true, true));
234+
// Add the range minimum
235+
o.add(rangeMin);
236+
// Add the original value
237+
o.add(i);
238+
});
239+
240+
}
241+
195242
}

scijava-ops-image/src/test/java/org/scijava/ops/image/OpRegressionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class OpRegressionTest {
4242

4343
@Test
4444
public void opDiscoveryRegressionIT() {
45-
long expected = 1868;
45+
long expected = 1870;
4646
long actual = ops.infos().size();
4747
assertEquals(expected, actual);
4848
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.scijava.ops.image.filter.addNoise;
2+
3+
import net.imglib2.img.array.ArrayImgs;
4+
import net.imglib2.type.numeric.integer.ByteType;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
import org.scijava.ops.image.AbstractOpTest;
8+
9+
import java.util.Arrays;
10+
import java.util.List;
11+
12+
public class NoiseAddersTest extends AbstractOpTest {
13+
14+
@Test
15+
public void testAddUniformNoiseRegression() {
16+
var input = ArrayImgs.bytes(2, 2);
17+
ops.unary("image.fill").input(new ByteType((byte) 1)).output(input).compute();
18+
var output = ArrayImgs.bytes(2, 2);
19+
var rangeMin = new ByteType((byte) -1);
20+
var rangeMax = new ByteType((byte) 1);
21+
ops.ternary("filter.addUniformNoise").input(input, rangeMin, rangeMax).output(output).compute();
22+
var cursor = output.cursor();
23+
List<Byte> expected = Arrays.asList((byte) 0, (byte) 2, (byte) 1, (byte) 1);
24+
for(var e : expected) {
25+
Assertions.assertEquals(e, cursor.next().get());
26+
}
27+
Assertions.assertFalse(cursor.hasNext());
28+
}
29+
}

0 commit comments

Comments
 (0)