Coccobiom2 - SEM measuring macros
revised March 2015
When working with coccoliths we often are able to study numerous specimens and from numerous samples. Given this quantitative comparisons of morphology can be very useful for refining and testing species concepts, examining possible ecophenotypic controls on morphology, studying evolution, improving biostratigraphy, etc. On the other hand there is often a problem that rather few parameters can be measured independently since coccoliths have rather simple morphologies and many potential parameters are strongly covariant - e.g. if working with placolith coccoliths it is often the case that coccolith length, width and number of elements are very strongly correlated so essentially a single parameter, size is responsible for the vast majority of variation in all three parameters. So a typical coccolith morphometric study will consist of measuring a few parameters from a very large number of specimens. Given this streamlining and automating the process as far as possible can make a massive difference to the quantity and quality of results. I have ben carrying out studies of this type with a range of collaborators over a long period and have used macros written in NIH-Image and its successor programs to facilitate this. The version documented here is a set developed for work with Emiliania and Gephyrocapsa.
Objective - a set of macros to measure Emiliania huxleyi and Gephyrocapsa coccoliths in distal view fast and accurately, including:
NB Most of the routines are not very specialised and could be applied to other genera of coccoliths or to other types of image with little or no modification. The macro code is not complex and can be modified by anyone with a little programming experience.
- For both genera, the distal shield length and width, central area length and width, shield width, collar width;
- For Emiliania - number and thickness of distal shield elements
- For Gephyrocapsa - bridge angle
- ImageJ is a free image analyse programme produced by National Institutes of Health (NIH) in the USA. It is a powerful and flexible programme written in Java, it is available for both Windows and Mac, and is widely used by image analysis professionals. In comparison to commercial image analysis programmes provided with e.g. microscope cameras it is a bit less user-friendly, but you never need to pay for upgrades and it is much more flexible in how you can modify it, write macros etc.
- The standard version is ImageJ. Fiji is an extended version with a neater upgrade path, I recommend Fiji. Both are extensively documented on the internet.
- New versions ImageJ-2 and Fiji2 were released in 2014, the macros have not been adjusted to work with them, yet.
- To install macros use menu item Plugins/Macros/Install...
- Once macros are installed they are available form the menu Plugins/Macros as a list of extra commands each usually with a keyboard shortcut listed in square brackets - e.g. "measure [m]"
- Macros can be run either from the menu, or by simply pressing the shortcut key, without any modifier keys. Since the cursor position is often used by the macro using the shortcut keys is normally essential, as well as being much quicker
- In the notes below the shortcut keys are given in square brackets and bold - e.g. [f5]
- A manual for the macro language is available on the ImageJ website
- Download Fiji (June 2014 version) from the website
- Download the archive containing macro files, some more documentation, and test images from download here.
- Install Fiji and unpack the archive.
- Open Fiji and experiment with its functions using the image supplied and/or your own images
- Install the macros (use menu item Plugins/Macros/Install...)- and find them listed under PlugIns/Macros
- Open the test image of E. huxleyi
- Experiment with setting the magnification using both built in Fiji tools and the macros - see below.
- Experiment with measuring the coccolith using both built in Fiji tools and the macros - see below.
- Read the rest of these notes.
- Send feedback - writing macros/getting a nice workflow takes a fairly long time, documenting it takes a lot more work. This is not a fully worked out step through yet and I do not know how far to go in producing one. I need feedback. Jeremy Young
Overview of the process
- Open new set of images using menu command: File/Import../Import sequence
- If the images are small then Rescale the images using menu command: Image/Scale..
In Dialog box set: X Scale 5.0; Y scale 5; interpolation bilinear; process entire stack; create new window
- Run macro set magnification [f5]
in dialog box set: SEM calibration - NOC, magnification/1000 - 25 [ignore other options]
- set distal shield using the ellipse selection tool then measure - 
- set outer edge of tube - place cursor in location where tube edge is clearly visible - 
- set inner edge of tube - 
- a. Emiliania only: observe accuracy of ray identification
b. Gephyrocapsa only: make bridge diagonal - [e]; rotate bridge diagonal - [s/d] set bridge diagonal - [r]
- select next image - 
This part is very easy. Open the results window; select all; copy and paste into an excel spreadsheet.
Details of the process
Setting up a batch of images
Typically with morphometric work you will want to measure a batch of say 50-60 images of a species from each of several samples. This requires working through numerous images an these macros assume each specimen is on a separate image. To build up the set of images see the separate image collecting macros. Having separate images for each specimen makes archiving and automation easier and is my preferred method. Process is basically:
- Prepare the images with each image being the same magnification, cropped to the same size
- and containing 1 coccolith lying flat in distal view.
- Transfer the set of images to a folder
- In ImageJ select menu option File/Import/Image sequence... and select the folder
- If desired use the dialog box options to enlarge all the images in the stack - this can help significantly with measuring. I hae worked succesfully with Ehuxleyi specimens captured as 100 x 100 pixel SEM images and enlarged to 500 x 500 pixel images.
- The program opens the images as a stack which you can rapidly work through without opening and closing individual images (look at menu Image/Stacks/ to see more things you can do with the stack of images). This will not work if the images are different sizes.
Setting the magnification
As with any image analysis program you can set the scale so that measurements are made in microns rather than pixels. There arre two ways to do this.
- From a scalebar using commands buit into ImageJ
- Measure the length of the scalebar in pixels
- Open dialog box from Analyze/Set Scale...
- From recorded magnification using the macro
- Run macro Plugins/Macros..?setmagnification [f5]
- Enter magnification in dialog box and SEM calibration
- The first time you do this you will not know the calibration for your SEM and you will need an image with both a magnification and a scalebar. To determine the calibration leave it at 1 and close dialog box. Then measure the scale bar. It should be measured as something near to its real length (typically from 0.5 to 2x real length). The ratio of the measured length to real length is the SEM calibration. E.g If the scale bar is 2microns long and it is measured as 2.4 microns then the SEM calibration is 1.2, and this value should hold for all images from the SEM taken with that image resolution (some SEMs offer the option to save images at different resolutions/image sizes).
Finally - don't assume the scalebars on your SEMs are correct - they can be wrong although this is less common now. An easy check is provided by E.huxleyi - the length of a well-formed E. huxleyi coccolith is very close to its length/10 - e.g an Ehux cocolth with 35 rays will be about 3.5µm long (+/- about 10%) if you measure a few specimens and they are consistently departing from this then suspect your scalebars are wrong.
Images from older SEMs may also be distorted - often the vertical scale is 5% less than the horizontal scale so the images should first be stretched in Photoshop or ImageJ.
The macros work fine on TEMs. For the ehux ray identification routine you need to switch between detecting bright and dark peaks using macro choose dark or bright peaks [f6].
 Set outside edge of coccolith by drawing an ellipse (select ellipse tool from the toolbar - NOT the oval tool), drag from the best defined end to opposite end of coccolith to set length and orientation of the ellipse; then adjust the ellipse width by dragging handles on the edges. key-
Adjustments to the ellipse can be made via the following macros (not that in practice you need to much).
[f] [t] - fatter thinner (NB once you have measured a couple of specimens then these can be used instead dragging handles)
[s] [d] rotate left/right (this can be useful). If you remember some latin s/d is sinistral/dextral.
[o] [l] [j] [k] move up/down left/right (rarely useful in practice)
[n] [h] shrink/grow (rarely useful in practice)
NB For each of these holding down the spacebar while pressing the key will result in larger adjustments
Rerunning the macro  initiates measuring of a new specimen, i.e. it will create a new row in the results window, so it should only be done once per specimen.
 Set outer edge of tube by placing mouse on inner edge of d shield at a point where it is well-defined. key- or [z]
If macro [z] is used then only the rim width is calculated (NB The macro measures the distance from the mouse to the nearest point on the rim ellipse, using a routine described in Young et al., 1994). This is displayed as an ellipse around the rim. Ideally this ellipse should neatly match the outer tube but it won't always. Deviations can result from irregular rim, tilted specimens or mis-set rim ellipse. If the last of these seems likely it may be worth remeasuring them (using the adjustment macros)
If macro  is used then in addition the distal shield rays are identified, counted and their average width measured.
To do this the macro samples the greyscale values around an ellipse halfway between the outer tube edge and the rim edge, then it detects local brightness maxima around this ellipse. These are identified by placing a red square on the image
Ray widths are then measured by finding their edges (on a brightness profile these are points of maximum steepness). These ray widths are shown on the image as red lines.
Finally the ray widths are ranked from widest to narrowest and widths of the middle 50% are averaged (this is done in order to eliminate rogue values from misidentified rays, merged rays etc). The selected ray widths are marked with black squares.
If macro [z] is run after macro  then the tube tube width will be set independently of the ray number. This is useful e.g. with type R Ehux where ray number can be set from the outer edge where the rays show serrated outline.
Rerunning macro  will recalculate the rim width, ray-number and ray-thickness based on the current cursor location and overwrite the previous results, so it can be done repeatedly without any problems.
 Set inner edge of tube by placing mouse on edge of central area at a point where it is well-defined. key-
NB The point to try to identify is inner top edge of the tube (not the base of the tube), this is often a bright edge.
Also note that although an ellipse is placed around the central area the parameter which is recorded is the spacing between this ellipse and the outer tube ellipse. If the inner tube magian is irregular then what we want is an estimate of the average tube thickness. For an Ehux with a completely closed central area click right in the centre of the specimen.
Rerunning the macro will recalculate the tube width based on the current cursor location and overwrite the previous results, so it can be done repeatedly without any problems. Macros  and  can be run in reverse sequence without any problems
[3a] Adjust ray-number (Emiliania)
The ray counting macro is much more successful than we might expect from the quality of the images, but on the other hand ray number is very strongly correlated with coccolith length -so scientifically it is probably not worth adjusting the ray counts unless there is reason to believe the population (or some of it) is unusual in this respect)
- If ray count is obviously poor, you can run macro  (flag poor ray count) this will put a value of -1 in the RayFlag column, thus noting that the ray number data from this specimen should be excluded from any analysis.
- Alternatively if the ray count is obviously good, you can run macro  (flag good ray count) this will put a value of +1 in the RayFlag column, thus noting that the ray number data from this specimen can be used for ray analysis. This is useful if ost images are too poor to give good ray count data
- If ray number is to be used then it should be checked and adjusted if necessary by using the macros  (decrease ray count) or  (increase ray count), the adjusted ray count is displayed in the top left of the image.
If ray number is consistently too high or low then adjust the "window" used to detect the rays by using [f7]. The default value is set based on the magnification and will give good results for E. huxleyi if the magnification is set correctly (Ie in part the ray identification macro 'cheats' because it is set up to detect rays at the spacing of Ehux rays). If rays are being consistently missed then decrease the window size; if too many 'rays' are being detected then increase the window value. The default value is set when macro [f5] is run. For TEM images the rays will appear dark not bright so use macro f6 to change to selecting dark peaks.
[3b] Measure the bridge orientation (Gephyrocapsa)
- A line is drawn through the centre of the coccolith at a likely bridge orientation. This is done by a macro [x] with no need for any user input (the mouse position is not used).
- The line is rotated until it is parallel to the bridge. This can be done manually by dragging the end points of the line (nb if you right click or press ctrl then the line will rotate about its mid point). Alternatively it can be done via macros [q] or [w] which rotate the bar in increments of 1°, holding the space bar results in rotation of 10°.
Alternatively - set orientation with a hand drawn line-selection (toggle between selection tools by using macro ) and if desired rotate this using [q] / [w] or set angle directly from it [s]
 select next image key
This steps to the next image in the stack, if the specimen if unsuitable it can be repeated
NB The < and > keys can also be used to step through the stack of specimens, but they will not remove the overlay so the dedicated macro  is better to use.
Oops - correcting mistakes! After selecting the next image it is important that the next macro run is . If any other macro is run then the results of the previous specimen will be changed, and displayed on the screen. An easy error to make is to press [t] or [f] to correct ellipse width before actually setting the outer ellipse (macro-). If you do this just do the reverse action to undo it (e.g. if you tapped [f] twice, then tap [t] twice to undo the effect), then [f8] to remove the overlay and start again. (To understand this, try it out observing also how the numbers in the results window change)
Tilted specimens and specimens on coccospheres
The measurement routine used here is vulnerable to tilt, or rather it makes the problems of making reliable measurements on tilted specimens very obvious. On tilted specimens the width on opposite sides of the coccolith will appear different the long axis orientation may be difficult to determine exactly, the axial cross will not lie at the centre of the specimen and the tube ellipses will not track the features nicely. Such specimens should not be measured unless there is a severe shortage of specimens. If you need to measure specimens on coccospheres I suggest experimenting with setting the ellipse then tuning its position etc using the adjustment macros.
The program applies the axial ratio of the previous measurement to the next measurement. This minimises the amount of manual adjustment needed BUT it is important to watch and fine tune the width each time. The use of this routine will reduce the variability in recorded axial ratios, so if this is of special interest the macro could be adjusted to force the user into correcting ellipticity each time.
[f8] remove overlay - clears screen of overlay (NB removing overlay makes selection tools work more smoothly)
[f9] change tool - toggle between ellipse measure tool (which is normally wanted) and line tool
Coccobiom2-collect: Macros for collecting specimens from batch processed images
Purpose: collect and count specimens from batch collected SEM images (i.e. images auto-captured in large batches on an automated SEM). Specifically they are designed for the use with images captured on the Southampton NOC SEM, but in principle should work fine with any SEM.
- Load macro set "Coccobiom2-collect
- Save SEM images to be analysed into folder named 'raw' inside a second folder with the Batch name or date on it. NB The raw folder can contain images from multiple specimens collected in the same batch run. There is no need to separate the images manually into separate folders or to rename them. My standard batches are 110 image collected in 11 rows of 10 images, but the macros can handle different batch sets.
INITIATION OF BATCH OF IMAGES
- Close any open images
- Run macro "Start batch [f4]"
- Macro first asks you to locate the folder which contains the folder of unprocessed images (NB This does need to be named 'raw')
- Macro then opens dialog box and asks for:
- Sample prefix; e.g JR271-CTD
- Sample number; eg. 060 (leaving this blank results in NaN)
- SEM image type (NOC for Southampton NOC images)
- magnification/1000 (5 for x5000 mag images)
- Number of files; i.e. the number of images in the batch (110 for my batch settings)
- First file; i.e. the number of the first file in this batch within the folder raw
- The macro then uses these values to:
- Create a new folder in the batch folder labelled with the sample prefix and number and inside this a series of folders to collect the sub-images
- Open the selected set of unprocessed images in a virtual stack
- Open a window labelled "Keys" containing summary of key macro assignments
Place cursor over specimen
Run key macro corresponding to the species - e.g. [a]. the macro then:
- makes a square selection around the specimen
- copies and pastes the specimen into a new file
- saves this file in the appropriate sub-folder
- increments a counter recording number of specimens identified
- marks the counted specimen with a small square.
Repeat until all specimens in the image have been counted
run macro next slice  to move on to next image and update log of progress (log window)
SPECIMENS WITH OVERLAPPING IMAGES
- If images are in isolated images - specimens at the edge should only be included if >50% of the specimen is in the image.
- If images are in a grid/array with significant overlap between tiles - an overlay rectangle can be added to the image and used as the margin of area for selecting specimens (this is very easy and works well - applied to batch images collected by Martine on NHM SEM).
- If images are in a grid/array with no overlap between tiles - this is the usual case with the NOC images. To handle this the macros have been written to determine of the specimen is likely to continue onto an adjacent image and if so to make a composite sub-image from both the active image and the adjacent image. (NB This works around all four edges but not at the corners, since this would require patching together a sub-image from 4 images which would have needed more programming than seemed worthwhile).
NB At present changing between these modes requires alteration to the macro code - should be rewritten to make it a selection in the start batch dialog.
NB After 60 distal Ehux specimens have been counted a message appears indicating this -> no need to collect further specimens
Window behaviour - the stack window in imageJ can seem to behave a bit erratically. Note the following
- to pan image holddown shift and use one finger on trackpad
- to flick through slices of the stack use < and > keys, or use scroll bar at bottom of image (but sroll keys on this work erratically)
- if stack window does not show the scroll bar at bottom try zooming to x100, resizing the image, then zooming back down.
- two-finger scrolling on the trackpad will result in scrolling through the slices.
- clicking in the scroll bar will result in scrolling through the slices.
Customising the key assignments.
At present this needs to be done by editing the macro - sparray assignments
Better would be to have macro read a text file
Start batch dialog
- having separate sample prefix and sample number fields is not really ideal, too many cases where it does not work, so probably better to use a single string number a string
FORMAT OF KEY ASSIGNMENT MATRIX
The key assignment data consists of the following fields for each key
- key assigned (from a to z)
- taxon name - this name is used to label a folder into which the sub-images will be saved, it also is shown in a window labelled "keys".
- relative size of sub-image - this a number usually between 1 and 4 which controls the size of the sub-image. On NOC images 1 is size to include an Ehux specimen nicely and is 100 x 100 pixels, which is 7 x 7 µm. On other SEM images the boxes are scaled up/down bed on the SEm magnification to select the same area.
- r.g.b colour - three values representing the RGB colour used to draw the outline of the selected area eg red is 255,0,0
Jeremy Young, UCL, March 2012, updated March 2015