Hexagonal grid ROI macro

I have a macro which enables analysis of images by dividing them up into squares and analysing the particles within them: http://imagej.1557.x6.nabble.com/grid-particle-count-td3699271.html.

Is there any way to replace the squares with hexagons, so that that the images I have are analysed in a honeycomb grid structure instead?

My coding skills are at their limit with creating squares and I don’t really know where to begin with using hexagons. I have tried replacing the ‘makeRectangle’ command with ‘makePolygon’ but it gets complicated when constructing a hexagon from the coordinates. I am also very new to using loops.

Any help would be greatly appreciated.

run("Set Measurements...", "area perimeter fit redirect=None  decimal=2");

A=20;   //Size of local area.

w = getWidth();
h = getHeight();
setForegroundColor(0, 0, 0);
for (j=1;j<=nSlices;j++){
        setSlice(j);
        for (ii=0;ii<h/A;ii++){
                for (i=0;i<w/A;i++){
                        run("Clear Results");
                        x=i*A;
                        y=ii*A;
                        makeRectangle(x, y, A, A);
                        run("Analyze Particles...", "size=0-Infinity circularity=0.00-1.00  show=Nothing include display summarize");
                        run("Draw");
                        print("Slice "+j+"Count: "+nResults);
                }
        }
}

Hello sgericeb -

Indeed, makePolygon() is the IJM function that you want.
You just have to sort out the geometry of the hexagons.

Here is a complete, runnable macro that tiles an image with
hexagonal ROIs of a chosen size:

// make an example image
newImage ("hexagons", "8-bit ramp", 512, 512, 1);

l = 20;  // length of hexagon side (pixels)

// hexagon geometry parameters
xIni = 1.0;
yIni = sqrt (3.0) / 2.0;
xSiz = xIni;
ySiz = yIni;
xDel = 3.0;
yDel = yIni;
xOff = 1.5;  // offset for every other row of hexagons

// scale up to get desired size
xIni *= l;
yIni *= l;
xSiz *= l;
ySiz *= l;
xDel *= l;
yDel *= l;
xOff *= l;

getDimensions (width, height, dummy, dummy, dummy);

// loop over rows of hexagons
iOff = 0;  // which staggered row of hexagons
y = yIni;
while (y + ySiz < height) {
  // loop over hexagons in a row
  x = xIni + (iOff % 2) * xOff;
  while (x + xSiz < width) {
      // build hexagonal ROI
      x1 = x + xSiz;
      y1 = y;
      x2 = x + xSiz / 2.0;
      y2 = y - ySiz;
      x3 = x - xSiz / 2.0;
      y3 = y - ySiz;
      x4 = x - xSiz;
      y4 = y;
      x5 = x - xSiz / 2.0;
      y5 = y + ySiz;
      x6 = x + xSiz / 2.0;
      y6 = y + ySiz;
      makePolygon (x1, y1, x2, y2, x3, y3, x4, y4, x5, y5, x6, y6);
      // add to ROI Manager
      roiManager ("add");
      
      x += xDel;
  }

  iOff++;
  y += yDel;
}

// display all hexagonal ROIs
roiManager ("deselect");
roiManager ("show all");

// flatten and save
run ("Flatten");
saveAs ("png", "hexagons");

And here is the flattened-overlay image generated by the macro:

Thanks, mm

3 Likes

Thank you mountain man! This was very helpful.