Have you ever wanted to quantify something from an image? For example:
- The number of stars in a photo of the nighttime sky
- The number of cells under a microscope, and the area of each
- The distance between two points on a map
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.
Easy example
Let's count the stars in an image of the nighttime sky:
- Save the image below to your device (see image credit below).
File > Open

- Open the point tool menu.
Edit > Options > Point tool
Select the options to add to overlay and label points.

- 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*.

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:
- Open the original image.
File > Open
- Reduce the color profile to 8-bit.
Image > Type > 8 bit
- Add a threshold.
Image > Adjust > Threshold > Apply
The best threshold for this image is "Intermodes" with options for "Red", and "Dark background".
Thresholds are mathematical algorithms that select some parts of a photo but not other parts.
Try out other thresholds to see how it works.
All photos in one dataset should be analyzed with the same threshold.

- Tell ImageJ to count the particles (ie. stars).
Analyze > Analyze particles > OK
Select the following options from the Analyze particles menu: "Show outlines," "Clear results," "Summarize," and "Exclude on edges"

Nine stars are counted.
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:
- Open the original image.
File > Open
- Open the Macro recorder. Edit the name of the macro, ex. "CountStars.ijm" (Note- .ijm is the ImageJ Macro extension).
- Click through all the steps for threshold counting (steps 2-4 in the previous section). The steps will be recorded in the "recorder" window.
Image > Type > 8 bit
Image > Adjust > Threshold > Apply
Use Threshold options: "Intermodes", "Red", and "Dark background"
Analyze > Analyze particles > OK
Use Analyze particles options: "Show outlines," "Clear results," "Summarize,"" and "Exclude on edges"

- In the recoder window, click
create
. A new .ijm window will open. Save the macro to the ImageJ > macros folder.
- Close all windows.
- Open the original image.
File > Open
- Open CountStars.ijm.
Plugins > Macros > Open
- Click on the .ijm window. New top menu options will appear. Run the macro.
Macros > Run macro
Nine stars are counted, just as if the menus were clicked through by hand.
- Test the macro on another star image:
- Open the second image (use the one below).
File > Open

- Open CountStars.ijm.
Plugins > Macros > Open
- Click on the .ijm window. Run the macro.
Macros > Run macro
Thirty two stars are counted.

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:
- 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.

- Open CountStars.ijm in ImageJ.
Plugins > Macros > Edit
- Save the macro as "CountStarsInFolder.ijm"
File > Save As
- Add the following text to the macro (or copy-paste from this PDF):
- 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 ");
- 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]);
}
- 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();
}
- Save the macro.
File > Save
- Run the macro.
Macros > Run macro
- A dialogue box will open. Select the folder in which your images are stored.
- 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.
- 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.
- On the accuracy front, you may have noticed that there is a mistake made on "StarsToCount2.jpg": two stars appear so close together that they are counted as one. This type of error can be dealt with by adding a watershed step.
- On your own star images, you may notice that the stars aren't counted properly. You'll find the problem by examining the saved thresholded images: thresholding is failing. Thresholding takes the overall brightness and contrast of an image into account. Therefore, pictures taken with differet settings or at different times/locations often require different thresholds. Consider chaning the threshold to the one that works best for your images. Note that using different thresholds on images in a set can alter count results; controlling for camera settings and time/location in batches of images is the best way to avoid threshold bias.
- Test the star counting macro on images of other objects to be counted. For example, try counting cell nuclei that have been stained with DAPI. As discussed above, the threshold may need to be changed.
- Set a scale for the image and then report the area of each star. See my ImageJ protocols for an example.
The sky's the limit on macros [pun intended]. To infinity and beyond!
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.