Problem with drawing particle related shapes in images with scale

Hi,

I am trying write a macro that draws elipse axes (Major and Minor) and measures them for each particle. My images need to have a scale set, however when I do it, it does not work properly and lines are drawn shifted agains the elipse (measurements are done well). If I do not set the scale both drawing and measurements are done well.

imagej

Below is some reproducible example with a macro from https://imagej.nih.gov/ij/macros/examples/DrawParticleEllipseAxis.txt

 run("Blobs (25K)");

  run("Set Scale...", "distance=50 known=30 unit=um"); // just for test

  setAutoThreshold;
  run("Set Measurements...", " area mean centroid fit ");
  run("Analyze Particles...", "size=0 circularity=0 show=Ellipses display exclude clear ");
  run("RGB Color");
  for(i=0; i<nResults; i++) {
      x=getResult('X',i);
      y=getResult('Y',i);
      d=getResult('Major',i);
      a = getResult('Angle',i)*PI/180;
      setColor("blue");
      drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
      d=getResult('Minor',i);
      a=a+PI/2;
      setColor("red");
      drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
  }

I tried to get a pixel size with getPixelSize(unit, pixelWidth, pixelHeight) and correct values obtained with getResult() but it did not help.

Thanks for any help

Hi Mikolaij,

Your problem is due to the fact that the drawLine macro command wants the coordinates in pixel values regardless whether the image is scaled or not. So, you have to take care of the conversion. You can get the pixel scale with getVoxelSize(width, height, depth, unit) and then divide all the scaled measurements from the results table by the pixel width or height.
Below is a modified version of your macro code that draws the lines in the correct places regardless of the image scale.

Cheers,
Volko

run("Blobs (25K)");
  run("Set Scale...", "distance=1 known=0.5 unit=um"); // just for test
  setAutoThreshold;
  run("Set Measurements...", " area mean centroid fit ");
  run("Analyze Particles...", "size=0 circularity=0 show=Ellipses display exclude clear ");
  run("RGB Color");
  getVoxelSize(width, height, depth, unit);  //get pixel dimensions

  for(i=0; i<nResults; i++) {
      x=getResult('X',i)/width;				//convert coordinates to pixel values
      y=getResult('Y',i)/height;
      d=getResult('Major',i)/width;
      a = getResult('Angle',i)*PI/180;
      setColor("blue");
      drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
      d=getResult('Minor',i)/width;
      a=a+PI/2;
      setColor("red");
      drawLine(x+(d/2)*cos(a),y-(d/2)*sin(a),x-(d/2)*cos(a),y+(d/2)*sin(a));
  }
1 Like

Hi Volko,
thank you so much. I thought this might be an issue and tried the same with getPixelSize() but as it did not work I gave up.

All the best,
Mikolaj