Table of Contents

Particle Properties to Image

An ImageJ macro that creates a new (floating-point) image where the pixel value of each particle (or one point at the center of the particle) is equal to a property of the particle.

Dialog Parameters

screen shot
[For clarity, a Lookup Table and a Calibration Bar have been applied to the 'blobs.gif-Area' output using the Standard ImageJ commands]


Code

var saveMaskImage="", saveDataImage="", saveProperty="RawIntDen", saveFillWhat="Full Particle";
macro "Particle Properties To Image" {
  if (nImages==0) exit("No Image");

  propertyNames = newArray("Area","Mean","Min","Max","IntDen","RawIntDen") ;//same as results column headers
  fillNames = newArray("Point at Center", "Full Particle");
  imageNames = getOpenImageNames();
  if (!isOpen(saveMaskImage)) saveMaskImage=getTitle();
  if (!isOpen(saveDataImage)) saveDataImage=getTitle();
  Dialog.create("Particle Properties To Image...");
  Dialog.addChoice("Mask or Thresholded Image", imageNames, saveMaskImage);
  Dialog.addChoice("Image with Data", imageNames, saveDataImage);
  Dialog.addChoice("Property that Determines Pixel Value", propertyNames, saveProperty);
  Dialog.addChoice("Fill Area in Output", fillNames, saveFillWhat);
  Dialog.show();
  maskWindow = Dialog.getChoice();
  dataWindow = Dialog.getChoice();
  property = Dialog.getChoice();
  fillWhat = Dialog.getChoice();
  saveMaskImage = maskWindow;
  saveDataImage = dataWindow;
  saveProperty = property;
  saveFillWhat = fillWhat;

  setBatchMode(true);
  selectWindow(dataWindow);
  width = getWidth();
  height = getHeight();
  getPixelSize(unit, pixelWidth, pixelHeight);
  selectWindow(maskWindow);
  if ((width != getWidth()) || (height != getHeight))
    exit("Error:\nMask and Data image must have the same size");

  saveSettings();
  roiSavePath="";
  if (fillWhat=="Full Particle") {
    nRois=roiManager("count");
    if (nRois>0) {
      roiSavePath = getDirectory("temp")+getTime()+"_rois.zip";
      roiManager("Save", roiSavePath);
      selectWindow("ROI Manager");
      run("Close");
    }
    run("Set Measurements...", "area mean min integrated redirect=&dataWindow");
    run("Analyze Particles...", "display clear add");
    roiManager("Show None");
    newImage(dataWindow+"-"+ property, "32-bit", getWidth(), getHeight(), 1);
    for (i=0; i<nResults; i++) {
      if ((i%20)==0) showProgress(i/nResults);
      roiManager("select", i);
      v=getResult(property,i);
      run("Set...", "value=&v");
    }
    roiManager("Reset");
    showProgress(1.0);
  } else {
    run("Set Measurements...", "area mean min center integrated redirect=&dataWindow");
    run("Analyze Particles...", "display clear");
    newImage(dataWindow+"-"+ property, "32-bit", getWidth(), getHeight(), 1);
    for (i=0; i<nResults; i++) {
      x = getResult("XM",i);
      y = getResult("YM",i);
      v = getResult(property,i);
      setPixel(x/pixelWidth,y/pixelHeight,v);
    }
  }
  run("Select None");
  run("Remove Overlay");
  resetMinAndMax();
  setBatchMode("exit and display");
  run("Set Measurements...", "area mean min integrated redirect=None");
  restoreSettings();
  aspectRatio=pixelHeight/pixelWidth;
  run("Set Scale...", "distance=1 known=&pixelWidth pixel=&aspectRatio unit=&unit");
  if (roiSavePath != "") {
    roiManager("Open", roiSavePath);
    dummy = File.delete(roiSavePath);
  }
}

function getOpenImageNames() {
  setBatchMode(true);
  imageNames = newArray(nImages);
  for (i = 0; i<nImages; i++) {
    selectImage(i+1);
    imageNames[i] = getTitle();
  }
  setBatchMode(false);
  Array.sort(imageNames);
  return imageNames;
}

Extensions and Modifications

For further particle properties, add them to the propertyNames array near the top of the code, using their names as they appear in the column headings of the Results Table. Also add the corresponding item(s) to the run(“Set Measurements…” command roughly in the middle of the code (In case of doubt, use the Macro Recorder to see the appropriate keywords when running 'Set Measurements' manually).

If you don't want the Results Table to remain open after running the macro, insert the following code at the end (after run(“Set Scale…”):

selectWindow("Results");
run("Close");

See Also

ROI Color Coder