Shift the lines of grid

hello everyone,
How can i shift the lines of the grid please ?

Do you mean the Analyze > Tools > Grid… command?
Did you try the Random offset option?

If this doesn’t help, and if you are a bit more specific in explaining what you’re trying to achieve, I believe someone here on the forum can give you more helpful advice.

yes i talk about this command.
I want to shift the squares to the right.

You can’t, at least not with this plugin.

Do you know another plugin that can do this? If not you can mention the person who can help me please?

You can try this plugin:

Or, if you need more flexibility, use any scripting language to add lines to the overlay as you like. You can find an example macro here:

I do not do the coding, I just do the image processing.thanks anyway

I encourage you to have a look at the documentation linked above if you want to achieve anything that doesn’t come out of the box with a plugin. It’s not difficult.

The macro language was created with users like you in mind.


Hi @Emna ,

It is possible there is some other feature in ImageJ that can accomplish your goal, so you will get better help if you can describe what you are doing with your images. A sample image, indicating what you want to measure, would be helpful too.

Can you explain more specifically what you want to accomplish?

Do you want to move the grid interactively to line up with some feature in your image?


Yes it is, I would like to go up for example the grid up or move it to the left

Hi @Emna , can you please provide more details? Why do you want to move the grid?

Since moving the grid interactively is not a built-in feature of ImageJ, we need to think a bit creatively to find another solution, and to do that, we need to understand the problem.


For example in this image, I want to translate the squares to put the balls in the square

Something like this?

Here is the macro I wrote to do that:

// @int minSize(min = 5, max = 500)
// @int maxSize(min = 5, max = 500)

function gridError(g, offset, xPoints, yPoints) {
	error = 0;
	for (i = 0; i < xPoints.length; i++) {
		x = xPoints[i];
		y = yPoints[i];
		// WLOG: translate to origin grid box
		xm = (x - offset) % g / g;
		ym = (y - offset) % g / g;
		xerr = abs(xm - 0.5);
		yerr = abs(ym - 0.5);
		error += xerr * xerr + yerr * yerr;
	return error;

// clear previous selections and ROIs
makeRectangle(1, 1, 1, 1);
run("Select None");

// find center points
run("Duplicate...", " ");
setAutoThreshold("Minimum dark");
run("Create Mask");
setOption("BlackBackground", true);
run("Fill Holes");
run("Ultimate Points");
setThreshold(1, 255);
run("Convert to Mask");
run("Create Selection");
roiManager("Select", 0);

// extract point coordinates
roiCount = roiManager("count");
xPoints = newArray(roiCount);
yPoints = newArray(roiCount);
for (r = 0; r < roiCount; r++) {
	roiManager("select", r);
	Roi.getBounds(x, y, width, height);
	xPoints[r] = x;
	yPoints[r] = y;

// find optimum grid size and offset
// for now: brute force!
bestG = 0;
bestO = 0;
bestError = 1/0; // infinity
for (g = minSize; g <= maxSize; g++) {
	for (offset = 0; offset < g; offset++) {
		error = gridError(g, offset, xPoints, yPoints);
		if (error < bestError) {
			bestG = g;
			bestO = offset;
			bestError = error;

// draw the best grid we found
print("Best grid: g=" + bestG + ", offset=" + bestO + ", error=" + bestError);
for (x = bestO; x < getWidth(); x += bestG) {
	Overlay.drawLine(x, 0, x, getHeight());
for (y = bestO; y < getHeight(); y += bestG) {
	Overlay.drawLine(0, y, getWidth(), y);

First it segments the balls, finding the center point of each (actually: the brightest point, from the reflected light). Then it tries to fit a grid with minimum squared error, as measured from each point to the center of that point’s containing grid box.

The real image processing experts on this forum may have suggestions for a better approach.

If you just want to do it by hand, you could just steal the final part of that macro which plots the horizontal and vertical lines as overlays.

1 Like

Hi @tswayne,

I have an image where I have a measuring scale in it and I want to align the grids to the scale i.e. shift 1mm to the left. How do I do this? Would be great if you could help me.

Welcome, @Sheethal! Sorry for the delay in answering.

I have found a new (to me) and easy way to do this, so thank you for inspiring this discovery.

  1. Run Analyze > Tools > Grid… to produce a grid of the desired size.
  2. Image > Overlay > To ROI Manager to transfer the overlay to the ROI Manager. You’ll see a new entry in the ROI Manager, “1-GRID.”
  3. Select the GRID in the ROI Manager, then in the ROI Manager window click More >> Translate…. Enter the number of pixels you want to move in x and y to line the grid up with the scale bar.
  4. If it looks ok, then go to Image > Overlay > From ROI Manager to transform the ROI back into an overlay.

Hope this helps!


Thank you so much @tswayne. It works well.
But, I’m wondering if this would be the right way to measure since translate takes onnly discrete values. So, I can’t give in values like 22.4 or 23.7, etc. as it rounds offf.
I’m also curious to know how did you find this solution :slight_smile:

You’re welcome!

How I found the solution – I knew overlays can be converted to selections, and vice versa (the macros earlier in the thread use this fact). I knew selections can be moved (e.g. with the keyboard). So I was playing around with a grid that I moved into the ROI Manager, and looking in the More… menu of the ROI Mgr. I noticed this additional Translate command.

I’m wondering if this would be the right way to measure since translate takes onnly discrete values. So, I can’t give in values like 22.4 or 23.7, etc. as it rounds offf.

From your question, I can’t tell why you would want to move the grid with subpixel resolution. Could you describe what you want to measure, and how you were thinking to do it, and post a sample image so we can better understand what you need? Thanks and hope this helps.

Upgrade to the ImageJ 1.53b daily build and you will be able to use the Image>Transform>Translate command to move the grid.


Thank you @tswayne.
Firstly, I’m sorry for responding so late.
Secondly, I don’t think I have to move the grid in continuous values. I now move pixel by pixel on the x-axis to get the suitable alignment. To briefly describe, I have an image where there is a scale and I’m trying to align the grids to the scale. But I realized, I just have to calibrate using ‘set scale’ and then move the grids to the required point.