Hi Hannes,
On Mon, 11 Feb 2013, Hannes Cattrysse wrote:
> The program I have written is trying to convert a random Roi to a
> PolygonRoi. The problem arises when the random Roi is a ShapeRoi. With
> my code I get an error message. Does anyone know a workaround to get my
> ShapeRoi to a PolygonRoi?
>
> private Roi[] roiArray;
> private PolygonRoi roi;
> private Rectangle rect;
> private RoiManager rm;
>
> roiArray = rm.getRoisAsArray();
>
> for( int i = 0 ; i < rm.getCount() ; i++){
>
> roi = (PolygonRoi) roiArray[i];
> rect = roi.getBounds();
>
> }
First a note: since this is Java, you are allowed to declare the variables
when you use them, unlike C89, where you have to declare them at the top.
Second note: a cast in Java is only possible if the variable to be cast is
of that type (in C++, there are cast operators which can transform the
value to conform to the desired type).
And a ShapeRoi is not a PolygonRoi. In fact, a ShapeRoi can contain many
PolygonRois, since a ShapeRoi is meant to encode composite ROIs.
Now, if you are sure that roiArray[i] is in fact a ShapeRoi, you can split
it into its components:
if (roiArray[i] instanceof ShapeRoi) {
ShapeRoi shapeRoi = (ShapeRoi)roiArray[i];
Roi[] rois = shapeRoi.getRois();
...
}
See
http://jenkins.imagej.net/job/ImageJ1-javadoc/javadoc/ij/gui/ShapeRoi.html#getRois%28%29Of course, the bigger problem is that you do not know much about those
ROIs (and as a matter of fact, the roiArray elements). They could be any
subclass of ij.gui.Roi.
If I were to handle just the polygons in the RoiManager, I would make a
method that takes a single PolygonRoi, say, handlePolygon(PolygonRoi roi)
and make another method looping over the Rois like this:
for (Roi roi : roiArray) {
if (roi instanceof PolygonRoi)
handlePolygon((PolygonRoi)roi);
else if (roi instanceof ShapeRoi) {
ShapeRoi shapeRoi = (ShapeRoi)roi;
for (Roi roi2 : shapeRoi.getRois())
if (roi2 instanceof PolygonRoi)
handlePolygon((PolygonRoi)roi2);
}
Ciao,
Johannes
--
ImageJ mailing list:
http://imagej.nih.gov/ij/list.html