Skip to content

Commit

Permalink
Making issue fmi-faim#13 pull request ready.
Browse files Browse the repository at this point in the history
  • Loading branch information
Charismatic Claire committed Mar 23, 2021
1 parent 6ab38db commit 91bbc33
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ If you want to install this plugin manually via the KNIME ImageJ2 integration, y

| Project | Download `jar` from maven.imagej.net |
| --- | --- |
| [`AnalyzeSkeleton_`](https://github.com/fiji/AnalyzeSkeleton/) | [`AnalyzeSkeleton_-3.3.0.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/AnalyzeSkeleton_/3.3.0/AnalyzeSkeleton_-3.3.0.jar) |
| [`Descriptor_based_registration`](https://github.com/fiji/Descriptor_based_registration) | [`Descriptor_based_registration-2.1.2.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/Descriptor_based_registration/2.1.2/Descriptor_based_registration-2.1.2.jar) |
| [`AnalyzeSkeleton_`](https://github.com/fiji/AnalyzeSkeleton/) | [`AnalyzeSkeleton_-3.4.2.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/AnalyzeSkeleton_/3.4.2/AnalyzeSkeleton_-3.4.2.jar) |
| [`Descriptor_based_registration`](https://github.com/fiji/Descriptor_based_registration) | [`Descriptor_based_registration-2.1.7.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/Descriptor_based_registration/2.1.7/Descriptor_based_registration-2.1.7.jar) |
| [`Fiji_Plugins`](https://github.com/fiji/Fiji_Plugins) | [`Fiji_Plugins-3.1.1.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/Fiji_Plugins/3.1.1/Fiji_Plugins-3.1.1.jar) |
| [`fmi-trackmate-addons`](https://github.com/fmi-faim/fmi-trackmate-addons) | [`fmi-trackmate-addons-0.1.2.jar`](http://maven.imagej.net/service/local/repositories/releases/content/ch/fmi/fmi-trackmate-addons/0.1.2/fmi-trackmate-addons-0.1.2.jar) |
| [`guava`](https://github.com/google/guava) | [`guava-21.0.jar`](http://maven.imagej.net/service/local/repositories/central/content/com/google/guava/guava/21.0/guava-21.0.jar) |
| `jdom2` | [`jdom2-2.0.6.jar`](http://maven.imagej.net/service/local/repositories/bedatadriven/content/org/jdom/jdom2/2.0.6/jdom2-2.0.6.jar) |
| [`jgrapht-core`](https://github.com/jgrapht/jgrapht) | [`jgrapht-core-1.3.0.jar`](http://maven.imagej.net/service/local/repositories/central/content/org/jgrapht/jgrapht-core/1.3.0/jgrapht-core-1.3.0.jar) |
| [`jgrapht-core`](https://github.com/jgrapht/jgrapht) | [`jgrapht-core-1.4.0.jar`](http://maven.imagej.net/service/local/repositories/central/content/org/jgrapht/jgrapht-core/1.4.0/jgrapht-core-1.4.0.jar) |
| [`jts-core`](https://github.com/locationtech/jts) | [`jts-core-1.18.1.jar`](https://repo.maven.apache.org/maven2/org/locationtech/jts/jts-core/1.18.1/jts-core-1.18.1.jar) |
| [`legacy-imglib1`](https://github.com/fiji/legacy-imglib1) | [`legacy-imglib1-1.1.9.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/legacy-imglib1/1.1.9/legacy-imglib1-1.1.9.jar) |
| [`mpicbg`](https://github.com/axtimwalde/mpicbg/tree/master/mpicbg) | [`mpicbg-1.3.0.jar`](http://maven.imagej.net/service/local/repositories/releases/content/mpicbg/mpicbg/1.3.0/mpicbg-1.3.0.jar) |
| [`SPIM_Registration`](https://github.com/fiji/SPIM_Registration) | [`SPIM_Registration-5.0.19.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/SPIM_Registration/5.0.19/SPIM_Registration-5.0.19.jar) |
| [`TrackMate_`](https://github.com/fiji/TrackMate) | [`TrackMate_-4.0.0.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/TrackMate_/4.0.0/TrackMate_-4.0.0.jar) |
| [`mpicbg`](https://github.com/axtimwalde/mpicbg/tree/master/mpicbg) | [`mpicbg-1.4.1.jar`](http://maven.imagej.net/service/local/repositories/releases/content/mpicbg/mpicbg/1.4.1/mpicbg-1.4.1.jar) |
| [`SPIM_Registration`](https://github.com/fiji/SPIM_Registration) | [`SPIM_Registration-5.0.21.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/SPIM_Registration/5.0.21/SPIM_Registration-5.0.21.jar) |
| [`TrackMate_`](https://github.com/fiji/TrackMate) | [`TrackMate_-6.0.1.jar`](http://maven.imagej.net/service/local/repositories/releases/content/sc/fiji/TrackMate_/6.0.1/TrackMate_-6.0.1.jar) |
| [`TrackMate_extras`](https://github.com/tinevez/TrackMate-extras) | [`TrackMate_extras-0.0.4.jar`](http://maven.imagej.net/service/local/repositories/releases/content/org/scijava/TrackMate_extras/0.0.4/TrackMate_extras-0.0.4.jar) |
21 changes: 15 additions & 6 deletions src/main/java/ch/fmi/ContourPolygonBitmaskToText.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*-
* #%L
* <MY TEXT HERE...>
* Plug-in to convert a 2D binary to a polygon geometry serialized to
* well-known text (wkt).
* %%
* @author Charismatic Claire
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -26,6 +29,7 @@
import net.imglib2.type.logic.BitType;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.scijava.ItemIO;
import org.scijava.command.Command;
import org.scijava.command.ContextCommand;
Expand All @@ -46,15 +50,20 @@ public class ContourPolygonBitmaskToText extends ContextCommand {

@Override
public void run() {
Polygon2D polygon = (Polygon2D) opService.run(Ops.Geometric.Contour.class, input, true);
int numVertices = polygon.numVertices();
// get imglib2-roi polygon from input
Polygon2D polygon2d = (Polygon2D) opService.run(Ops.Geometric.Contour.class, input, true);
// get coordinates
int numVertices = polygon2d.numVertices();
Coordinate[] coordinates = new Coordinate[numVertices + 1];
for (int i = 0; i < numVertices + 1; i++) {
Double x = polygon.vertex(i % numVertices).getDoublePosition(0);
Double y = polygon.vertex(i % numVertices).getDoublePosition(1);
Double x = polygon2d.vertex(i % numVertices).getDoublePosition(0);
Double y = polygon2d.vertex(i % numVertices).getDoublePosition(1);
coordinates[i] = new Coordinate(x, y);
}
// create GIS polygon
GeometryFactory factory = new GeometryFactory();
output = factory.createPolygon(coordinates).toText();
Polygon polygon = factory.createPolygon(coordinates);
// return result
output = polygon.toText();
}
}
41 changes: 29 additions & 12 deletions src/main/java/ch/fmi/ContourPolygonTextToBitmask.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/*-
* #%L
* <MY TEXT HERE...>
* Plug-in to convert a polygon geometry represented in well-known text
* (wkt) to a 2D binary mask.
* %%
* @author Charismatic Claire
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -31,6 +34,8 @@
import net.imglib2.type.logic.BoolType;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;
Expand All @@ -52,34 +57,46 @@ public class ContourPolygonTextToBitmask extends ContextCommand {
@Parameter
private LogService logService;

@Parameter(label = "X Dimension")
private long xDim;

@Parameter(label = "Y Dimension")
private long yDim;

@Parameter(label = "Well-Known Text Polygons")
private String text;

@Parameter(type = ItemIO.OUTPUT)
private Img<BitType> output;

private FinalInterval envelopeToDims(Geometry envelope) throws Exception {
Coordinate boxMin, boxMax;
boxMin = envelope.getCoordinates()[0];
if(envelope.getDimension() == 2) boxMax = envelope.getCoordinates()[2];
else if(envelope.getDimension() == 1) boxMax = envelope.getCoordinates()[1];
else throw new Exception(
String.format("Your envelop <%s> stems from another world!", envelope.toText()));
long[] min = new long[] { (long) boxMin.getX(), (long) boxMin.getY() };
long[] max = new long[] { (long) boxMax.getX(), (long) boxMax.getY() };
return new FinalInterval(min, max);
}

@Override
public void run() {
GeometryFactory factory = new GeometryFactory();
WKTReader reader = new WKTReader(factory);
try {
// get GIS polygon from input
Polygon polygon = (Polygon) reader.read(text);
// construct bitmask
List<RealLocalizable> points = Arrays.stream(polygon.getCoordinates())
.map(coordinate -> new RealPoint(coordinate.getX(), coordinate.getY()))
.collect(Collectors.toList());
Polygon2D polygon2d = GeomMasks.polygon2D(points);
RealRandomAccessibleRealInterval<BoolType> mask = Masks.toRealRandomAccessibleRealInterval(polygon2d);
FinalInterval dims = new FinalInterval(new long[] {0, 0}, new long[] {xDim - 1, yDim - 1});
IntervalView<BoolType> view = Views.interval(Views.raster(mask), dims);
Polygon2D polygon2d = GeomMasks.polygon2D(points); // imglib2-roi polygon
RealRandomAccessibleRealInterval<BoolType> bitmask = Masks.toRealRandomAccessibleRealInterval(polygon2d);
// create view
FinalInterval dims = envelopeToDims(polygon.getEnvelope());
IntervalView<BoolType> view = Views.interval(Views.raster(bitmask), dims);
// create image
RandomAccessibleInterval<BitType> interval = Converters.convert(
(RandomAccessibleInterval<BoolType>) view, (in, out) -> out.set(in.get()), new BitType());
output = new ImgView<>(interval, new ArrayImgFactory<>());
Img<BitType> image = new ImgView<>(interval, new ArrayImgFactory<>());
// return result
output = image;
} catch (ParseException e) {
logService.warn(String.format("Could not create a polygon from input <{}>", text));
} catch(Exception e) {
Expand Down

0 comments on commit 91bbc33

Please sign in to comment.