Randomly select and extract grid boxes

Tags: #<Tag:0x00007fd53f7573a0> #<Tag:0x00007fd53f757238>


I am looking for a way to do the following:

  1. generate a grid overlay (boxes)
  2. assign number to each box in the grid
  3. then randomly select a set number of boxes (e.g. select 10 out of 100 grid boxes) from the grid
  4. crop/extract each randomly selected grid box
  5. overlay random point on each randomly selected box from the grid.

Is this possible in ImageJ?

Selecting grid-created ROIs based on an Initially Defined ROI

Hi @sjohnson,

Welcome to the forums!

Yes! I have made a script that I think works well. To run the script, copy and paste it in to the script editor (use the [ key to open the script editor) and select the language as “IJ1 Macro”. It is commented, but feel free to ask any questions.


    mainWindow = getTitle();
    close("\\Others"); // close all but selected window
    h = getHeight;
    w = getWidth;
    area = w*h;

    gridNum = getNumber("How many grid boxes in total?",100);
    boxArea = area/gridNum; // area of each grid box
    boxSide = sqrt(boxArea); // side length of each grid box
    numBoxY = floor(h/boxSide); // number of boxes that will fit along the width
    numBoxX = floor(w/boxSide); // "" height
    remainX = (w - (numBoxX*boxSide))/2; // remainder distance left when all boxes fit
    remainY = (h - (numBoxY*boxSide))/2;
    for (i=0; i<numBoxY; i++) { // draws rectangles in a grid, centred on the X and Y axes and adds to ROI manager
    	for (j=0; j<numBoxX; j++) {
    		makeRectangle((remainX + (j*boxSide)), (remainY+(i*boxSide)), boxSide, boxSide); 

    grids = getNumber("How many grid boxes to select?", 10);
    boxID = newArray(grids); 
    gridTotal = roiManager("count"); // not neccessarily as many boxes in the grid as specified due to the constraint of # squares not fitting the width/height

    for (i=0; i<grids; i++) { // generate random number to select a square in the grid, makes sure the square has not already been chosen
    	randNum = round(random*gridTotal);
    		for (j=0; j<boxID.length; j++) {
    			if (randNum == boxID[j]) {
    				randNum = round(random*gridTotal);
    boxID[i] = randNum; 
    roiManager("select", boxID[i]);
    run("Duplicate...", "duplicate"); // duplicates the random square in the grid box
    rename("Grid " + i);

    for (i=0; i<grids; i++) {
    	selectWindow("Grid " + i);
    	pointX = round(random*boxSide); // random X coord
    	pointY = round(random*boxSide); // random Y coord
    	makePoint(pointX, pointY);

I don’t know if there is already a function to make a grid, or code online (I assume you searched), but this is the best I could come up with.

EDIT: turns out there is a grid plugin - https://imagej.nih.gov/ij/plugins/grid.html (other examples of grid plugins/macros are available from this page also).


Thanks very much! Now I just need to figure out how to extract the random grids to a file. Much appreciated!


Do you mean you need the cropped grids to be saved in an automated way? As separate files, or a stack?


Yes I would like to save the cropped grids in a separate file.


Hi Selena,

You could add a saveAs function to the ‘for’ loop, I’ve assumed you want to save them as .tiff files here:

    for (i=0; i<grids; i++) {
        	selectWindow("Grid " + i);
        	pointX = round(random*boxSide); // random X coord
        	pointY = round(random*boxSide); // random Y coord
        	makePoint(pointX, pointY);
            saveAs("Tiff", outputDir + mainWindow + "Grid" + i);

You need to define outputDir, this can be defined at the start of your macro, by the user, or could be retrieved from the file you have opened most recently (assuming it is in the directory you want to save to).

For example:

outputDir = getDirectory("Choose folder to save grids to");
outputDir = File.directory;

If you want to repeat this macro for multiple files in a folder or subfolders, you may want to consider the template found in the script editor (Templates > IJ1 Macro > Process Folder). You will see there that you can insert your code to be run on all the files in a single folder + output the grids to another folder of your choice (outputDir).