pythonjythonimagejimagej-macro

ImageJ: NameError when using selectionType() in Python script


I am a newbie to ImageJ and was trying to recreate the following macro using Python (in ImageJ macro editor).

s = selectionType();

if( s == -1 ) {
    exit("There was no selection.");
} else if( s != 10 ) {
    exit("The selection wasn't a point selection.");
} else {
    getSelectionCoordinates(xPoints,yPoints);
    x = xPoints[0];
    y = yPoints[0];
    showMessage("Got coordinates ("+x+","+y+")");
}

The problem is, I don't know how to import selectionType() and getSelectionCoordinates() builtin functions. I get NameError when I try to use them in the Python Code.

Any ideas?

Thanks, Alex


Solution

  • Unfortunately, ImageJ 1.x's built-in macro functions are not first-class Java methods, and hence not necessarily available from other script languages such as Python.

    You can read the Java source to see what the macro functions do, but it takes some effort in some cases to decipher. E.g., the getSelectionCoordinates function can be seen here.

    In short: it calls the getRoi() method of ImagePlus and then depending what kind of Roi it is, populates the coordinates differently. For Roi.LINE type, the x1d, y1d, x2d and y2d fields are used. Otherwise, the getFloatPolygon() method of Roi is called to convert the ROI to a polygon type, and then its coordinates are walked.

    For the selectionType function, just call getRoi() on the ImagePlus.