python-3.ximagejpyimagej

is there any way to switch ImageJ macro code to python3 code?


I'm making an app in python3 and I want to use some function in imagej. I used macro recorder to switch to python code but it got really messy, now I don't know how to do next. Can someone help me, please.

Here is the marco recorder code and my marco code

imp = IJ.openImage("D:/data/data_classify/data_train/1/9700TEST.6.tiff40737183_2.jpg");
//IJ.setTool("line");
//IJ.setTool("polyline");
xpoints = [177,155,114,101,100,159,179];
ypoints = [82,94,109,121,133,163,173];
imp.setRoi(new PolygonRoi(xpoints,ypoints,Roi.POLYLINE));
IJ.run(imp, "Straighten...", "title=9700TEST.6.tiff40737183_2-1.jpg line=30");

my python3 code

mport imagej
from scyjava import jimport
ij = imagej.init('2.5.0', mode='interactive')

print(ij.getVersion())

imp = ij.IJ.openImage("D:/data/data_classify/data_train/1/9700TEST.6.tiff40737183_2.jpg")

xpoints = [177,155,114,101,100,159,179]
xpoints_int = ij.py.to_java(xpoints)
ypoints = [82,94,109,121,133,163,173]
ypoints_int = ij.py.to_java(xpoints)
straightener = jimport('ij.plugin.Straightener')
polyRoi = jimport('ij.gui.PolygonRoi')

and I don't know how to do next...


Solution

  • After a few days, I finally found the answer. It is important to understand the parameters of the function to write, I have referenced in: https://pyimagej.readthedocs.io/en/latest/ https://imagej.nih.gov/ij/developer/api/ij/module-summary.html in my example the next thing i need is polygonroi from the given coordinates. I found the required coefficients of PolygonRoi in the above website and determined to take as parameters PolygonRoi​(int[] xPoints, int[] yPoints, int nPoints, int type)

    Next, I found a way to convert my list of coordinates to an int[] which was shown in the pyimagej tutorial. In the type section, I can find it by trying print(int(roi.PolygonRoi)) and the result is 6, you can also find the reason in the website above in the Roi section The rest, the last thing you need to do is put that PolygonRoi into the Straightener function with the line value you want Here is my code for using macro Imagej in Python3

    import imagej
    from scyjava import jimport
    from jpype import JArray, JInt
    
    ij = imagej.init('2.5.0', mode='interactive')
    
    print(ij.getVersion())
    
    imp = ij.IJ.openImage("D:/AI lab/joint_detection/data/1/9700TEST.6.tiff133328134_1.jpg")
    
    
    xpoints = [124,126,131,137,131,128,121,114]
    xpoints_int = JArray(JInt)(xpoints)
    
    ypoints = [44,63,105,128,148,172,194,206]
    ypoints_int = JArray(JInt)(ypoints)
    
    
    straightener = jimport('ij.plugin.Straightener')
    polyRoi = jimport('ij.gui.PolygonRoi')
    roi = jimport('ij.gui.Roi')
    
    
    new_polyRoi = polyRoi(xpoints_int,ypoints_int,len(xpoints), int(roi.POLYLINE))
    imp.setRoi(new_polyRoi)
    straightened_img = ij.IJ.run(imp, "Straighten...", "title=9700TEST.6.tiff40737183_2-1.jpg line=30")
    ij.IJ.saveAs(straightened_img, 'Jpeg', './test.jpg')