i
Viewing Image Data within ImageJ2 Java Code Brian Northan, True North Intelligent Algorithms LLC
Start ImageJ
// create an instance of imagej
final ImageJ ij = new ImageJ();
// launch it
ij.ui().showUI();
Open Some Image Data
// get bridge as IJ1 ImagePlus (imp)
ImagePlus impBridge = IJ.openImage("../images/bridge.tif");
// get bridge as IJ2 Dataset
// Dataset
Dataset datasetBridge = (Dataset) ij.io().open("../images/bridge.tif");
Display Image using IJ1
// show the IJ1 ImagePlus
impBridge.show();
impBridge.setTitle("ImageJ1 ImagePlus");
Display Image using IJ2 UIService
// show the IJ2 Dataset
ij.ui().show("Bridge IJ2 ij.ui().show", datasetBridge);
Display Image using IJ2 ImageJFunctions
// show using imagej functions
ImageJFunctions.show((RandomAccessibleInterval) datasetBridge
.getImgPlus()).setTitle("Bridge IJ2 ImageJFunctions.show");
Display output of Command
@Parameter(type = ItemIO.OUTPUT)
Img out;
@Override
public void run() {
out = ops.create().img(in);
ops.filter().addPoissonNoise(out, in);
}
// create an instance of imagej
final ImageJ ij = new ImageJ();
// launch it
ij.launch(args);
// get bridge as IJ2 Dataset
Dataset dataBridge = (Dataset) ij.io().open("../images/bridge.tif");
// blur the bridge
RandomAccessibleInterval blurred = ij.op().filter().gauss(
(RandomAccessibleInterval) dataBridge, 3.0);
// convert bridge to IJ1
ImagePlus impBridge = ImageJFunctions.wrap(
(RandomAccessibleInterval) dataBridge, "bridge");
// convert blurred to IJ1
ImagePlus impBlurred = ImageJFunctions.wrap(blurred, "blurred");
// show IJ1
impBridge.show();
impBlurred.show();
// get the fire color map using the Utility
// (note the code in the Utility was cut and pasted from LutLoader
// I couldn't figure out how to cleanly grab the fire LUT from it....
LUT lut = IJ2CourseImageUtility.fire();
// set the LUT on the bridge and blurred
impBridge.setLut(lut);
impBridge.updateAndRepaintWindow();
impBlurred.setLut(lut);
impBlurred.updateAndRepaintWindow();
Use IJ1 API to draw line profiles
// now draw an ROI (using the IJ1 interface) on each image and plot it
impBridge.setRoi(new Line(100, 100, 200, 200));
impBlurred.setRoi(new Line(100, 100, 200, 200));
ProfilePlot plotter1 = new ProfilePlot(impBridge);
ProfilePlot plotter2 = new ProfilePlot(impBlurred);
plotter1.createWindow();
plotter2.createWindow();
Interval interval = Intervals.createMinMax(100, 100, 20, 400, 400, 40);
// crop interval
RandomAccessibleInterval raiVolume = (RandomAccessibleInterval) ij
.op().transform().crop(image, interval);
// alternatively you can use Views directly
RandomAccessibleInterval rai2 = (RandomAccessibleInterval) Views
.interval(image, interval);
// display the image
ij.ui().show("RAI volume", raiVolume);
Draw Sphere Command
@Plugin(type = Command.class,
menuPath = "Plugins>Learnathon>Draw A Sphere (IJ2)")
public class DrawSphereInCenter & NativeType>
implements Command
{
@Parameter
OpService ops;
@Parameter
Img img;
@Override
public void run() {
final Point center = new Point(img.numDimensions());
for (int d = 0; d < img.numDimensions(); d++)
center.setPosition(img.dimension(d) / 2, d);
long radius = Math.min(img.dimension(0), Math.min(img.dimension(1), img
.dimension(2)));
T intensity = ops.stats().max(Views.iterable(img));
HyperSphere hyperSphere = new HyperSphere<>(img, center, radius);
for (final T value : hyperSphere) {
value.setReal(intensity.getRealFloat());
}
}
}
Interval interval = Intervals.createMinMax(-100, 100, 200, 700);
// Try cropping with an interval that goes out of bounds...
RandomAccessibleInterval rai = (RandomAccessibleInterval) Views
.interval(image, interval);
// try again but extend image... (comment out above code, comment in this code)
//RandomAccessibleInterval rai = (RandomAccessibleInterval) Views
// .interval(Views.extendZero(image), interval);
// display the image
ij.ui().show("RAI", rai);
Interval interval = Intervals.createMinMax(-100, 100, 200, 700);
// Try cropping with an interval that goes out of bounds...
//RandomAccessibleInterval rai = (RandomAccessibleInterval) Views
// .interval(image, interval);
// try again but extend image... (comment out above code, comment in this code)
RandomAccessibleInterval rai = (RandomAccessibleInterval) Views
.interval(Views.extendZero(image), interval);
// display the image
ij.ui().show("RAI", rai);
Show axis info using dimension index
for (int d = 0; d < image.numDimensions(); d++) {
ij.log().info(image.axis(d).type());
ij.log().info(image.dimension(d));
ij.log().info("");
}
int xIndex = image.dimensionIndex(Axes.X);
int yIndex = image.dimensionIndex(Axes.Y);
int zIndex = image.dimensionIndex(Axes.Z);
int cIndex = image.dimensionIndex(Axes.CHANNEL);
int tIndex = image.dimensionIndex(Axes.TIME);
long xLen = image.dimension(image.dimensionIndex(Axes.X));
long yLen = image.dimension(image.dimensionIndex(Axes.Y));
long zLen = image.dimension(image.dimensionIndex(Axes.Z));
long cLen = image.dimension(image.dimensionIndex(Axes.CHANNEL));
long tLen = image.dimension(image.dimensionIndex(Axes.TIME));
// we can use Views to get a hyperslice using a dimensions index and position
RandomAccessibleInterval raiViews = (RandomAccessibleInterval) Views
.hyperSlice(image, cIndex, 0);
// display the image... note that something isn't quite right
ij.ui().show("RAI volume", raiViews);
// the cropped section is an RAI. To get it to display correctly we need to
// use the dtaasetservice to convert it to an ImgPlus with ocrrect axis
DatasetService datasetService = ij.dataset();
AxisType[] axisTypes = new AxisType[] { Axes.X, Axes.Y, Axes.Z,
Axes.TIME };
ImgPlus imgPlusVolume = new ImgPlus(datasetService.create(raiViews), "image",
axisTypes);
// now the viewer should display the image with correct axis
ij.ui().show("ImgPlus volume", imgPlusVolume);
Permute (Reslice) using Views to see xz slices
IntervalView dataXZY = Views.permute((RandomAccessibleInterval) data,
1, 3);
ij.ui().show("Data XZY", dataXZY);
long[] projectedDimensions = new long[imgPlus.numDimensions() - 1];
int projectedDimensionIndex = imgPlus.dimensionIndex(ax);
int i = 0;
for (int d = 0; d < imgPlus.numDimensions(); d++) {
if (d != projectedDimensionIndex) {
projectedDimensions[i] = imgPlus.dimension(d);
i++;
}
}
Img projection = ops.create().img(new FinalDimensions(
projectedDimensions), imgPlus.firstElement());
UnaryComputerOp, T> projector = Computers.unary(ops, projectType,
projection.firstElement(), imgPlus);
return ops.transform().project(projection, imgPlus,
(UnaryComputerOp) projector, projectedDimensionIndex);
// get the RAI to draw the Gaussian in
RandomAccessibleInterval rai = Views.interval(img,
new FinalInterval(start, end));
Cursor c1 = Views.iterable(Views.zeroMin(rai)).cursor();
Cursor c2 = Views.iterable(guassian).cursor();
while (c1.hasNext()) {
c1.fwd();
c2.fwd();
c1.get().add(c2.get());
}
Perform FFT using ops, Display real and complex parts
RandomAccessibleInterval fft=ij.op().filter().fft(impBridge);
// default is power spectrum
ImageJFunctions.show(fft).setTitle("fft power spectrum");
// real values
ImageJFunctions.show(fft, new ComplexRealFloatConverter()).setTitle("fft real values");
// imaginary values
ImageJFunctions.show(fft, new ComplexImaginaryFloatConverter()).setTitle("fft imaginary values");