use of ij.gui.ShapeRoi in project imagingbook-common by imagingbook.
the class RoiUtils method makePolygon.
@Deprecated
public static Roi makePolygon(Point2D[] points, double strokeWidth, Color color) {
Path2D poly = new Path2D.Double();
if (points.length > 0) {
poly.moveTo(points[0].getX(), points[0].getY());
for (int i = 1; i < points.length; i++) {
poly.lineTo(points[i].getX(), points[i].getY());
}
poly.closePath();
}
Roi shapeRoi = new ShapeRoi(poly);
shapeRoi.setStrokeWidth(strokeWidth);
shapeRoi.setStrokeColor(color);
return shapeRoi;
}
use of ij.gui.ShapeRoi in project imagej1 by imagej.
the class ThresholdToSelection method getRoi.
/*
* Construct all outlines simultaneously by traversing the rows
* from top to bottom.
*
* prevRow[x + 1] indicates if the pixel at (x, y - 1) is selected.
*
* outline[x] is the outline which is currently unclosed at the
* lower right corner of the previous row.
*/
Roi getRoi() {
if (showStatus)
IJ.showStatus("Converting threshold to selection");
boolean[] prevRow, thisRow;
ArrayList polygons = new ArrayList();
Outline[] outline;
int progressInc = Math.max(h / 50, 1);
prevRow = new boolean[w + 2];
thisRow = new boolean[w + 2];
outline = new Outline[w + 1];
for (int y = 0; y <= h; y++) {
boolean[] b = prevRow;
prevRow = thisRow;
thisRow = b;
for (int x = 0; x <= w; x++) {
if (y < h && x < w)
thisRow[x + 1] = selected(x, y);
else
thisRow[x + 1] = false;
if (thisRow[x + 1]) {
if (!prevRow[x + 1]) {
// - left != right: merge
if (outline[x] == null) {
if (outline[x + 1] == null) {
outline[x + 1] = outline[x] = new Outline();
outline[x].push(x + 1, y);
outline[x].push(x, y);
} else {
outline[x] = outline[x + 1];
outline[x + 1] = null;
outline[x].push(x, y);
}
} else {
if (outline[x + 1] == null) {
outline[x + 1] = outline[x];
outline[x] = null;
outline[x + 1].shift(x + 1, y);
} else if (outline[x + 1] == outline[x]) {
// System.err.println("subtract " + outline[x]);
// MINUS
polygons.add(outline[x].getPolygon());
outline[x] = outline[x + 1] = null;
} else {
outline[x].shift(outline[x + 1]);
for (int x1 = 0; x1 <= w; x1++) if (x1 != x + 1 && outline[x1] == outline[x + 1]) {
outline[x1] = outline[x];
outline[x] = outline[x + 1] = null;
break;
}
if (outline[x] != null)
throw new RuntimeException("assertion failed");
}
}
}
if (!thisRow[x]) {
// left edge
if (outline[x] == null)
throw new RuntimeException("assertion failed!");
outline[x].push(x, y + 1);
}
} else {
if (prevRow[x + 1]) {
// - right != left: push
if (outline[x] == null) {
if (outline[x + 1] == null) {
outline[x] = outline[x + 1] = new Outline();
outline[x].push(x, y);
outline[x].push(x + 1, y);
} else {
outline[x] = outline[x + 1];
outline[x + 1] = null;
outline[x].shift(x, y);
}
} else if (outline[x + 1] == null) {
outline[x + 1] = outline[x];
outline[x] = null;
outline[x + 1].push(x + 1, y);
} else if (outline[x + 1] == outline[x]) {
// System.err.println("add " + outline[x]);
// PLUS
polygons.add(outline[x].getPolygon());
outline[x] = outline[x + 1] = null;
} else {
outline[x].push(outline[x + 1]);
for (int x1 = 0; x1 <= w; x1++) if (x1 != x + 1 && outline[x1] == outline[x + 1]) {
outline[x1] = outline[x];
outline[x] = outline[x + 1] = null;
break;
}
if (outline[x] != null)
throw new RuntimeException("assertion failed");
}
}
if (thisRow[x]) {
// right edge
if (outline[x] == null)
throw new RuntimeException("assertion failed");
outline[x].shift(x, y + 1);
}
}
}
if (y % progressInc == 0) {
if (Thread.currentThread().isInterrupted())
return null;
if (showStatus)
IJ.showProgress(y * (PROGRESS_FRACTION_OUTLINING / h));
}
}
if (polygons.size() == 0)
return null;
if (showStatus)
IJ.showStatus("Converting threshold to selection...");
GeneralPath path = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
progressInc = Math.max(polygons.size() / 10, 1);
for (int i = 0; i < polygons.size(); i++) {
path.append((Polygon) polygons.get(i), false);
if (Thread.currentThread().isInterrupted())
return null;
if (showStatus && i % progressInc == 0)
IJ.showProgress(PROGRESS_FRACTION_OUTLINING + i * (1. - PROGRESS_FRACTION_OUTLINING) / polygons.size());
}
ShapeRoi shape = new ShapeRoi(path);
// try to convert to non-composite ROI
Roi roi = shape != null ? shape.shapeToRoi() : null;
if (showStatus)
IJ.showProgress(1.0);
if (roi != null)
return roi;
else
return shape;
}
use of ij.gui.ShapeRoi in project bioformats by openmicroscopy.
the class ROIHandler method saveROIs.
/**
* Save ROIs in the ROI manager to the given MetadataStore.
*
* @param store Where to store the rois.
*/
public static void saveROIs(MetadataStore store) {
Roi[] rois = readFromOverlays();
if (rois == null || rois.length == 0) {
rois = readFromRoiManager();
}
if (rois == null || rois.length == 0)
return;
List<String> discardList = new ArrayList<String>();
String roiID = null;
OME root = (OME) store.getRoot();
int roicount = root.sizeOfROIList();
int cntr = roicount;
ImagePlus imp = WindowManager.getCurrentImage();
for (int i = 0; i < rois.length; i++) {
String polylineID = MetadataTools.createLSID("Shape", cntr, 0);
roiID = MetadataTools.createLSID("ROI", cntr, 0);
Roi ijRoi = rois[i];
int c = ijRoi.getCPosition() - 1;
int z = ijRoi.getZPosition() - 1;
int t = ijRoi.getTPosition() - 1;
ImagePlus image = WindowManager.getImage(ijRoi.getImageID());
if (image == null) {
// pick the current image in that case
image = imp;
}
int pos = ijRoi.getPosition();
if (imp != null) {
if (imp.getNChannels() == 1 && imp.getNSlices() == 1) {
t = pos - 1;
} else if (imp.getNChannels() == 1 && imp.getNFrames() == 1) {
z = pos - 1;
} else if (imp.getNSlices() == 1 && imp.getNFrames() == 1) {
c = pos - 1;
}
if (t > imp.getNFrames() - 1 || c > imp.getNChannels() - 1 || z > imp.getNSlices() - 1) {
//
continue;
}
}
if (ijRoi.isDrawingTool()) {
// Checks if the given roi is a Text box/Arrow/Rounded Rectangle
if (ijRoi.getTypeAsString().matches("Text")) {
if (ijRoi instanceof TextRoi) {
store.setLabelID(polylineID, cntr, 0);
storeText((TextRoi) ijRoi, store, cntr, 0, c, z, t);
}
} else if (ijRoi.getTypeAsString().matches("Rectangle")) {
if (ijRoi instanceof Roi) {
store.setRectangleID(polylineID, cntr, 0);
storeRectangle(ijRoi, store, cntr, 0, c, z, t);
}
} else {
roiID = null;
String type = ijRoi.getName();
IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
}
} else if (ijRoi instanceof OvalRoi) {
// Check if its an oval or ellipse ROI
store.setEllipseID(polylineID, cntr, 0);
storeOval((OvalRoi) ijRoi, store, cntr, 0, c, z, t);
} else if (ijRoi instanceof Line) {
// Check if its a Line or Arrow ROI
boolean checkpoint = ijRoi.isDrawingTool();
if (!checkpoint) {
store.setLineID(polylineID, cntr, 0);
storeLine((Line) ijRoi, store, cntr, 0, c, z, t);
} else {
roiID = null;
String type = ijRoi.getName();
IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
}
} else if (ijRoi instanceof PolygonRoi || ijRoi instanceof EllipseRoi) {
if (ijRoi.getTypeAsString().matches("Polyline") || ijRoi.getTypeAsString().matches("Freeline") || ijRoi.getTypeAsString().matches("Angle")) {
store.setPolylineID(polylineID, cntr, 0);
storePolygon((PolygonRoi) ijRoi, store, cntr, 0, c, z, t);
} else if (ijRoi.getTypeAsString().matches("Point")) {
store.setPointID(polylineID, cntr, 0);
storePoint((PointRoi) ijRoi, store, cntr, 0, c, z, t);
} else if (ijRoi.getTypeAsString().matches("Polygon") || ijRoi.getTypeAsString().matches("Freehand") || ijRoi.getTypeAsString().matches("Traced") || ijRoi.getTypeAsString().matches("Oval")) {
store.setPolygonID(polylineID, cntr, 0);
storePolygon((PolygonRoi) ijRoi, store, cntr, 0, c, z, t);
}
} else if (ijRoi instanceof ShapeRoi) {
Roi[] subRois = ((ShapeRoi) ijRoi).getRois();
for (int q = 0; q < subRois.length; q++) {
polylineID = MetadataTools.createLSID("Shape", cntr, q);
roiID = MetadataTools.createLSID("ROI", cntr, q);
Roi ijShape = subRois[q];
if (ijShape.isDrawingTool()) {
// Checks if the given roi is a Text box/Arrow/Rounded Rectangle
if (ijShape.getTypeAsString().matches("Text")) {
if (ijShape instanceof TextRoi) {
store.setLabelID(polylineID, cntr, q);
storeText((TextRoi) ijShape, store, cntr, q, c, z, t);
}
} else if (ijShape.getTypeAsString().matches("Rectangle")) {
if (ijShape instanceof Roi) {
store.setRectangleID(polylineID, cntr, q);
storeRectangle(ijShape, store, cntr, q, c, z, t);
}
} else {
roiID = null;
String type = ijShape.getName();
IJ.log("ROI ID : " + type + " ROI type : " + "Arrow (Drawing Tool) is not supported");
}
} else if (ijShape instanceof Line) {
boolean checkpoint = ijShape.isDrawingTool();
if (!checkpoint) {
store.setLineID(polylineID, cntr, 0);
storeLine((Line) ijShape, store, cntr, 0, c, z, t);
} else {
roiID = null;
String type1 = ijShape.getName();
discardList.add(type1);
IJ.log("ROI ID : " + type1 + " ROI type : " + "Arrow (DrawingTool) is not supported");
}
} else if (ijShape instanceof OvalRoi) {
store.setEllipseID(polylineID, cntr, q);
storeOval((OvalRoi) ijShape, store, cntr, q, c, z, t);
} else if (ijShape instanceof PolygonRoi || ijShape instanceof EllipseRoi) {
if (ijShape.getTypeAsString().matches("Polyline") || ijShape.getTypeAsString().matches("Freeline") || ijShape.getTypeAsString().matches("Angle")) {
store.setPolylineID(polylineID, cntr, q);
storePolygon((PolygonRoi) ijShape, store, cntr, q, c, z, t);
} else if (ijShape.getTypeAsString().matches("Point")) {
store.setPointID(polylineID, cntr, q);
storePoint((PointRoi) ijShape, store, cntr, q, c, z, t);
} else if (ijShape.getTypeAsString().matches("Polygon") || ijShape.getTypeAsString().matches("Freehand") || ijShape.getTypeAsString().matches("Traced") || ijShape.getTypeAsString().matches("Oval")) {
store.setPolygonID(polylineID, cntr, q);
storePolygon((PolygonRoi) ijShape, store, cntr, q, c, z, t);
}
} else if (ijShape.getTypeAsString().matches("Rectangle")) {
store.setRectangleID(polylineID, cntr, q);
storeRectangle(ijShape, store, cntr, q, c, z, t);
} else {
roiID = null;
String type = ijShape.getName();
IJ.log("ROI ID : " + type + " ROI type : " + ijShape.getTypeAsString() + "is not supported");
}
}
} else if (ijRoi.getTypeAsString().matches("Rectangle")) {
// Check if its a Rectangle or Rounded Rectangle ROI
store.setRectangleID(polylineID, cntr, 0);
storeRectangle(ijRoi, store, cntr, 0, c, z, t);
} else {
roiID = null;
String type = ijRoi.getName();
IJ.log("ROI ID : " + type + " ROI type : " + rois[cntr].getTypeAsString() + "is not supported");
}
// Save Roi's using ROIHandler
if (roiID != null) {
store.setROIID(roiID, cntr);
store.setImageROIRef(roiID, 0, cntr);
cntr++;
}
}
}
use of ij.gui.ShapeRoi in project TrakEM2 by trakem2.
the class Patch method mousePressed.
@Override
public void mousePressed(final MouseEvent me, final Layer la, final int x_p, final int y_p, final double mag) {
final int tool = ProjectToolbar.getToolId();
final DisplayCanvas canvas = (DisplayCanvas) me.getSource();
if (ProjectToolbar.WAND == tool) {
if (null == canvas)
return;
Bureaucrat.createAndStart(new Worker.Task("Magic Wand ROI") {
@Override
public void exec() {
final PatchImage pai = createTransformedImage();
pai.target.setMinAndMax(min, max);
final ImagePlus patchImp = new ImagePlus("", pai.target.convertToByte(true));
final double[] fp = new double[2];
fp[0] = x_p;
fp[1] = y_p;
try {
at.createInverse().transform(fp, 0, fp, 0, 1);
} catch (final NoninvertibleTransformException e) {
IJError.print(e);
return;
}
final int npoints = IJ.doWand(patchImp, (int) fp[0], (int) fp[1], WandToolOptions.getTolerance(), WandToolOptions.getMode());
if (npoints > 0) {
System.out.println("npoints " + npoints);
final Roi roi = patchImp.getRoi();
if (null != roi) {
final Area aroi = M.getArea(roi);
aroi.transform(at);
canvas.getFakeImagePlus().setRoi(new ShapeRoi(aroi));
}
}
}
}, project);
}
}
use of ij.gui.ShapeRoi in project TrakEM2 by trakem2.
the class Selection method selectAll.
/**
* Select all objects under the given roi, in the current display's layer.
* If visible_only, then a Displayable is not selected when its visible boolean flag is false, or its alpha value is zero, or either of its width,height dimensions are 0.
*/
public void selectAll(Roi roi, boolean visible_only) {
if (null == display)
return;
if (null == roi) {
if (visible_only) {
selectAllVisible();
return;
}
// entire 2D bounds:
roi = new ShapeRoi(display.getLayerSet().get2DBounds());
}
ShapeRoi shroi = roi instanceof ShapeRoi ? (ShapeRoi) roi : new ShapeRoi(roi);
Area aroi = new Area(shroi.getShape());
AffineTransform affine = new AffineTransform();
Rectangle bounds = shroi.getBounds();
affine.translate(bounds.x, bounds.y);
aroi = aroi.createTransformedArea(affine);
Collection<Displayable> al = new ArrayList<Displayable>(display.getLayer().getDisplayables(Displayable.class, aroi, visible_only, true));
al.addAll(display.getLayer().getParent().findZDisplayables(ZDisplayable.class, display.getLayer(), aroi, visible_only, true));
final Rectangle tmp = new Rectangle();
if (visible_only) {
for (Iterator<Displayable> it = al.iterator(); it.hasNext(); ) {
Displayable d = (Displayable) it.next();
if (!d.isVisible() || 0 == d.getAlpha()) {
it.remove();
continue;
}
Rectangle box = d.getBounds(tmp, display.getLayer());
if (0 == box.width || 0 == box.height) {
it.remove();
// defensive programming
continue;
}
}
}
if (al.size() > 0)
selectAll(al);
}
Aggregations