Commit 27629633 authored by lombardo's avatar lombardo

Add 3 ops to ease scripting: one to create a z map, one to create two z map...

Add 3 ops to ease scripting:  one to create a z map, one to create two z map with no crossing constraints and one to reslice a volume along a z map.
parent d09d69a8
package de.mpicbg.scf.mincostsurface;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ops.Op;
import net.imagej.ops.AbstractOp;
import net.imglib2.Cursor;
import net.imglib2.img.Img;
import net.imglib2.img.display.imagej.ImageJFunctions;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.FloatType;
import org.scijava.ItemIO;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import java.io.File;
@Plugin(type = Op.class, menuPath = "Plugins>MinCost2ZSurface", name="MinCost2ZSurface", headless = true, label="MinCost2ZSurface")
public class MinCost2ZSurface_Ops < T extends RealType<T> & NativeType< T > > extends AbstractOp {
//
// should implement Map if I want to benefit the ops matching
//
@Parameter ( label = "cost image", persist = false )
private Img input;
// parameter related to the initial surface detection
int channel = 1;
@Parameter ( label = "rescale xy" ) // downsampling factor of the input image for the direction x and y
private float downsample_factor_xy;
@Parameter ( label = "rescale z" ) // downsampling factor of the input image for the direction z
private float downsample_factor_z;
@Parameter ( label = "Max_delta_z between adjacent voxel" ) // constraint on the surface altitude change from one pixel to another
private int max_dz;
// parameter for the use case with 2 surfaces detection
@Parameter( label = "Max_distance between surfaces (in pixel)" )
private int max_dist;
@Parameter( label = "Min_distance between surfaces (in pixel)" )
private int min_dist;
// output
@Parameter (type = ItemIO.OUTPUT)
private Img<FloatType> upsampled_depthMap1;
@Parameter (type = ItemIO.OUTPUT)
private Img<FloatType> upsampled_depthMap2;
@Override
public void run() {
process2( (Img<T>) input );
}
public void process2( Img<T> input ){
int nDim = input.numDimensions();
if ( nDim != 3 ) {
System.err.println("The data should be of dimensions 3 (found " + nDim + " dimensions)");
return;
}
long end,start;
long[] dims_orig = new long[nDim];
input.dimensions( dims_orig );
Img<T> image_cost_orig = input.copy();
///////////////////////////////////////////////////////////////////////////////////////////////////
// downsampling the input image ///////////////////////////////////////////////////////////////////
Img<T> image_cost_ds = img_utils.downsample(image_cost_orig, new float[] {downsample_factor_xy, downsample_factor_xy, downsample_factor_z});
///////////////////////////////////////////////////////////////////////////////////////////////////
// creating a surface detector solver instance ///////////////////////////////////////////////////
MinCostZSurface<T> ZSurface_detector = new MinCostZSurface<T>();
///////////////////////////////////////////////////////////////////////////////////////////////////
// filling the surface graph for a single surface
start = System.currentTimeMillis();
ZSurface_detector.Create_Surface_Graph(image_cost_ds, max_dz);
ZSurface_detector.Create_Surface_Graph(image_cost_ds, max_dz);
ZSurface_detector.Add_NoCrossing_Constraint_Between_Surfaces(1, 2, min_dist, max_dist);
end = System.currentTimeMillis();
System.out.println("...done inserting edges. (" + (end - start) + "ms)");
///////////////////////////////////////////////////////////////////////////////////////////////////
// computing the maximum flow //////////////////////////////////////////////////////////////////////
System.out.println("Calculating max flow");
start = System.currentTimeMillis();
ZSurface_detector.Process();
float maxFlow = ZSurface_detector.getMaxFlow();
end = System.currentTimeMillis();
System.out.println("...done. Max flow is " + maxFlow + ". (" + (end - start) + "ms)");
/////////////////////////////////////////////////////////////////////////////////////////////
// building the depth map, upsample the result and display it //////////////////////////////
//IJ.log("n surfaces: " + ZSurface_detector.getNSurfaces() );
Img<FloatType> depth_map1 = ZSurface_detector.get_Altitude_Map(1);
Img<FloatType> depth_map2 = ZSurface_detector.get_Altitude_Map(2);
upsampled_depthMap1 = img_utils.upsample(depth_map1, new long[] { dims_orig[0], dims_orig[1]}, img_utils.Interpolator.Linear );
upsampled_depthMap2 = img_utils.upsample(depth_map2, new long[] { dims_orig[0], dims_orig[1]}, img_utils.Interpolator.Linear );
//ImageJFunctions.show( depth_map1, "altitude map1" );
//ImageJFunctions.show( depth_map2, "altitude map2" );
Cursor<FloatType> up_map_cursor1 = upsampled_depthMap1.cursor();
Cursor<FloatType> up_map_cursor2 = upsampled_depthMap2.cursor();
while(up_map_cursor1.hasNext())
{
up_map_cursor1.next().mul(1/ downsample_factor_z);
up_map_cursor2.next().mul(1/ downsample_factor_z);
}
//IJ.log("creating z surface reslice" );
//outputExcerpt1 = img_utils.ZSurface_reslice(image_orig, upsampled_depthMap1, output_height/2, output_height/2);
//ImageJFunctions.show(excerpt1, "excerpt");
//outputExcerpt2 = img_utils.ZSurface_reslice(image_orig, upsampled_depthMap2, output_height/2, output_height/2);
//ImageJFunctions.show(excerpt2, "excerpt");
System.out.println("processing done");
}
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.ui().showUI();
// ask the user for a file to open
final File file = ij.ui().chooseFile(null, "open");
if (file != null) {
// load the dataset
final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());
// show the image
ij.ui().show(dataset);
// invoke the plugin
ij.command().run(MinCost2ZSurface_Ops.class, true);
}
}
}
package de.mpicbg.scf.mincostsurface;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ops.AbstractOp;
import net.imagej.ops.Op;
import net.imglib2.Cursor;
import net.imglib2.img.Img;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.RealType;
import net.imglib2.type.numeric.real.FloatType;
import org.scijava.ItemIO;
//import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import java.io.File;
@Plugin(type = Op.class, menuPath = "Plugins>MinCostZSurface", name="MinCostZSurface", headless = true, label="MinCostZSurface")
public class MinCostZSurface_Ops< T extends RealType<T> & NativeType< T > > extends AbstractOp {
//
// should implement ops if I want to benefit the matching mechanism
//
//@Parameter ( label = "input image" )
//private ImagePlus imp_orig;
@Parameter ( label = "cost image" , persist = false )
private Img input;
// parameter related to the initial surface detection
int channel = 1;
@Parameter ( label = "rescale xy" ) // downsampling factor of the input image for the direction x and y
private float downsample_factor_xy;
@Parameter ( label = "rescale z" ) // downsampling factor of the input image for the direction z
private float downsample_factor_z;
@Parameter ( label = "Max_delta_z between adjacent voxel" ) // constraint on the surface altitude change from one pixel to another
private int max_dz;
//@Parameter( label = "output number of slice" ) // range of pixel grabbed around the surface to build the output
//private int output_height;
// output
//@Parameter (type = ItemIO.OUTPUT)
//private Img<T> outputExcerpt;
@Parameter (type = ItemIO.OUTPUT)
private Img<FloatType> upsampled_depthMap;
@Override
public void run() {
process( (Img<T>) input );
}
public void process( Img<T> input ){
int nDim = input.numDimensions();
if ( nDim != 3 ) {
System.err.println("The data should be of dimensions 3 (found " + nDim + " dimensions)");
return;
}
long end,start;
long[] dims_orig = new long[nDim];
input.dimensions( dims_orig );
Img<T> image_cost_orig = input.copy();//ImagePlusAdapter.wrap(imp_cost_dup);
///////////////////////////////////////////////////////////////////////////////////////////////////
// downsampling the input image ///////////////////////////////////////////////////////////////////
Img<T> image_cost_ds = img_utils.downsample(image_cost_orig, new float[] {downsample_factor_xy, downsample_factor_xy, downsample_factor_z});
//ImageJFunctions.show( image_cost_ds );
///////////////////////////////////////////////////////////////////////////////////////////////////
// creating a surface detector solver instance ///////////////////////////////////////////////////
MinCostZSurface<T> ZSurface_detector = new MinCostZSurface<T>();
///////////////////////////////////////////////////////////////////////////////////////////////////
// filling the surface graph for a single surface
start = System.currentTimeMillis();
ZSurface_detector.Create_Surface_Graph(image_cost_ds, max_dz);
end = System.currentTimeMillis();
System.out.println("...done inserting edges. (" + (end - start) + "ms)");
///////////////////////////////////////////////////////////////////////////////////////////////////
// computing the maximum flow //////////////////////////////////////////////////////////////////////
System.out.println("Calculating max flow");
start = System.currentTimeMillis();
ZSurface_detector.Process();
float maxFlow = ZSurface_detector.getMaxFlow();
end = System.currentTimeMillis();
System.out.println("...done. Max flow is " + maxFlow + ". (" + (end - start) + "ms)");
/////////////////////////////////////////////////////////////////////////////////////////////
// building the depth map ///////////////////////////////////////////////////////////////////
//IJ.log("n surfaces: " + ZSurface_detector.getNSurfaces() );
Img<FloatType> depth_map = ZSurface_detector.get_Altitude_Map(1);
////////////////////////////////////////////////////////////////////////////////////////////////
// up-sample the depth_map result //////////////////////////////////////////////////////////////
upsampled_depthMap = img_utils.upsample(depth_map, new long[] { dims_orig[0], dims_orig[1]}, img_utils.Interpolator.Linear );
// multiply the altitude value to compensate earlier z sampling
Cursor<FloatType> up_map_cursor = upsampled_depthMap.cursor();
while(up_map_cursor.hasNext())
up_map_cursor.next().mul(1/ downsample_factor_z);
//ImageJFunctions.show( upsampled_depthMap, "altitude map" );
//IJ.log("creating z surface reslice" );
//outputExcerpt = img_utils.ZSurface_reslice(image_orig, upsampled_depthMap, output_height/2, output_height/2);
//ImageJFunctions.show(excerpt, "excerpt");
System.out.println("processing done");
}
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.ui().showUI();
// ask the user for a file to open
final File file = ij.ui().chooseFile(null, "open");
if (file != null) {
// load the dataset
final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());
// show the image
ij.ui().show(dataset);
// invoke the plugin
ij.command().run(MinCostZSurface_Ops.class, true);
}
}
}
/*
* To the extent possible under law, the ImageJ developers have waived
* all copyright and related or neighboring rights to this tutorial code.
*
* See the CC0 1.0 Universal license for details:
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package de.mpicbg.scf.mincostsurface;
import net.imagej.Dataset;
import net.imagej.ImageJ;
import net.imagej.ops.AbstractOp;
import net.imagej.ops.Op;
import net.imagej.ops.OpService;
import net.imglib2.img.Img;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.type.numeric.RealType;
import org.scijava.ItemIO;
//import org.scijava.command.Command;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;
import java.io.File;
// ImageJ collapse when running that plugin
//@Plugin(type = Command.class, menuPath = "Plugins>Z map reslice")
@Plugin(type = Op.class, name="zMapReslice", headless = true, label="zMapReslice", visible=true, menuPath = "Plugins>Z map reslice")
public class ResliceAlongZMap_Ops < T extends NumericType<T> & NativeType< T >, U extends RealType<U> > extends AbstractOp {
@Parameter ( label = "input image" )
private Img input;
@Parameter ( label = "z map" )
private Img zMap;
@Parameter ( label = "slice Above the z map" )
private int sliceAbove;
@Parameter ( label = "slice Below the z map" )
private int sliceBelow;
// output
@Parameter (type = ItemIO.OUTPUT)
private Img<T> outputExcerpt;
@Parameter
OpService op;
@Override
public void run() {
Img<T> input_img = (Img<T>) input;
Img<U> zMap_img = (Img<U>) zMap;
outputExcerpt = img_utils.ZSurface_reslice(input_img, zMap_img, sliceAbove, sliceBelow);
//op.morphology().top
}
public static void main(final String... args) throws Exception {
// create the ImageJ application context with all available services
final ImageJ ij = new ImageJ();
ij.ui().showUI();
// ask the user for a file to open
final File file = ij.ui().chooseFile(null, "open");
if (file != null) {
// load the dataset
final Dataset dataset = ij.scifio().datasetIO().open(file.getPath());
// show the image
ij.ui().show(dataset);
// invoke the plugin
ij.command().run(ResliceAlongZMap_Ops.class, true);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment