Graphic STI
logo EPFL
english only
Biomedical Imaging Group
IP-LAB: Image Processing Laboratories
BIG > Teaching > IP-LAB > Morphological operators 18

Morphological Operators

1. Basic erosion and dilation

1.1 Dilation operator

Understand the code of the dilation operator that is provided in the method dilateBasic() of the file Find the structuring element from the Java code and fill the report. By using the plugin, describe how you would still be able to determine the structuring element without access to the source code. The name of the plugin is Basic Dilate.

1.2 Erosion operator

Write the method erodeBasic() that implements the erosion operator with the structuring element found in Question 1.1. The name of the plugin is Basic Erode.

Test your code on the images miretv and monarch.

2. Morphological operators with a user-defined structuring element

We want to implement the morphological operators the four following structuring elements.

2.1 Creation of structuring elements

A structuring element is a 2D boolean array containing true in the gray cells and false in the white cells. Write the code for the methods block(n), diamond(n), line(n) and corner(n) to build the "Block", "Diamond", "Line", and "Corner" structuring elements of size n. We assume that the size n is always odd and is greater than 2.
The provided method show(boolean se[][]) can display the structuring element as an image to help you check your results.

2.2 Implementation of the morphological operators

The task is to implement several operators that make use of the four structuring elements. The list of operations is provided in the box below. The user chooses the shape and the size of the structuring element through the dialog box. Test your code on the images miretv and monarch.

To make pixelwise arithmetic operations of ImageAccess objects, you can use the in-place methods as
image1.subtract(ImageAccess image2)
You can first protect your input data by duplicating the content using
ImageAccess a = image.duplicate()
This class is documented at

Operator Method in Name of the plugin
Dilation dilate() Dilate
Erosion erode() Erode
Open open() Open
Close close() Close
Gradient gradient() Gradient
Top Hat Dark topHatDark() Top Hat Dark
Top Hat Bright topHatBright() Top Hat Bright
Median† median() Median

Hint: To code the median filter, the method Arrays.sort() is useful to sort a one-dimensional array

Dialog box

2.3 Understanding of the morphological operators

  1. Compare the results of 1 x Dilate("Diamond",5) and 1 x Dilate("Block",5) on notes. Are they the same? Comment on the effect.
  2. Compare the results of 3 x Erode("Line",3) and 1 x Erode("Line",7) on notes. Are they the same? Comment on the effect.
  3. Compare the results of 3 x Close("Block",3) and 1 x Close("Block",3) on notes. Are they the same? Comment on the effect.
  4. Compare the Gradient("Diamond", 3) and Gradient("Corner", 3) on notes. Are they the same? Comment on the effect.
  5. Apply the Median("Block",3) on the image peppers and monarch. Comment on the effect.
  6. Apply the Median("Diamond",11) on the image peppers and monarch. Comment on the effect.

3. Application: Detect black note heads

Original image

Desired result

Propose an algorithm to detect only the black heads of the notes by doing any of the following steps (1) morphological operators of the Question 2.2, (2) the arithmetic operations of ImageJ and (3) a final thresholding.

The goal is to replace the black heads in the image notes by small squares (e.g., 7x7 pixel) and to remove everything else. Save the resulting image and briefly describe the steps and parameters of your algorithm.

4. Mouse tracking

We propose to build a program that can track the body of a mouse, excluding its tail.

a. Code the routine trackMouse() to segment the body of mouse using only the studied morphological operators and, as the last step, a threshold operation. The output body has to be white (value=255) over a black background (value=0) in the sequence of images stored as an array of ImageAccess objects. The sequence can be displayed using

// Show a sequence of images as a stack
ImageAccess out[] = new ImageAccess[n];
ImageAccess.Display display = new ImageAccess.Display(out, "Tracking");

b. Complete the routine trackMouse() to compute the center of gravity of the body in every binary images The trajectory can be displayed as a segmented line using 1 segment by frame transition.

// Draw a red line from point Fk (xa, ya) to point Fk+1 (xb, yb)
display.overlayLine(xa, ya, xb, yb); • 29.11.2017