use of ij.gui.Wand in project TrakEM2 by trakem2.
the class Segmentation method magicWand.
public static Bureaucrat magicWand(final AreaWrapper aw, final Layer layer, final Rectangle srcRect, final int x_p_w, final int y_p_w, final List<Runnable> post_tasks, final boolean inverse, final boolean subtract) {
// Capture pointers before they are set to null
final AreaContainer ac = (AreaContainer) aw.getSource();
final AffineTransform source_aff = aw.getSource().getAffineTransform();
final Rectangle box = new Rectangle(x_p_w - Segmentation.fmp.width / 2, y_p_w - Segmentation.fmp.height / 2, Segmentation.fmp.width, Segmentation.fmp.height);
Bureaucrat burro = Bureaucrat.create(new Worker.Task("Magic Wand") {
public void exec() {
// Capture image as large as the fmp width,height centered on x_p_w,y_p_w
Utils.log2("fmp box is " + box);
ImageProcessor ip = Patch.makeFlatImage(ImagePlus.GRAY8, layer, box, 1.0, (Collection) layer.getDisplayables(Patch.class, new Area(box), true), Color.black);
// Apply wand
Wand wand = new Wand(ip);
String smode = WandToolOptions.getMode();
int mode = Wand.LEGACY_MODE;
if (null == smode) {
} else if (smode.startsWith("4"))
mode = Wand.FOUR_CONNECTED;
else if (smode.startsWith("8"))
mode = Wand.EIGHT_CONNECTED;
// 8-bit image
wand.autoOutline(ip.getWidth() / 2, ip.getHeight() / 2, WandToolOptions.getTolerance(), mode);
if (wand.npoints > 0) {
Area area = M.getArea(new PolygonRoi(wand.xpoints, wand.ypoints, wand.npoints, PolygonRoi.FREEROI));
if (inverse) {
Area b = new Area(new Rectangle(0, 0, box.width, box.height));
b.subtract(area);
area = b;
}
// Compose an Area that is local to the AreaWrapper's area
final AffineTransform aff = new AffineTransform(1, 0, 0, 1, box.x, box.y);
try {
aff.preConcatenate(source_aff.createInverse());
} catch (NoninvertibleTransformException nite) {
IJError.print(nite);
return;
}
area.transform(aff);
if (subtract)
aw.getArea().subtract(area);
else
aw.getArea().add(area);
ac.calculateBoundingBox(layer);
Display.repaint(layer);
}
}
}, layer.getProject());
if (null != post_tasks)
for (Runnable task : post_tasks) burro.addPostTask(task);
burro.goHaveBreakfast();
return burro;
}
Aggregations