Crop multiple ROIs into individual images

imagej
macro
roi
imagecropper

#1

Hi all,

I am totally new to ImageJ and have no coding background.

I found this previous forum Saving each ROI as individual images that discussed how to save each ROI as an individual image using macros but I don’t know how to apply this to create a macro that does what I want.

Basically I want to be able to select multiple ROIs and then with one click crop them and save the cropped images as individual files.

Would anybody please be able to give a complete n00b introduction/explanation on how I could do this?

(I’m using a Mac if that impacts anything)

Many thanks for your help :slight_smile:


#2

If you do not care for output images being the same size, the code below should get you started.
Otherwise you can find the largest ROI from the roi manager using roiManager(“select”,i) and Roi.getBounds(x, y, width, height) in a loop for 1…roiManager(“count”), then first create a (stack of) images, for each ROI, select the ROI, copy from input, paste into the stack slice or output image.

run("Cardio (768K, RGB DICOM)");
run("ROI Manager...");
if(roiManager("count")!=0){
	roiManager("deselect");
	roiManager("delete");
}
makeOval(576, 206, 74, 54);
roiManager("Add");

makeOval(528, 325, 103, 87);
roiManager("Add");

makeOval(395, 231, 42, 26);
roiManager("Add");

roiManager("Select", 0);
run("Duplicate...", "1");

selectWindow("Cardio.dcm");
roiManager("Select", 1);
run("Duplicate...", "2");

selectWindow("Cardio.dcm");
roiManager("Select", 2);
run("Duplicate...", "3");

selectWindow("Cardio.dcm");
close();
run("Tile");

#3

Thanks, this worked in creating oval images. How about if I want the images to be extracted in the shape that the ROI was selected?


#4

This example uses a standard image from the samples and manually defined oval ROIs to get you a reproducible example. Rather than the ‘makeOval’ commands to create ROIs, you can replace them with your ROIs. Once your ROIs are in the Roi Manager, you now know how to get new images each containing a separate ROI.
If you mean that you want to have the exact contents of the roi and nothing outside the roi, do a ‘clear outside’ after duplicating the ROI. If you don’t want a black bacground, after duplicating (which will get you an exactly sized new image), you need to run copy, clear the image’s entire contents with a value you deem background, then run paste to get the contents witin the ROI only.