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

Howtos LoopBuilder #85

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion howtos/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<parent>
<groupId>org.scijava</groupId>
<artifactId>pom-scijava</artifactId>
<version>26.0.0</version>
<version>28.0.0</version>
<relativePath />
</parent>

Expand Down Expand Up @@ -71,6 +71,7 @@
<license.licenseName>unlicense</license.licenseName>
<license.copyrightOwners>N/A</license.copyrightOwners>
<license.projectName>ImageJ software for multidimensional image processing and analysis.</license.projectName>
<imglib2.version>5.9.0</imglib2.version>
</properties>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nice illustration how to use forEachChunk returning a List. How about adding an example with synchronized as discussed here, to complete the picture for multi-threading options?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, I didn't see this post, thanks for pointing me to it!

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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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 net.imglib2.util.Intervals;

/**
* How to use the LoopBuilder while accessing the position of a pixel
*
* @author Matthias Arzt
* @author Deborah Schmidt
*/
public class LoopWithPosition {

public static void run() {

ImageJ ij = new ImageJ();

Img<DoubleType> image = ij.op().create().img(new long[]{50, 60});

LoopBuilder.setImages( Intervals.positions( image ), image ).forEachPixel(
( position, pixel ) -> {
int x = position.getIntPosition( 0 );
int y = position.getIntPosition( 1 );
pixel.set( x * x + y * y );
}
);

ij.ui().show(image);

}

public static void main(String...args) {
run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;

import java.io.IOException;

/**
* How to use the LoopBuilder running on multiple threads
*
* @author Matthias Arzt
* @author Deborah Schmidt
*/
public class Multithreading {

public static <T extends RealType<T>> void run() throws IOException {

ImageJ ij = new ImageJ();

// load example image
RandomAccessibleInterval<T> source = (Img<T>) ij.io().open(Object.class.getResource("/blobs.png").getPath());

// create result image
Img<DoubleType> target = ij.op().create().img(source, new DoubleType());

LoopBuilder.setImages( source, target ).multiThreaded().forEachPixel( ( s, t ) -> t.set( s.getRealDouble() ) );

ij.ui().show(target);

}

public static void main(String...args) throws IOException {
run();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.RandomAccessibleInterval;
import net.imglib2.img.Img;
import net.imglib2.loops.LoopBuilder;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.DoubleType;

import java.io.IOException;

/**
* How to use the LoopBuilder for processing multiple images together
*
* @author Matthias Arzt
* @author Deborah Schmidt
*/
public class ProcessMultipleImages {

public static <T extends RealType<T> & NativeType<T>> void run() throws IOException {

ImageJ ij = new ImageJ();

// load example image to imageA
Img<T> imageA = (Img<T>) ij.io().open(Object.class.getResource("/blobs.png").getPath());
// set imageB to mirrored view of ImageA
RandomAccessibleInterval<T> imageB = ij.op().transform().invertAxisView(imageA, 0);
// create result image
Img<DoubleType> sum = ij.op().create().img(imageA, new DoubleType());

LoopBuilder.setImages(imageA, imageB, sum).forEachPixel(
(a, b, s) -> {
s.setReal(a.getRealDouble() + b.getRealDouble());
}
);

ij.ui().show(sum);

}

public static void main(String...args) throws IOException {
run();
}

}