How can I select and crop out a single grid cell?



Hi! My name is Cathy and I’m pretty new to ImageJ.

For my research project, I have a picture of a bird with a grid on top. I need help on selecting a single grid cell automatically and cropping it out without having to use the rectangle selection tool and create another box on top of the grid. I’ve tried doing this before, but it doesn’t work as smoothly. I have a feeling that I would need to mess around with java to do this, but I don’t have any coding experience. I do have patience though to try out new solutions.

Any advice helps!!




Good day Cathy,

as with all tools you get to know how to use them and to learn what can be done.
Consequently, the first step is to learn and study the ImageJ User Guide:

No Java coding is necessary!

If you think you need code, an ImageJ-macro should do. Please make sure you understand how to start macro coding with the hlep of the macro-recoder, which is really easy!

You didn’t tell us how you decide which cell to choose. I think if this question is answered, things are straightforward.



Both of your original images are JPG-compressed which are not suited for scientific work. Please use a professional camera that delivers uncompressed RAW-images.
(Converting a JPG-compressed image to TIFF- or PNG-format doesn’t make sense.)

It is rather easy to write a macro that extracts a cell that is defined by its coordinates …
Please report back.


Hello Cathy,

this simple macro helps you to understand and create rectangular selections (ROI’s) and add them to the ROI Manager. This is a simple copy of this older post. I added some comments for you as a beginner to understand the code.

Just change the default values for your needed grid (you can add some calculus to find the perfect grid - here I added a grid for your displayed image size of 6000x4000).

To crop a rectangle select the required rectangle in the ROI Manager or click on one label in the image and then crop.

x = 0; //the x-start coordinate
y = 0; //the y-start coordinate
width = 100; //the width of the rectangle
height = 100; //the height of the rectangle
spacing = 0; //spacing between the rectangles
numRow = 40; //how many rows
numCol = 60; //how many columns

/*Create the selections and add them to the ROI Manager!*/
for (i = 0; i < numRow; i++) {
    for (j = 0; j < numCol; j++) {
        xOffset = j * (width + spacing);
        yOffset = i * (height + spacing);
        /*Create a rectangular selection!*/
        makeRectangle(x + xOffset, y + yOffset, width, height);
        /*Add the selection to the ROI Manager!*/

/*Show all selections in the image with (mouse) selectable labels!*/
roiManager("Show All with labels");

More information about the ROI Manager and the macro language:



here is an ImageJ-macro that works with this demo image:

// imagej-macro "extractCell" (Herbie G., 06. Nov. 2018)
requires( "1.52d" );
nme = File.nameWithoutExtension;
run("Select None");
run("RGB to CMYK");
stck = getTitle();
Dialog.create("Cell coordinate");
Dialog.addNumber("Horizontal ", 0, 0, 3, "");
Dialog.addNumber("Vertical ", 0, 0, 3, "");;
i = round( Dialog.getNumber());
j = round( Dialog.getNumber());
run("Select All");
h = getProfile();
h = Array.findMaxima(h, 0.1);
v = getProfile();
v = Array.findMaxima(v, 0.1);
nh = h.length;
dh = round((h[nh-1]-h[0])/nh);
nv = v.length;
dv = round((v[nv-1]-v[0])/nv);
if (i>nh-2 || j>nv-2) exit("Position out of range!");
makeRectangle(h[i]+1, v[j]+1, dh, dv);
run("Duplicate...", "title=["+nme+"_cell@"+i+"-"+j+"]");
run("Set... ", "zoom=800 x=0 y=0");
// imagej-macro "extractCell" (Herbie G., 06. Nov. 2018)

Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it with the demo image open in ImageJ.
(The macro requires that the ImageJ-plugin “RGB_to_CMYK” is installed.)

Enter the desired cell-position to the dialog and please note that the position starts with zero, i.e. for the top-left cell use Hor 0, Vert 0 and for the right-bottom cell use Hor 25, Vert 16.

Please try the macro with your original images too. It should work for those as well (provided the grid lines are one pixel wide).
Please report back!