-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy pathHandleInChunks.java
More file actions
60 lines (47 loc) · 1.27 KB
/
HandleInChunks.java
File metadata and controls
60 lines (47 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
* To the extent possible under law, the ImageJ developers have waived
* all copyright and related or neighboring rights to this tutorial code.
*
* See the Unlicense for details:
* https://unlicense.org/
*/
package howto.images.processing.loopbuilder;
import net.imagej.ImageJ;
import net.imglib2.img.Img;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.numeric.real.DoubleType;
import java.util.List;
/**
* How to use the LoopBuilder to process an image in chunks
*
* @author Matthias Arzt
* @author Deborah Schmidt
*/
public class HandleInChunks {
public static void run() {
ImageJ ij = new ImageJ();
// create image
Img<DoubleType> image = ij.op().create().img(new long[]{10, 10});
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
image.getAt(x, y).set(x+y);
}
}
List<DoubleType> listOfSums = LoopBuilder.setImages( image ).multiThreaded().forEachChunk(
chunk -> {
DoubleType sum = new DoubleType();
chunk.forEachPixel( pixel -> {
sum.add(new DoubleType(pixel.getRealDouble()));
});
return sum;
}
);
DoubleType totalSum = new DoubleType();
listOfSums.forEach(totalSum::add);
System.out.println(totalSum);
ij.dispose();
}
public static void main(String...args) {
run();
}
}