use of mpicbg.imglib.type.logic.BitType in project TrakEM2 by trakem2.
the class BinaryInterpolation2D method process.
/**
* The first time, it will prepare the distance transform images, which are computed only once.
*/
public Image<BitType> process(final float weight) {
synchronized (this) {
if (null == idt1 || null == idt2) {
ExecutorService exec = Executors.newFixedThreadPool(Math.min(2, Runtime.getRuntime().availableProcessors()));
Future<IDT2D> fu1 = exec.submit(new NewITD2D(img1));
Future<IDT2D> fu2 = exec.submit(new NewITD2D(img2));
exec.shutdown();
try {
this.idt1 = fu1.get();
this.idt2 = fu2.get();
} catch (InterruptedException ie) {
throw new RuntimeException(ie);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
}
// Cannot just img1.createNewImage() because the container may not be able to receive data,
// such as the ShapeList container.
final ImageFactory<BitType> f = new ImageFactory<BitType>(new BitType(), new ArrayContainerFactory());
final Image<BitType> interpolated = f.createImage(new int[] { img1.getDimension(0), img1.getDimension(1) });
if (img1.getContainer().compareStorageContainerCompatibility(img2.getContainer())) {
final Cursor<IntType> c1 = idt1.result.createCursor();
final Cursor<IntType> c2 = idt2.result.createCursor();
final Cursor<BitType> ci = interpolated.createCursor();
while (ci.hasNext()) {
c1.fwd();
c2.fwd();
ci.fwd();
if ((c1.getType().get() * weight) + (c2.getType().get() * (1 - weight)) > 0) {
ci.getType().set(true);
}
}
c1.close();
c2.close();
ci.close();
} else {
System.out.println("using option 2");
final LocalizableByDimCursor<IntType> c1 = idt1.result.createLocalizableByDimCursor();
final LocalizableByDimCursor<IntType> c2 = idt2.result.createLocalizableByDimCursor();
final LocalizableByDimCursor<BitType> ci = interpolated.createLocalizableByDimCursor();
while (ci.hasNext()) {
ci.fwd();
c1.setPosition(ci);
c2.setPosition(ci);
if (0 <= c1.getType().get() * weight + c2.getType().get() * (1 - weight)) {
ci.getType().set(true);
}
}
c1.close();
c2.close();
ci.close();
}
return interpolated;
}
use of mpicbg.imglib.type.logic.BitType in project TrakEM2 by trakem2.
the class AreaUtils method manyToManyInterpolation.
public static final Area[] manyToManyInterpolation(final Area a1, final Area a2, final int nInterpolates) throws InterruptedException, ExecutionException {
final Rectangle b = a1.getBounds();
b.add(a2.getBounds());
final AffineTransform translate = new AffineTransform(1, 0, 0, 1, -b.x, -b.y);
final ShapeList<BitType> shapeList1 = new ShapeListCached<BitType>(new int[] { b.width, b.height }, new BitType(false), 32);
shapeList1.addShape(a1.createTransformedArea(translate), new BitType(true), new int[] { 0 });
final Image<BitType> img1 = new Image<BitType>(shapeList1, shapeList1.getBackground(), "ShapeListContainer");
final ShapeList<BitType> shapeList2 = new ShapeListCached<BitType>(new int[] { b.width, b.height }, new BitType(false), 32);
shapeList2.addShape(a2.createTransformedArea(translate), new BitType(true), new int[] { 0 });
final Image<BitType> img2 = new Image<BitType>(shapeList2, shapeList2.getBackground(), "ShapeListContainer");
final float inc = 1.0f / (nInterpolates + 1);
final BinaryInterpolation2D interpol = new BinaryInterpolation2D(img1, img2, inc);
if (!interpol.checkInput()) {
System.out.println("Error: " + interpol.getErrorMessage());
return null;
}
final Area[] as = new Area[nInterpolates];
final AffineTransform back = new AffineTransform(1, 0, 0, 1, b.x, b.y);
// TODO parallelize, which needs the means to call process() in parallel too--currently it cannot,
// the result would get overwritten.
final ExecutorService exec = Executors.newFixedThreadPool(Math.min(nInterpolates, Runtime.getRuntime().availableProcessors()));
final ArrayList<Future<Area>> fus = new ArrayList<Future<Area>>();
try {
for (int i = 1; i <= nInterpolates; i++) {
final float weight = 1 - inc * i;
fus.add(exec.submit(new Callable<Area>() {
@Override
public Area call() throws Exception {
final Image<BitType> imb = interpol.process(weight);
final ImagePlus imp = ImageJFunctions.copyToImagePlus(imb, ImagePlus.GRAY8);
// BitType gets copied to 0 and 255 in 8-bit ByteProcessor
final ThresholdToSelection ts = new ThresholdToSelection();
ts.setup("", imp);
final ImageProcessor ip = imp.getProcessor();
ip.setThreshold(1, 255, ImageProcessor.NO_LUT_UPDATE);
ts.run(ip);
final Roi roi = imp.getRoi();
return null == roi ? new Area() : M.getArea(roi).createTransformedArea(back);
}
}));
}
int i = 0;
for (final Future<Area> fu : fus) {
as[i++] = fu.get();
}
} catch (final Throwable t) {
IJError.print(t);
} finally {
exec.shutdown();
}
return as;
}
Aggregations