use of mpicbg.imglib.image.ImageFactory 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;
}
Aggregations