Multiple Cells in 1 image to single cells in multiple images?

Hi,

I’m looking to take images that have numerous cells (10-20) cells, and create a macro that will use a DAPI channel to create ROI of each individual cell, then make new images of each individual cell. Essentially I want to go from having 1 image with 15 cells, to 15 images of 1 single cell. Any ideas? Nothing I have tried yet has worked.

Justin

Using Analyze > Analyze Particles… you can add an ROI for each cell to the ROI manager.

To then create a copy of the original image with the size of each ROI (as you’d do manually by making the ROI an active selection and then Image > Duplicate… it), you can use a little script, e.g. in Python from the Script Editor:

# @ImagePlus imp

from ij.plugin import Duplicator
from ij.plugin.frame import RoiManager

dup = Duplicator()
rm = RoiManager.getInstance()

for roi in rm.getRoisAsArray():
    imp.setRoi(roi)
    dup.run(imp).show()
1 Like

Hi Jan!

Thanks a lot for your help.

I have just tried to use your script but I get an error that says imp not defined.

File “New_.py”, line 8, in
NameError: name ‘imp’ is not defined

at org.python.core.Py.NameError(Py.java:260)
at org.python.core.PyFrame.getname(PyFrame.java:257)
at org.python.pycode.pyx2.f$0(New.py:7)
at org.python.pycode.pyx2.call_function(New.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1275)
at org.scijava.plugins.scripting.jython.JythonScriptEngine.eval(JythonScriptEngine.java:76)
at org.scijava.script.ScriptModule.run(ScriptModule.java:174)
at org.scijava.module.ModuleRunner.run(ModuleRunner.java:167)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:126)
at org.scijava.module.ModuleRunner.call(ModuleRunner.java:65)
at org.scijava.thread.DefaultThreadService$2.call(DefaultThreadService.java:191)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

Justin

Did you also copy what looks like a comment at the beginning of the script?

# @ImagePlus imp

It uses the SciJava Parameter declaration that is part of ImageJ2 (and therefore any up-to-date Fiji installation).

2 Likes

Hi Jan,

I did not, I thought it was a comment line. I ended up getting to work by doing this:

from ij.plugin import Duplicator
from ij.plugin.frame import RoiManager
from ij import IJ

dup = Duplicator()
rm = RoiManager.getInstance()
imp = IJ.getImage()

for roi in rm.getRoisAsArray():
	

    imp.setRoi(roi)	
    dup.run(imp).show()

I added the # @ImagePlus imp line and it gives me an error “(imp) is required but unset”.

When the duplicator runs, alll the single cell images have the same name. The issue I’m having now is trying to save each individual image with a nested loop.

dir = "/Users/justinsimms/Desktop/testing/MIP/";
print(dir);
singleDir= dir + "Singles"
print(singleDir)
File.makeDirectory(singleDir);
list = getFileList(dir);
counter = counter + 1

for (i=0; i<list.length; i++){
	if (endsWith(list[i], ".tif"))
		open(dir+list[i]);
		imgName=getTitle();
	baseNameEnd=indexOf (imgName, ".tif");
	baseName=substring(imgName, 0, baseNameEnd);
	run("Duplicate...", "duplicate");
	selectWindow(baseName + "-1.tif");
	run("Split Channels");
	selectWindow("C3-" + baseName + "-1.tif");
	setAutoThreshold("Otsu dark");
	run("Threshold...");
	setOption("BlackBackground", false);
	run("Convert to Mask");
	selectWindow("C3-"+ baseName + "-1.tif");
	run("Fill Holes");
	run("Analyze Particles...", "size=500-Infinity display exclude clear include add");
	selectWindow("Composite-2-MIP.tif");
	run("New roi");
		for (i=0; i <list.length; i++){
			counter = counter + 1;
			selectWindow("DUP_Composite-2-MIP.tif");
			saveAs("Tiff", singleDir+counter+baseName + ".tif");
	}
}
run("Close All");

Any ideas? It saves all the images, throws an error once there are no new images to save but does not start on the next composite image.

1 Like

Yes, that’s the ImageJ1 way of doing it. The new parameter syntax is just much more concise.

Did you have an image opened when you started the script?

  • If not, make sure that you have an active image before runngin the script.
  • If yes, you might have hit a bug that I encountered a few times, but am not sure how to reproduce. Restarting Fiji will help in this case.

Oh, if you want to save the image on disk: why are you using a new macro if you can do the saving right within the loop of the first (python) script, after duplicating the image, and without even displaying it?

Just replace dup.run(imp).show() in the python script by e.g.:

IJ.saveAs(dup.run(imp), "Tiff", "/path/to/your/roi_" + roi.getName() + ".tif")

The roi.getName() part makes sure you’re saving each image with a unique name corresponding to the ROI list in the ROI manager.

2 Likes

Because I’m new and have very little idea what I am doing!

This works wonderfully, however, I don’t think it generates a unique name every time because it resets the ROI manager so there is the possibility the ROI name will be same for a new image. When I run this on about 10 images, I got 105 output images. So I added in a uuid to the output file and it produced 107 images!

from ij.plugin import Duplicator
from ij.plugin.frame import RoiManager
from ij import IJ
import uuid

dup = Duplicator()
rm = RoiManager.getInstance()
imp = IJ.getImage()
filename = str(uuid.uuid4())


for roi in rm.getRoisAsArray():

    imp.setRoi(roi)
    IJ.saveAs(dup.run(imp), "Tiff", "/path/to/file/folder/" + roi.getName() + filename+ ".tif")

Again, thank you so much! This has been very helpful.

2 Likes