Decrease hexagon grid ROI's whilst keeping centroids in the same position

I have a Fiji macro which enables me to apply a hexagon grid to an image for sampling (it’s not my macro). I can change the size of the hexagons in the grid by changing the edge lengths in the macro. However, I now need to decrease the size of the hexagons now (below 500) whilst keeping the centre (centroid) of each hexagon in a fixed spot. This will obviously mean breaking up the grid, which is fine. I just need the x y coordinates to remain fixed. Is there a way to do this by adding to the code I have? Essentially I would like to start off with a hexagon grid, then decrease the sizes of the hexagons so it appears like there are multiple smaller hexagons inside each initial one.

Any help would be greatly appreciated as this is beyond my skill now.

l = 500;  // 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`Preformatted text`
roiManager ("deselect");
roiManager ("show all");
run("Analyze Particles...", "include ");;

numROIs= roiManager("Count");
for (j=0; j<numROIs; j++)
{
imgID= getImageID();
roiManager("select",j);
run("Analyze Particles...", "include ");;
}
type or paste code here

You could go through the list of ROIs and apply the Enlarge command with a negative argument (i.e. to shrink it), e.g.:
run("Enlarge...", "enlarge=-200");

1 Like

Thanks Gabriel,

That seems promising and it works for individual ROI selections however, when I add it to my code it says it is an unrecognised variable and won’t run it on all the hexagon ROI’s in the grid. Any ideas where I am going wrong here?

I tried that line after selecting manually one of the hexagonals ROIs that you macro generated and it shrunk it as expected.
I have no idea what variable error you are referring to might be because that line above has no variables. Perhaps inadvertently you introduced an error somewhere else, but without looking at the code it is impossible to know.
Anyway, what I suggested is to go through the list of ROIs and shrink them one by one, then analyse their contents. It might require more than just adding that line to your code.

1 Like

Hi,

at the end if full code is the code to reduce the size of hexgons. The key parts are:

1. Creating a new set of displacement vectors

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

The grid is made by stepping on the vectors [1,0] and [\frac{1}{2},\frac{\sqrt{3}}{2}]. The variables xSizf and ySizf is a second set of vectors which you can scale to suit your needs.

2 Use the scaled displacement vector in the RoI generation

 while (x + xSiz < width) {
      // build hexagonal ROI
      x1 = x + xSizf;
      y1 = y;
      x2 = x + xSizf / 2.0;
      y2 = y - ySizf;
      x3 = x - xSizf / 2.0;
      y3 = y - ySizf;
      x4 = x - xSizf;
      y4 = y;
      x5 = x - xSizf / 2.0;
      y5 = y + ySizf;
      x6 = x + xSizf / 2.0;
      y6 = y + ySizf;

Here’s a screen shot:

Cheers,

Chris

Full Code

l = 100;  // 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; xSizf=xSiz*0.3;
ySiz *= l; ySizf=ySiz*0.3;
xDel *= l;
yDel *= l;
xOff *= l;

getDimensions (width, height, nchannels,nslices, nframes);


// 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;
  print(x,y);
  while (x + xSiz < width) {
      // build hexagonal ROI
      x1 = x + xSizf;
      y1 = y;
      x2 = x + xSizf / 2.0;
      y2 = y - ySizf;
      x3 = x - xSizf / 2.0;
      y3 = y - ySizf;
      x4 = x - xSizf;
      y4 = y;
      x5 = x - xSizf / 2.0;
      y5 = y + ySizf;
      x6 = x + xSizf / 2.0;
      y6 = y + ySizf;
      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`Preformatted text`
roiManager ("deselect");
roiManager ("show all");
run("Analyze Particles...", "include ");;

numROIs= roiManager("Count");
for (j=0; j<numROIs; j++)
{
imgID= getImageID();
roiManager("select",j);
run("Analyze Particles...", "include ");;
}
3 Likes

Thanks both for your help. This has solved my problem!