Skip to content

Instantly share code, notes, and snippets.

@JeremyPike
Last active March 16, 2022 13:53
Show Gist options
  • Save JeremyPike/546fc3bfedce605db83775edbe5664d5 to your computer and use it in GitHub Desktop.
Save JeremyPike/546fc3bfedce605db83775edbe5664d5 to your computer and use it in GitHub Desktop.
imagej_roi_converter.py
from ij import IJ
from ij.plugin.frame import RoiManager
from ij.gui import PolygonRoi
from ij.gui import Roi
from java.awt import FileDialog
fd = FileDialog(IJ.getInstance(), "Open", FileDialog.LOAD)
fd.show()
file_name = fd.getDirectory() + fd.getFile()
print(file_name)
RM = RoiManager()
rm = RM.getRoiManager()
imp = IJ.getImage()
textfile = open(file_name, "r")
for line in textfile:
xy = map(int, line.rstrip().split(","))
X = xy[::2]
Y = xy[1::2]
imp.setRoi(PolygonRoi(X, Y, Roi.POLYGON))
# IJ.run(imp, "Convex Hull", "")
roi = imp.getRoi()
#######
roi_rect = roi.getBounds()
if roi_rect.x > 0 and roi_rect.y > 0 and roi_rect.x + roi_rect.width < imp.width - 1 and roi_rect.y + roi_rect.height < imp.height - 1:
rm.addRoi(roi)
else:
print("roi touching edge, not adding to manager")
#####
print(roi)
textfile.close()
rm.runCommand("Associate", "true")
rm.runCommand("Show All with labels")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment