Drawing circles of variable radii around points

fiji
jython
imagej
roi
python
draw

#1

I have made a Jython script which opens an image and asks the user to select points on the image using multipoint selection tool and add the points to ROI manager. This is the Jython script

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import WaitForUserDialog

imp = IJ.openImage("/Users/prakash/Desktop/1.tif");
imp.show()

IJ.run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");
WaitForUserDialog("","Move to slice where points are to be selected").show() 

IJ.run("ROI Manager...");
WaitForUserDialog("Select ROIs","Add points to ROI Manager").show() 
	
roi = RoiManager.getRoiManager()
roi_points = roi.getRoisAsArray()

After this I would like to do the following for each point.

Take one of the points as centre and ask the user for the radius of the circle that needs to be drawn around that point. Then the script should draw the circle and overlay on top of the image. Then the script should ask the user if the circle is satisfactory or not. If not satisfactory, ask the user for new radius for the same point. If satisfactory, save this radius with the corresponding centre coordinates in a csv file and then move to the next point and repeat the same procedure.

Is there any way to do this all in Jython? The reason being I have other Jython code snippets that do something else and I would like to integrate this process with the existing snippets.


#2

I figured out the following solution. Here is the Jython script. I will be interested to know if the script can be written better. I am a newbie to Jython scripting.

from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import WaitForUserDialog, OvalRoi, GenericDialog
from ij.measure import ResultsTable

def getOptions():

    gd = GenericDialog("radius Selection")
    gd.addNumericField("radius (pixel)", 3, 0)
    gd.showDialog()
    radius = gd.getNextNumber()
    return radius

def getConfirmation():
	gd1 = GenericDialog("radius confirmation")
	gd1.addMessage('Press 1 if circle is good, else select 0. Selecting 0 asks for new radius \n')
	gd1.addChoice('type', ['1', '0'], '1')
	gd1.showDialog()
	profileType = gd1.getNextChoice()
	return profileType

# configuration
folder = "/Users/prakash/Desktop/";

# reset the table at the beginning
table = ResultsTable.getResultsTable();
table.reset()

imp = IJ.openImage("/Users/prakash/Desktop/1.tif");

imp.show()

IJ.run("Set Scale...", "distance=0 known=0 pixel=1 unit=pixel");

WaitForUserDialog("","Move to slice where points are to be selected").show() 


IJ.run("ROI Manager...");
WaitForUserDialog("Select ROIs","Add points to ROI Manager").show() 
	
	
roi = RoiManager.getRoiManager()
roi_points = roi.getRoisAsArray()
print(roi_points)
print("Coordinates:")
for i in range (0, len(roi_points)):
		p = roi_points[i].getPolygon()
		x = p.xpoints
		y = p.ypoints

i = 0
j =0

while (i< len(x)):
	
	radius = getOptions()
	roi = OvalRoi(x[i]-radius, y[i]-radius, radius*2, radius*2);
	imp.setRoi(roi)
	profileType = getConfirmation()
	print(profileType)
	
	if (int(profileType) is 0):
		print("i:", i)
	else:
		j = j+1
                radiiList.append(radius)
		# write the analysed values in a table
		table = ResultsTable.getResultsTable();
		table.incrementCounter();
		table.addValue("center_x", x[i]);
		table.addValue("center_y",y[i]);
		table.addValue("radius",radius);
		table.show("Results");
		
		
	i = j

# save the table
table = ResultsTable.getResultsTable();
table.show("Results");
table.save(folder + "sampleResults.csv");
table.reset()

IJ.run("Close All", "");