How-To: Generate primary object masks

In this article, you can read on how to generate primary object masks.

The following figure shows an agglomerate and the three associated primary object masks. We can see that primary object masks are a good way to deal with overlapping objects. We can go even further by assigning classes to each of the primary object masks. In this example all masks have the same class. However, we could also mix multiple classes, e.g. spheres and cubes.

Primary object masks
Scanning electron microscope image of an agglomerate consisting of three primary particles (top left) and the three associated primary object masks

Unfortunately, at the moment, the only reliable way to generate primary object masks is to do so by hand. This means, an operator has to mark the outlines of the primary objects on an image. There are many tools available for this task. The choice of the optimal tool depends on the shape of the objects.

Circular or elliptical objects – Generate primary object masks with ImageJ

For circular and elliptical objects it suffices to store five values per object, to generate the primary object masks later on.

  • x-coordinate of the centroid
  • y-coordinate of the centroid
  • length of the long axis
  • length of the short axis
  • tilt angle

Based on these values, we can produce the desired primary object masks. For circular objects, the length of the long axis is equal to the length of the short axis and the tilt angle is 0°.

A good tool to generate the primary objects masks of circular and elliptical objects is ImageJ. In the following tutorials you can learn how to use ImageJ for this task:

Text tutorial
  1. Download ImageJ here.
  2. Unzip it to a path of your choice.
  3. Open ImageJ by double-clicking on ImageJ.exe.
    ImageJ: Main Window
  4. In the ImageJ main menu, goto Analyze Set Measurements… and check the following checkboxes:
    ImageJ: Set measurements
    All other properties (e.g. Area, Skewness, etc.) can be calculated based on the primary object masks later on.
  5. Open the image that you want to annotate in ImageJ.
  6. In the ImageJ main menu, goto Analyze Set Scale… . Then check Global, click on the button Click to Remove Scale and click on the button OK:
    ImageJ: Set Scale...
    We do this because we need to store all values in units of pixels so that we can draw the pixel-based masks from it. It is no problem if you need to have the values in real-life units as well. You can always calculate them later on by multiplying the pixel values with the scale of the image.
  7. In the ImageJ main window, right-click the second button on the left and click Elliptical selections:
    ImageJ: Elliptical Selections
  8. Now you can start annotating your objects. Define the length of the longer axis of the current annotation by clicking and dragging across a primary object. Subsequently, adjust the length of the shorter axis.  Now press the shortcut M on your keyboard. This will open the Results window.
    ImageJ: Results
    If you made an error, click on the corresponding entry in the Results window to mark it and remove it by right-clicking on it and selecting Clear.
  9. After you have annotated all objects on an image (also the objects touching the border), close the image and continue annotating the objects.
  10. When you processed all the images you have, save your results by pressing CTRL+S in the Results window. Depending on the number of images you are going to process, it might be a good idea to save your intermediate results to minimize the risk of loosing results, e.g. as a result of a system crash.

Arbitrarily shaped objects – Generate primary object masks with VIA

The easiest and most versatile way to generate the primary object masks for arbitrarily shaped objects is to approximate their shape with polygons. A very handy tool for this task is the VGG Image Annotator (VIA). In the following tutorials you can learn how to use it:

Text tutorial
  1. Download VIA here.
  2. Unzip it to a path of your choice.
  3. Start VIA by double-clicking on the via.html file.
  4. After VIA has opened in your browser, click on select images and select the images you want to annotate:
    VIA: load images
  5. Scroll down and expand the Attributes menu by clicking on it.
    VIA: Attributes
  6. Add the attribute “shape” by typing it into the text field and clicking on the +- symbol next to it.
    VIA: Attributes 2
  7. Select “dropdown” as the attribute Type and add the shape(s) of object(s) that are present on your images (e.g. ‘sphere’ and ‘cube’). Also you should make one of the shapes the default shape by clicking on the corresponding radio button.
    VIA: Attributes 3
  8. Now you can start to annotate your images. Therefore, select an appropriate region shape. For arbitrarily shaped objects, this will most likely be the Polygon region shape.
    VIA: Region Shape
  9. Annotate an object by clicking along it’s outlines. Finish an annotation by double-clicking, where you want the last point to be. To abort the annotation before it is complete, press Esc. You can also zoom in and out by holding Ctrl and turning your mouse wheel while hovering with your mouse over the image.
  10. To move a point of an already drawn annotation, drag the point where you want it to be. You can also add points by holding Ctrl and clicking on an edge of an annotation. To remove a point hold Ctrl and click on it. You can also move the complete annotation. To do so, select the annotation by clicking on it’s inside. Then drag the annotation where you want it to be. Remove annotations by selecting them and pressing Del. Annotations can also be copied with Ctrl+C and pasted with Ctrl+V.
  11. Optional: If you created more than one object shape in step 7 (e.g. ‘sphere’ and ‘cube’), then you need to set the correct object shape for each of your annotations. To do so, select each of the annotations one by one, by clicking on them and select the correct object shape from the dropdown menu.
    VIA: Shapes
    To get a better overview, you can select one of the annotations by clicking on it. Then hold Ctrl and turn your mouse wheel just a single notch, to display the shapes of the objects instead of their IDs.
  12. After you annotated all objects and set their shapes correctly, you can go to the next image by pressing N.
  13. When you are done annotating all the images or simply need a break you can save the project by clicking on Project  Save in the main menu. Give your project a meaningful name, make sure that all checkboxes are checked and click on OK. You can then resume it at a later point of time by loading it via Project  Load.
    VIA: Save project
  14. When you finished annotating all images, export the results as a JSON file by going to Annotations  Export Annotations (as json) in the main menu.


If you found this How-To useful, then be sure to check out our other How-Tos.