ImageJ basics

Have you ever wanted to quantify something from an image? For example:

ImageJ is image-processing software specially made for scientific measurements. It is free to download at imagej.nih.gov and has a large online wiki.

ImageJ excells in automating image quantification. For example, counting hundreds of stars in 10+ images of the nightime sky. Someone could do this manually, but the star count can be less biased and more accurate (not to mention less tedious!) when automated.

Experienced ImageJ users can skip ahead to automation with macros, extending macros to automate quantification of all images in a folder, or [to my example macros].

Easy example

Let’s count the stars in an image of the nighttime sky:

  1. Save the image below to your device (see image credit below). File > Open. Practice image for counting stars.
  2. Edit > Options > Point tool. Use options: add to overlay and label points. Screencap. Point tool options in ImageJ.
  3. From the toolbar, select the Point tool. Click on each star to be counted. If you make a mistake, click on the unwanted point and press *delete*. Screencap. Stars counted with the point tool.

Removing bias in quantification through thresholding

In the star counting example, someone might count more than 9 stars. Or fewer. The person counting brings a certain bias as to what a star should look like. To remove bias, let’s define exactly what a star looks like. That way, different people analyzing this image will arrive at the same star count. Here’s how:

  1. File > Open. Open the original image.
  2. Image > Type > 8 bit. Reduce the color profile to 8-bit.
  3. Image > Adjust > Threshold > Apply. Use Threshold options: Intermodes, Red, and Dark background. Intermodes is one of many mathematical models for automated thresholding; it is the best option for this particular image. Screencap. Stars thresholded in ImageJ.
  4. Analyze > Analyze particles > OK. Use Analyze particles options: Show outlines, Clear results, Summarize, and Exclude on edges. Screencap. Use analyze particles to count stars in a thresholded image in ImageJ.

Nine stars are counted.

Screencap. Nine stars have been counted after thresholding.

Automated quantification with macros

With thresholding, counting 100 stars is as easy as counding 9 stars. However, threshold counting stars in dozens of images would be tedious because of the need to click through the menus. ImageJ macros can automate the click through. Let’s make a macro to count the stars:

  1. File > Open. Open the original image.
  2. Open the Macro recorder. Edit the name of the macro, ex. "CountStars.ijm" (Note- .ijm is the ImageJ Macro extension).
  3. Click through all the steps for threshold counting. The steps will be recorded.
    1. Image > Type > 8 bit
    2. Image > Adjust > Threshold > Apply. Use Threshold options: Intermodes, Red, and Dark background.
    3. Analyze > Analyze particles > OK. Use Analyze particles options: Show outlines, Clear results, Summarize, and Exclude on edges. Screencap. The Macro Recorder shows each of the menu options that have been clicked.
  4. In the recoder window, click *create*. A new .ijm window will open. Save the macro to the ImageJ > macros folder.
  5. Close all windows.
  6. File > Open. Open the original image.
  7. Plugins > Macros > Open. Open CountStars.ijm.
  8. Click on the .ijm window. New menu options will appear. Macros > Run macro. Nine stars are counted, just as if the menus were clicked through by hand.
  9. Test the macro on another star image.
    1. File > Open. Open the second image (use the one below). Second practice image for counting stars.
    2. Plugins > Macros > Open. Open CountStars.ijm.
    3. Click on the .ijm window. Macros > Run macro. Thirty two stars are counted. Screencap. Thirty two stars are counted in the second image.

Extending macros to automate quantification of all images in a folder

If you have hundreds of images to process, the clicks required to open and close each image become tedious. Adding a few lines to a macro can automate opening each image, running the macro, saving the results, and closing each image. Here’s how:

  1. Download the third example star image, below. Save all of three example images (StarsToCount 1, 2, and 3) into one folder. Put nothing else in this folder. Third practice image for counting stars.
  2. In ImageJ, Plugins > Macros > Edit. Open CountStars.ijm.
  3. File > Save As. Save the macro as "CountStarsInFolder.ijm"
  4. Add the following text to the macro (or copy-past from a [PDF](https://LeighHenderson.github.io/assets/ImageJ-MACRO-CountStarsInFolder.pdf) of the finished macro):
    1. Before the rest of the text in the macro window, add instructions to choose an input folder and an output folder (folders are called "directories" in ImageJ): dirInput = getDirectory("Choose  Directory "); dirOutput = getDirectory("Choose  Directory ");
    2. Add instructions to process each image, or "file," in the folder: list = getFileList(dirInput); setBatchMode(true); for (i=0;i<list.length;i++) { open(dirInput+ list[i]); }
    3. At the end of the macro, add instructions to save the thresholded image and close the open windows. saveAs("Jpeg", dirOutput + File.nameWithoutExtension + "_Threshold"); close(); }
  5. File > Save.
  6. Macros > Run macro.
  7. A dialogue box will open. Select the folder in which your images are stored.
  8. A second dialogue box will open. Select a different folder in which thresholded images will be placed. Do not make a new folder inside the folder from step 7.
  9. The macro will run. Star counts will appear in the Summary window (9, 32, and 19 stars in images 1, 2, and 3, respectively). Thresholded images will appear in the folder selected.

Editing macros

Edit the contents of “CountStarsInFolder.ijm” to improve the accuracy of counting, or completely rework the contents to perform your own image quantification tasks.

The sky’s the limit on macros [pun intended]. To infinity and beyond!

See my ImageJ protocols for quantifying lipid droplets for inspriation.

Starry sky image credit

Thanks to Davide De Martin for the starry sky image from the ESO/Digitized Sky Survey 2. All smaller sky images were cropped from the original.