An introduction to ImageJJan-Yves Ruzicka, Ph.D. Graduate from the University of Canterbury
In my experience, almost any particle sizing activity can be automated in some shape or form. While systems vary in how amenable they are to statistical analysis, all of them can be analysed with the help of software, usually making the process far less monotonous and time-consuming.
Most particle sizing scenarios are covered by the various tools and options contained in ImageJ, a particle and image analysis tool put out by the US National Institute of Health.
ImageJ is a free, cross-platform image analysis tool developed by the US NIH, and while it is pretty close to feature-complete, new updates do still occasionally come out. It’s somewhat extensible, and people have written a number of plugins for some of the more esoteric processes not covered by its basic functions.
One caveat about ImageJ is that it’s written by academics, not software developers. This means that while ImageJ is very, very good at analysing data, it’s sometimes a bit of a mission to locate the exact option you want in between all the menu bars. Hopefully this guide will show you the tools you need for your particular purpose.
All the images in this guide will be taken from ImageJ for Mac OS X. There’s enough similarity between platforms, however, that you should be able to work out where buttons and menus are located on the Windows or Linux equivalents.
ImageJ can be obtained from the NIH site.
When you open ImageJ, you’ll likely be greeted with this little toolbox:
These are the basic commands you can run once you have an image loaded. Of course, the problem right now is that you don’t have an image loaded.
Opening images in ImageJ is relatively straightfoward – simply select
File→Open from the menu (or on OS X, drag the image onto the ImageJ icon in the dock). You can zoom in and out of your image by hitting Ctrl+ or Ctrl- (⌘+ and ⌘- on OS X): the zoom will be centred around where your cursor is on the image, so if you want to zoom in on one particular detail, hover the cursor over it when you zoom. To pan around the image, hold down
Space and click-and-drag.
ImageJ will open most image formats, but will also open
dm3 files as produced by a number of EM micrograph programs. If you happen to have
dm3 files on hand, your scale will already be set – if not, you’ll have to rely on the scale bar. Thankfully, ImageJ has your back here as well.
To set the scale in your image, zoom in so the scale bar is almost filling your screen. From the toolbar, select the rectangular marquee tool (the left-most button) and select the scale bar by click-and-dragging. If you don’t get it right, you can adjust by clicking-and-dragging the adjustment squares on the edges of the rectangle. We’re trying to get the length of the scale bar here – to the nearest pixel, if possible.
Once you have the length of the scale bar, hit
m (for measure) or select
Analyze→Measure from the menu. A box should pop up with the measurements of your rectangular selection, including its width. Note down the width, as well as the length of the scale bar in nm, because we’re going to be needing these. Now select
Analyze→Set scale… from the menu bar.
In this dialogue, enter the length in pixels you just measured, the length in nm (or µm, or whatever scale your scale bar happens to be in) of the bar, and the units you’re using. Then click OK. Any measurements you make will now be in nm, not pixels – which will make everything else a whole heap easier.
Case study one: automatic particle sizing
If you’re lucky, your particles will be non-aggregating, spherical and high-contrast when compared to your background. If these all apply you can pretty much sit back and let ImageJ do all of the work for you. Here’s how.
1. Convert your image to monochrome. Hover your mouse over
Image→Type. If you have any type checked other than
32-bit, click on
8-bit. We’re telling ImageJ that our image is greyscale here – it doesn’t need to worry about colour, just about brightness(You may also notice that the image type is displayed right at the top of the image.).
2. Set a threshold. Now we’re going to tell ImageJ exactly what comprises a particle in this image. Select
Image→Adjust→Threshold… or type Cmd+Shift+T (⌘+⇧+T on OS X). You’ll see a box like this:
The top slider controls minimum brightness, the bottom slider controls maximum brightness. What we’re trying to do is define a “window of brightness” that represents particles in your image. In other words: if you’re doing SEM, your particles will be bright, and your background will be dark. You’re telling ImageJ that “any pixel that is within these brightness values is part of a particle”, and it’ll do its best to grab all the particles.
Drag the sliders until just your particles are selected. It’s OK if you get a few grainy pieces of background, but try to keep them to a minimum. If you’re doing this with a TEM image (dark images on a light background), you’ll want to either uncheck the “dark background” box, or just drag the top slider all the way to the left to start with. You can also play around with the dropdown boxes – the left is (I imagine) a way of controlling which algorithms are used if you click the
Auto button (i.e. the program will try to set the threshold for you), and the right changes how selected/unselected portions of the image are displayed.
Once you have your particles selected, click
Apply. ImageJ will now turn your greyscale image into black and white – every point on the image is either a particle, or background.
3. Size particles. Select
Analyze→Analyse Particles…. You will see the following window:
There’s a few options here – you may need a couple of attempts at particle sizing to get good results, so I’ll try to go through everything you can do with the various checkboxes here.
First, the size and circularity controls. You can automatically set the range of your particles using these boxes. If you don’t want to select any particles whose sizes are less than 4 nm^2, for example, you could type
4-Infinity into the size box. If none of your particles are above 100nm^2 either, you could type
4-100. Why would you want to do this? Background noise picked up in the threshold step will be interpreted as very small particles if you don’t put some kind of size minimum threshold in here, so you’ll probably want to filter these out. On the other end of the scale, if you have aggregates you can automatically filter them out by setting a size maximum threshold. Be careful that you don’t exclude legitimate particles with this technique.
Circularity is a measure of how circular each particle is. Particles in ImageJ are modelled with ellipses – sorry, no fractal particle sizing. An ellipse with a circularity of 0 is a straight line, while an ellipse with a circularity of 1 is a perfect circle. If your particles are relatively circular, you can put filters here to (again) remove background noise or aggregates.
The show dropdown box allows you to create an image based on the measurements you took. I recommend that you select one of the
Overlay options – these will overlay an outline of the measured particles on your original image, and also provide a number for each image. You can use this to match particular particles with the measurements provided to you.
There are also a number of check boxes, as usual very poorly documented. The important ones (for me) are:
- Display results will pop up a measurement window with all of the particles. This is kind of important, since I’m assuming you want to do something with this data.
- Clear results will remove your last attempt at measuring particles. Useful if you’re trying out a number of different techniques.
- Exclude on edges will not count particles that hit the edge of the image.
I recommend checking these, but keeping the other options unchecked for now(At least to start. By all means play around with these options and see what they do – some may be just what you need to analyse your data).
When you hit OK, ImageJ will find all the particles it can and output data for you. You’ll notice if you selected a show option that you either get a new image with your measured particles on it, or you’ll now have a nice overlay on your original image.
Case study two: Semi-automatic particle sizing
Sometimes your particles aren’t entirely perfect. Maybe you have a couple of aggregates you need to remove from your measurements, or you’ve got a bit of dirt in there that’s screwed up a couple of measurements. Thankfully, all is not lost. If you read through case study one above, you’re in luck, because the first three steps of this method are exactly the same. There’s an added fourth step, though, that makes things a little trickier:
4. Generate an outlined original image. This is particular handy if you find you’re selecting a lot of aggregates and will need to go through this data again to pick them out. When you select
Analyze Particles, select
Outlines from the Show dropdown. You’ll end up with a separate image with outline of your particles and tiny numbers inside them. Select
Edit→Invert (Ctrl+Shift+I, ⌘+⇧+I on OS X) to turn it from a black-on-white image to a white-on-black image. Now re-open the original image in ImageJ and select
Still with me? You should have something like this in front of you:
You should have three images open:
- Your thresholded black-and-white image of your particles
- Your white-on-black outline of the particles you’ve just counted
- Your just-reopened EM image, without anything done to it.
Notice how they all have different names in the title bar – the image you just opened probably ends with “-1”. In the Image Calculator, select the white-on-black outline and the newly reopened image form the two dropdown boxes, and make sure the operation selected is
Add. Click OK, and marvel at your newly-created EM-with-outlines.
You’ll notice that each measurement in the measurement window comes with an index. This correlates to the indexes shown on the image. Now you can go through your image, identify “bad” particles, and remove them from your measurements. I don’t actually recommend removing them here – I’ve added a sub-step when I talk about analysis in Excel below which goes through a better method.
Case study three: manual particle counting
Some days you can’t do any automatic particle counting at all. Maybe your particles are too close in colour to your background, or they aggregate too much. In that case, there’s nothing for it but to manually select and measure particles.
It’s not all doom and gloom, though. This method is still likely to be more accurate and faster than doing the whole thing by hand.
1. Set measurement parameters. Select
Analyze→Set Measurements…. Marvel at all the things you can measure. When you hit M, there’s a whole heap of things ImageJ can measure, only some of which actually matter. Some of the more interesting ones:
- Area does what it says on the tin – works out the area of the selection.
- Center of mass reports the x and y values of the area.
- Fit ellipse returns the semi-major and semi-minor axis for an ellipse drawn around your selection.
In my Ph.D. I’ve been looking at ellipsoid particles, so the fit ellipse is particularly handy. I also recommend you check the following:
- Display label will just added a column to your measurement table for the image it came from. This can be handy.
- Add to overlay will make an outline of your selection on the image once you’ve measured it. This is an invaluable tool to prevent you measuring the same particle ten times.
2. Measure particles. I recommend zooming in your image a bit, and setting it to full-screen. Now go to the toolbar: the first four tools are all some form of marquee selection tool:
- If your particles are spherical, go nuts with the oval selection tool.
- If you have squares, I guess you can use the rectangle tool.
- If your particles are elliptical, I recommend the ellipse tool, which is different from the oval tool. To select the ellipse tool, right-click on the oval selection marquee. To use this, you first draw a line along the semi-major axis of your particle, and then use the selection markers to adjust the semi-minor axis.
- If your particles are a different shape, you’re in for a fun time. By right-clicking on the oval selection tool, you can get the selection brush tool, which lets you “paint” a selection on a particle, but you’re going to be there for a while.
The process is pretty simple: find a particle, outline it, make sure the ouline fits, hit m to measure it (and add it to the measurement box), continue.
Analysing particle sizing data with Microsoft Excel
Note: This section assumes that you know the basics of operating Microsoft Excel, including how to write functions, auto-fill cells, and the like. If you don’t know this, ask around in your group, or have a search for tutorials on the subject.
All this data is no good if you can’t do something with it. Different people will argue that different tools are better at this sort of thing, but Microsoft Excel is the most accessible of these.
1. Import your data into Microsoft Excel Select the measurement window, with all your hard-won data. Hit Ctrl+A (⌘+A) to select all the data, and copy it with Ctrl+C (⌘+C). Open a new file in Excel and paste your data into cell A1. You’ll end up with something like this:
If you’re collecting data from multiple images, you can put one set of measurements in below another. This is why I like having a record of which particular image my data has come from – it helps when I have multiple images in one spreadsheet.
You can now go to town on your data. However, there’s some well-established techniques that are easy enough to automate that I’ll go through here.
1a. Thin out your data. If you’ve done some form of automatic particle sizing, you may want to remove bad measurements – aggregates, artefacts, things that aren’t your particles, what-have-you. I recommend doing this in Excel because in ImageJ if you delete a particle from the measurement window, it auto-updates every particle’s index, throwing all your previous indices off by one. In Excel, those indices are just values – when you delete one, the rest stay the same.
When I’ve done this in the past, I tended to create a couple of columns next to my raw data for the “filtered” data. I’d then go through the image, identify good particles, and put their indices in the filtered data section:
Column D is then determined by the formula:
Type this into cell D1 and drag down. For the rest of this article I’m assuming you didn’t do this step – you might need to make a few adjustments, but these should be trivial.
2. Average, median, standard deviation. You can get a surprising amount of data out of Excel just by using its functions. Here’s some that may be handy at this point:
- AVERAGE(range) calculates the mean of a range of data.
- MEDIAN(range) gives you the median
- STDEV(range) gives the standard deviation of your data
If you’re lucky, that’s all you need. If you need to produce a size distribution graph, read on.
3. Binning and size distribution. The default way of showing particle sizing data in literature is via a histogram of frequency appearing versus size. Since every particle is its own unique snowflake, we need to create “bins” of particle size, and categorise our particles based on which size range they fall under. Thankfully, you can automate this all in Excel:
First we collect some values. I determine the minimum and maximum particle size using the
MAX(range) functions, and the range of the data is obtained by subtracting these two. The number of bins is a number I pick – you can always alter this later. The range per bin is, unsurprisingly, the range divided by the number of bins.
Now we make the bins. The first row is slightly different from the others, so I’ll go over the formulae I use in that first.
- Ceiling is set to
=E3+E6– the minimum value + the range of each bin
- f is set to
COUNTIF(B:B,"<="&G3). This counts the number of values in the “area” column of my raw data which are less than or equal to the bin’s ceiling.
- f/N will remain a mystery for now
The second row (and every subsequent row) differs from the first in the following ways:
- Ceiling is set to
G3+E$6– the previous ceiling plus the range of each bin. The range is set to stay constant when we auto-fill this cell.
- f is set to
COUNTIF(B:B, "<="&G4)-SUM(H$3:H3). This is the same as before, but now we subtract all previous bins. Expressed another way: “The number of particles less than or equal to this bin’s ceiling, that are not already accounted for in another bin”. Note that by keeping the start of the
SUMconstant but leaving the end to change with the cell, we create an “expanding range” for the sum.
At the bottom of the f column I’ve placed a total cell, which is just the sum of all frequencies. It should add up to the number of particles you’ve analysed. f/N is simply each f value divided by this total – a normalised measure of frequency.
This is a pretty robust method of binning. You can do all sort of things to customise it – make your own range or bin size, change the number of bins, etc. etc. Unfortunately, Excel doesn’t do histograms, and since this is continuous data the correct way to display it is by histogram. I imagine this is doable using Origin on Windows – I use Gnuplot for OS X, which is less user-friendly than some apps, but is able to produce professional-looking graphs. Plotting graps using Gnuplot is a topic for another lesson, however – for the meantime, you’ll have to find your way around it on your own.